你可以使用Shell.Application
COM对象的InvokeVerb
方法。在cmd提示符下,你可以这样滥用PowerShell单行代码。
powershell "(new-object -COM Shell.Application).NameSpace(17).ParseName('D:').InvokeVerb('Eject')"
你也可以使用Windows脚本主机(VBScript / JScript)来调用COM对象。下面是一个使用Batch + Jscript混合脚本的例子(用.bat扩展名保存):
@if (@CodeSection == @Batch) @then
@echo off
setlocal
set "CDdrive=D:"
cscript /nologo /e:JScript "%~f0" "%CDdrive%"
goto :EOF
@end // end batch / begin JScript hybrid chimera
var oSH = WSH.CreateObject('Shell.Application');
oSH.NameSpace(17).ParseName(WSH.Arguments(0)).InvokeVerb('Eject');
如果你喜欢让你的脚本检测CD驱动器的驱动字母,也可以这样安排。这里是一个更完整的版本,并对一些非自明的值进行了注释。
@if (@CodeSection == @Batch) @then
@echo off
setlocal
cscript /nologo /e:JScript "%~f0"
goto :EOF
@end // end batch / begin JScript hybrid chimera
// DriveType=4 means CD drive for a WScript FSO object.
// See http://msdn.microsoft.com/en-us/library/ys4ctaz0%28v=vs.84%29.aspx
// NameSpace(17) = ssfDRIVES, or My Computer.
// See http://msdn.microsoft.com/en-us/library/windows/desktop/bb774096%28v=vs.85%29.aspx
var oSH = new ActiveXObject('Shell.Application'),
FSO = new ActiveXObject('Scripting.FileSystemObject'),
CDdriveType = 4,
ssfDRIVES = 17,
drives = new Enumerator(FSO.Drives);
while (!drives.atEnd()) {
var x = drives.item();
if (x.DriveType == CDdriveType) {
oSH.NameSpace(ssfDRIVES).ParseName(x.DriveLetter + ':').InvokeVerb('Eject');
while (x.IsReady)
WSH.Sleep(50);
}
drives.moveNext();
}