formatting cmdlt to eject USB via Powershell
7 views (last 30 days)
Show older comments
I need the ability to eject usb drives in a deployed program (Windows 10, R2019b), and TMW sadly still does not provide this functionality.
When I enter the concatenated cmdlt into the Windows Command Prompt window, it ejects the usb. When Invoke PowerSheel from the ML Command Window and enter the concatenated_cmdlt it also ejects the usb. However, when I execute !powershell concatenated_cmdlt or system('powershell concencatenated_cmdlt'), ML returns the following error message:
At line:1 char:92
+ ... bject Shell.Application; $driveEject.Namespace(17).ParseName(G:).Invo ...
+ ~
Missing ')' in method call.
At line:1 char:92
+ ... ect Shell.Application; $driveEject.Namespace(17).ParseName(G:).Invoke ...
+ ~~
Unexpected token 'G:' in expression or statement.
At line:1 char:94
+ ... ct Shell.Application; $driveEject.Namespace(17).ParseName(G:).InvokeV ...
+ ~
Unexpected token ')' in expression or statement.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : MissingEndParenthesisInMethodCall
I've read through all PS related Answers and have fiddled with single-quotes and double-quotes until the version below , which does not error but also does not do anything. Openng system.m yielded no info and neither did other forums.
I am hoping for a resolution, otheriwse I will need to deploy under linux where this is peanuts.
Thanks
function ejectusb(driveletter)
% this function removes the usb drive if it is not busy
% the original cmdlts were lifetd from:
% https://serverfault.com/questions/130887/dismount-usb-external-drive-using-powershell
% $driveEject = New-Object -comObject Shell.Application
% $driveEject.Namespace(17).ParseName("G:").InvokeVerb("Eject")
% in powershell this concatenated version worked fine
% $driveEject = New-Object -comObject Shell.Application; $driveEject.Namespace(17).ParseName("G:").InvokeVerb("Eject")
% the reworked version does not crash, but also not remove the usb
usbdrive = [driveletter, ':'];
if exist(usbdrive, 'dir')
% double-check thar all files are closed
fclose('all');
% eject USB - this does not work
% this reworked version was the olny version that dis not crash, but also did not remove the usb
system(['powershell $driveEject = New-Object -comObject Shell.Application;',...
'$driveEject.Namespace(17).ParseName(''' ,...
usbdrive ,...
''').InvokeVerb(''Eject'')']);
% check for result
if exist(usbdrive, 'dir')
disp('could not remove the usb drive')
else
disp('usb drive removed')
end
end
0 Comments
Answers (0)
See Also
Categories
Find more on File Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!