vbscript-terminating process instance

2020-04-11 07:56发布

问题:

I'm writing a script that runs an external program, and after a while terminates it, the relevant code section looks like this:

Set objshell=createObject("Wscript.Shell")
objShell.run ""app.exe""
objShell.run ""app.exe""
WScript.Sleep(5000)

strWmiq = "select * from Win32_Process where name='app.exe'" 
Set objQResult = objWmi.Execquery(strWmiq)
    For Each objProcess In objQResult 
        intRet = objProcess.Terminate(1)
    Next 

What I want to do is to close each of the instances of app.exe after different sleep times, any ideas how to do that?

回答1:

Use WScript.Shell's .Exec method to get a WshScriptExec objects for each session/run/instance of app.exe and .Terminate them at your leisure (and risk). See Terminate Method (WshScriptExec)