VBS:终止多个正在运行的应用程序

时间:2013-04-05 14:21:12

标签: vba vbscript

我正在尝试使用以下VBS脚本同时关闭多个应用程序,但我在0x80041017循环行上收到错误For Each。只使用一个应用程序它可以正常工作,但我添加了另一个应用程序

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 

1 个答案:

答案 0 :(得分:5)

您的WMI查询无效,导致循环失败。您需要为WHERE子句的后半部分指定列(名称)。我还会删除objProcess.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 Name = '" & strProcessToKill2 & "'")


count = 0
For Each objProcess in colProcess
                objProcess.Terminate
                count = count + 1
Next