如何用VBScript杀死子进程?

时间:2013-12-04 15:45:55

标签: vbscript

我试图通过VBScript杀死进程,但它有一个子进程,我怀疑它正在重启它。我目前使用的程序是:

Const strComputer = "." 
Set WshShell = CreateObject("WScript.Shell")
Dim objWMIService, colProcessList
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colProcessList = objWMIService.ExecQuery("SELECT * FROM Win32_Process WHERE Name = 'Process.exe'")
For Each objProcess in colProcessList 
  WshShell.Exec "PSKill " & objProcess.ProcessId 
Next

这对我来说是最好的,因为该进程在SYSTEM下运行,因此很难用objProcess.terminate()来终止。我怎样才能获得所有子进程并将其杀死?

1 个答案:

答案 0 :(得分:0)

首先,您需要确定当前进程ID。您可以在https://stackoverflow.com/a/8438209/893670之前使用我之前分享过的CurrProcessId功能 确定当前流程ID后,除了列Name之外,您还需要使用当前流程ID值过滤列ParentProcessId。然后,您可以在循环中使用.Terminate方法终止枚举过程对象,或者如果您愿意,可以运行PSKill " & objProcess.ProcessId

Randomize
Function CurrProcessId
    Dim oShell, sCmd, oWMI, oChldPrcs, oCols, lOut
    lOut = 0
    Set oShell  = CreateObject("WScript.Shell")
    Set oWMI    = GetObject(_
        "winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
    sCmd = "/K @echo " & Int(Rnd * 3333) * CDbl(Timer) \ 1
    oShell.Run "%comspec% " & sCmd, 0
    WScript.Sleep 100
    Set oChldPrcs = oWMI.ExecQuery(_
        "Select * From Win32_Process Where CommandLine Like '%" & sCmd & "'", ,32)
    For Each oCols In oChldPrcs
        lOut = oCols.ParentProcessId
        oCols.Terminate
        Exit For
    Next
    Set oChldPrcs = Nothing
    Set oWMI = Nothing
    Set oShell = Nothing
    CurrProcessId = lOut
End Function

Dim PID
    PID = CurrProcessId

Const strComputer = "." 
Dim objWMIService, colProcessList
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colProcessList = objWMIService.ExecQuery("SELECT * FROM Win32_Process WHERE Name = 'Process.exe' And ParentProcessId = " & PID)
For Each objProcess in colProcessList 
    objProcess.Terminate
Next