您可以使用 Win32_Process WMI 类启动可执行文件以获取其进程 ID。在此之后,您可以查询 WMI 以判断该进程是否仍在运行。例子:
var SW_SHOWMINNOACTIVE = 7;
var WMI = GetObject("winmgmts:{impersonationlevel=impersonate}!root/CIMV2");
function processExists(processId) {
var processColl = new Enumerator(WMI.ExecQuery("select ProcessId " +
"from Win32_Process where ProcessId=" + processId.toString()));
return ! processColl.atEnd();
}
var startInfo = WMI.Get("Win32_ProcessStartup").SpawnInstance_();
startInfo.ShowWindow = SW_SHOWMINNOACTIVE;
var process = WMI.Get("Win32_Process");
var method = process.Methods_.Item("Create");
var inParams = method.InParameters.SpawnInstance_();
inParams.CommandLine = "cmd.exe";
inParams.CurrentDirectory = null;
inParams.ProcessStartupInformation = startInfo;
var outParams = process.ExecMethod_(method.Name, inParams);
if ( outParams.ReturnValue == 0 ) {
var processId = outParams.ProcessId;
WScript.Echo("ProcessId: " + processId.toString());
} else {
WScript.Echo("ReturnValue: " + outParams.ReturnValue);
WScript.Quit(outParams.ReturnValue);
}
while ( processExists(processId) ) {
WScript.Echo("Process still running");
WScript.Sleep(1000);
}