【问题标题】:vbscript check if multiple processes exist and kill themvbscript 检查是否存在多个进程并杀死它们
【发布时间】:2021-07-03 09:00:02
【问题描述】:

我正在尝试创建一个 VBScript 来杀死 3 个进程(如果它们存在)。 cscript.exewscript.execmd.exe

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

我需要这个,以防我制作另一个循环命令最终无限卡住的 VBScript。如果发生这种情况,我计划将此脚本链接到热键作为救命稻草。

我怎样才能为此添加更多进程?

' 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

【问题讨论】:

  • 这个问题会杀死同一进程的多个实例,而不是不同的进程。代码需要进行不同的设置。
  • 这是提问者的工作,Stack Overflow 提供了需要努力实施的理论,而不仅仅是希望有人为您编写。
  • 只是指出这仍然是重复的,只是因为您的流程要求不同,它并没有改变已经涵盖的事实(它在那里以黑色和书写,query multiple instances of the same process)。我以一种方式试图批评你个人,因为这不是这个社区所代表的。如果您认为是这种情况,我们深表歉意。

标签: loops vbscript kill-process wmi-service


【解决方案1】:

您可以将您想要杀死的进程存储到一个数组中,如下面的代码:

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

【讨论】:

    【解决方案2】:

    我能够借助 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
    '---------------------------------------------------------------------------------------------------
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多