vbscript 检查是否存在多个进程并杀死它们

vbscript check if multiple processes exist and kill them

我正在尝试创建一个 VBScript 来终止 3 个进程(如果存在)。 cscript.exe wscript.execmd.exe

它需要 运行 一个 kill 命令,然后检查进程是否仍然存在,以验证前一个命令是否有效,然后再继续。我添加了一个睡眠命令,让脚本有时间在重新检查之前工作。

我需要这个,以防我制作另一个 VBScript 来循环一个命令,最终会无限卡住。我计划 link 将此脚本设置为热键,以备不时之需。

如何向其中添加更多进程?

' 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

您可以像下面的代码一样将要终止的进程存储到数组中:

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 的回复,非常感谢他帮助我朝着正确的方向前进。

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
'---------------------------------------------------------------------------------------------------