【问题标题】:Killing processes in Vbscript在 Vbscript 中杀死进程
【发布时间】:2010-07-21 14:53:01
【问题描述】:

我正在尝试终止名为“AetherBS.exe”的进程的所有实例,但以下 VBscript 不起作用。我不确定在哪里/为什么会失败。

那么我怎样才能杀死“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

【问题讨论】:

  • 您是否收到错误消息?如果是,那么是哪个错误,在哪一行?另外,您使用的是什么操作系统?
  • 没有错误和 Windows Server 2003。

标签: vbscript wmi


【解决方案1】:

这里是杀死进程的函数:

Sub KillProc( myProcess )
'Authors: Denis St-Pierre and Rob van der Woude
'Purpose: Kills a process and waits until it is truly dead

    Dim blnRunning, colProcesses, objProcess
    blnRunning = False

    Set colProcesses = GetObject( _
                       "winmgmts:{impersonationLevel=impersonate}" _
                       ).ExecQuery( "Select * From Win32_Process", , 48 )
    For Each objProcess in colProcesses
        If LCase( myProcess ) = LCase( objProcess.Name ) Then
            ' Confirm that the process was actually running
            blnRunning = True
            ' Get exact case for the actual process name
            myProcess  = objProcess.Name
            ' Kill all instances of the process
            objProcess.Terminate()
        End If
    Next

    If blnRunning Then
        ' Wait and make sure the process is terminated.
        ' Routine written by Denis St-Pierre.
        Do Until Not blnRunning
            Set colProcesses = GetObject( _
                               "winmgmts:{impersonationLevel=impersonate}" _
                               ).ExecQuery( "Select * From Win32_Process Where Name = '" _
                             & myProcess & "'" )
            WScript.Sleep 100 'Wait for 100 MilliSeconds
            If colProcesses.Count = 0 Then 'If no more processes are running, exit loop
                blnRunning = False
            End If
        Loop
        ' Display a message
        WScript.Echo myProcess & " was terminated"
    Else
        WScript.Echo "Process """ & myProcess & """ not found"
    End If
End Sub

用法:

KillProc "AetherBS.exe"

【讨论】:

  • 那成功杀死了进程。有没有办法在没有 Windows 脚本主机消息框提示的情况下终止进程?我正在尝试自动化测试,提示有效地停止了脚本。
  • @iobrien:只需删除上面写着WScript.Echo的位置。
【解决方案2】:

问题出在下面一行:

If InStr(1,Ucase(objItem.Name),Appname) >= 1 Then

在这里您将Win32_Process.Name 属性值转换为大写,但不要将Appname 转换为大写。默认情况下,InStr 执行区分大小写的搜索,因此如果输入字符串相同但大小写不同,则不会匹配。

要解决此问题,您也可以将Appname 转换为大写:

If InStr(1, UCase(objItem.Name), UCase(Appname)) >= 1 Then

或者您可以使用vbTextCompare 参数忽略字母大小写:

If InStr(1, objItem.Name, Appname, vbTextCompare) >= 1 Then


但是,实际上根本不需要此检查,因为您可以将其直接合并到您的查询中:

Set colItems = objWMIService.ExecQuery( _
    "SELECT * FROM Win32_Process WHERE Name='" & Appname & "'", , 48)

【讨论】:

    【解决方案3】:

    下面用批处理脚本试试

    wmic path win32_process Where "Caption Like '%%AetherBS.exe%%'" Call Terminate
    

    从命令行使用

    wmic path win32_process Where "Caption Like '%AetherBS.exe%'" Call Terminate
    

    【讨论】:

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