VBS: Terminate multiple running applications

1

I am trying to close multiple applications at the same time using the following VBS script, but am getting the error 0x80041017 on the For Each loop line. It works fine with just one application but I added another to terminate

strComputer = "."
strProcessToKill1 = "Acrobat.exe" 
strProcessToKill2 = "iexplore.exe" 

Set objWMIService = GetObject("winmgmts:" _
                & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 

Set colProcess = objWMIService.ExecQuery _
                ("Select * from Win32_Process Where Name = '" & strProcessToKill1 & "' or '" & strProcessToKill2 & "'")


count = 0
For Each objProcess in colProcess
                objProcess.Terminate()
                count = count + 1
Next 
vba
vbscript
asked on Stack Overflow Apr 5, 2013 by Rick • edited Apr 5, 2013 by Bob77

1 Answer

5

Your WMI query is invalid, which is causing the loop to fail. You need to specify the column (Name) for the second half of the WHERE clause. I would also remove the brackets for the objProcess.Terminate call as well.

strComputer = "."
strProcessToKill1 = "Acrobat.exe" 
strProcessToKill2 = "iexplore.exe" 

Set objWMIService = GetObject("winmgmts:" _
                & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 

Set colProcess = objWMIService.ExecQuery _
                ("Select * from Win32_Process Where Name = '" & strProcessToKill1 & "' OR Name = '" & strProcessToKill2 & "'")


count = 0
For Each objProcess in colProcess
                objProcess.Terminate
                count = count + 1
Next 
answered on Stack Overflow Apr 5, 2013 by Fink • edited Apr 5, 2013 by Ansgar Wiechers

User contributions licensed under CC BY-SA 3.0