在 Vbscript 中杀死进程 [英] Killing processes in Vbscript
问题描述
我正在尝试终止名为AetherBS.exe"的进程的所有实例,但以下 VBscript 不起作用.我不确定这在哪里/为什么会失败.
I am trying to kill all instances of a process called "AetherBS.exe" but the following VBscript is not working. I am not exactly sure where/why this is failing.
那么我怎样才能杀死AetherBS.exe"的所有进程?
So how can I kill all process of "AetherBS.exe?"
CloseAPP "AetherBS.exe"
Function CloseAPP(Appname)
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery( _
"SELECT * FROM Win32_Process", , 48)
For Each objItem In colItems
If InStr(1,Ucase(objItem.Name),Appname) >= 1 Then
objItem.Terminate
End If
Next
End Function
推荐答案
问题出在下面一行:
If InStr(1,Ucase(objItem.Name),Appname) >= 1 Then
此处将 Win32_Process.Name
属性值转换为大写,但不要将 Appname
转换为大写.默认情况下,InStr
执行区分大小写的搜索,因此如果输入字符串相同但大小写不同,您将不会得到匹配项.
Here you convert the Win32_Process.Name
property value to uppercase, but don't convert the Appname
to uppercase. By default, InStr
performs a case-sensitive search, so if the input strings are the same but differ in case, you won't get a match.
要解决此问题,您也可以将 Appname
转换为大写:
To fix the problem, you can convert Appname
to uppercase as well:
If InStr(1, UCase(objItem.Name), UCase(Appname)) >= 1 Then
或者您可以使用 vbTextCompare
参数忽略字母大小写:
or you can use the vbTextCompare
parameter to ignore the letter case:
If InStr(1, objItem.Name, Appname, vbTextCompare) >= 1 Then
但是,实际上根本不需要此检查,因为您可以将其直接合并到您的查询中:
However, there's actually no need in this check at all as you can incorporate it directly in your query:
Set colItems = objWMIService.ExecQuery( _
"SELECT * FROM Win32_Process WHERE Name='" & Appname & "'", , 48)
这篇关于在 Vbscript 中杀死进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!