微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

我正在尝试创建一个 VBScript,如果它们存在,它会杀死 3 个进程。 cscript.exe wscript.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

解决方法

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。