0

I am trying to create a VBScript that kills 3 processes if they exist. cscript.exe wscript.exe and cmd.exe

It needs to run a kill command and then check if the process still exists to verify that the previous command worked before continuing. I added a sleep command to give the script time to work before re-checking.

I need this in case I make another VBScript that loops a command that ends up getting stuck infinitely. I plan to link this script to a hotkey as a lifesaver should that happen.

How can I add more processes to this?

' TK CSCRIPT & WSCRIPT & CMD

Set objWMIService = GetObject ("winmgmts:")
    foundProc = False
    procName1 = "cscript.exe"

For Each Process in objWMIService.InstancesOf ("Win32_Process")
    If StrComp(Process.Name,procName1,vbTextCompare) = 0 then
        foundProc = True
        procID = Process.ProcessId
    End If
Next
If foundProc = True Then
    Set colProcessList = objWMIService.ExecQuery("Select * from Win32_Process where ProcessId =" &  procID)
    For Each objProcess in colProcessList
        objProcess.Terminate()
    Next
        WScript.Sleep(1000) 'wait 1 second before checking if the process still exists
    Set colProcessList = objWMIService.ExecQuery("Select * from Win32_Process where ProcessId =" &  procID)
    If colProcessList.count = 0 Then
    End If
End If
slyfox1186
  • 301
  • 1
  • 13
  • 1
    Does this answer your question? [vbs silent and graceful taskkill all chrome process](https://stackoverflow.com/questions/43063812/vbs-silent-and-graceful-taskkill-all-chrome-process) – user692942 Jul 03 '21 at 12:19
  • 1
    this question kills multiple instances of the same process, not different processes. The code needs to be set up differently. – slyfox1186 Jul 05 '21 at 07:18
  • It does which is the job of the asker, [so] provides the theory which requires effort to implement not just hope someone writes it for you. – user692942 Jul 05 '21 at 12:18
  • Was simply pointing out that is still a duplicate just because your process requirements are different it doesn't change the fact that this has already been covered (it's there in black and write, [query multiple instances of the same process](https://stackoverflow.com/a/51850196/692942)). I was in one way trying to criticize you personally as that is not what this community stands for. Apologies if you feel this was the case. – user692942 Jul 05 '21 at 13:45

2 Answers2

1

You can store what process did you want to kill into an Array like this code below :

Option Explicit
Dim Ws,fso,MyArray,LogFile,OutPut,count,MyProcess
Set Ws = CreateObject("Wscript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
MyArray = Array("cscript.exe","cmd.exe","wscript.exe","notepad.exe","mshta.exe")
LogFile = Left(Wscript.ScriptFullName,InstrRev(Wscript.ScriptFullName, ".")) & "log"
count = 0 
If fso.FileExists(LogFile) Then fso.DeleteFile LogFile
Set OutPut = fso.OpenTextFile(LogFile,8,True)

For Each MyProcess in MyArray
    Call Kill(MyProcess)
Next

OutPut.WriteLine String(50,"=") 
OutPut.WriteLine count & " Process were killed !"
OutPut.WriteLine String(50,"=")

If fso.FileExists(LogFile) Then
    ws.run LogFile 'To show the LogFile
End if
'---------------------------------------------------------------------------------------------------
Sub Kill(MyProcess)
On Error Resume Next
    Dim colItems,objItem
    Set colItems = GetObject("winmgmts:").ExecQuery("Select * from Win32_Process " _
    & "Where Name like '%"& MyProcess &"%' AND NOT commandline like '%" & wsh.scriptname & "%'",,48)
    For Each objItem in colItems
        count= count + 1
        OutPut.WriteLine Mid(objItem.CommandLine,InStr(objItem.CommandLine,""" """) + 2)
        objItem.Terminate(0)
        If Err <> 0 Then
            OutPut.WriteLine Err.Description
        End If
    Next
End Sub
'---------------------------------------------------------------------------------------------------
Hackoo
  • 18,337
  • 3
  • 40
  • 70
1

I was able to piggyback off of Hackoo's response so a big thank you to him for helping me get in the right direction.

Option Explicit
Dim fso,myArray,procName,ws

Set ws = CreateObject("Wscript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
    myArray = Array("cmd.exe","cscript.exe","wscript.exe")

For Each procName in myArray
    Call Kill(procName)
Next

'---------------------------------------------------------------------------------------------------
Sub Kill(procName)
    Dim colProcess,name,objWMIService,strComputer
        strComputer = "."
    Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    Set colProcess = objWMIService.ExecQuery ("Select * from Win32_Process Where Name like '" & procName & "'")
    For Each name in colProcess
        name.Terminate
    Next
End Sub
'---------------------------------------------------------------------------------------------------
slyfox1186
  • 301
  • 1
  • 13