AKHIL K
AKHIL K

Reputation: 94

Kill a VBScript from another VBScript

Is there any way to kill a wscript.exe (windows process for vb script) from another VBScript (not from the currently executing wscript)?

If I create a script like this:

Set s = CreateObject("WScript.Shell")
s.Run "taskkill /im wscript.exe", , True

instead of killing the former script, this will kill itself.

Upvotes: 3

Views: 11065

Answers (2)

MC ND
MC ND

Reputation: 70951

Option Explicit

Dim strScriptToKill
    strScriptToKill = "ItIsDead.vbs"

Dim objWMIService, objProcess, colProcess

    Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
    Set colProcess = objWMIService.ExecQuery ( _ 
        "Select * from Win32_Process " & _ 
        "WHERE (Name = 'cscript.exe' OR Name = 'wscript.exe') " & _ 
        "AND Commandline LIKE '%"& strScriptToKill &"%'" _ 
    )
    For Each objProcess in colProcess
        objProcess.Terminate()
    Next

Upvotes: 2

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200503

I'd recommend using WMI for selecting and terminating the processes. Something like this should work:

Set wmi = GetObject("winmgmts://./root/cimv2")

qry = "SELECT * FROM Win32_Process WHERE Name='wscript.exe' AND NOT " & _
      "CommandLine LIKE '%" & Replace(WScript.ScriptFullName, "\", "\\") & "%'"

For Each p In wmi.ExecQuery(qry)
  p.Terminate
Next

Upvotes: 4

Related Questions