2010-07-21 205 views
0

我试图杀死一个名为“AetherBS.exe”的进程的所有实例,但是下面的VBScript不起作用。我不完全确定这是失败的原因。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 
+0

你得到一个错误?如果是,那么哪个错误和哪一行?另外,你在使用什么操作系统? – Helen 2010-07-21 15:00:11

+0

没有错误和Windows Server 2003. – 2010-07-21 15:05:25

回答

3

问题出在下面一行:

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) 
8

这里是杀死进程的功能:

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" 
+0

成功地终止了该进程。有没有一种方法可以在没有Windows Script Host消息框提示的情况下终止进程? 我正在尝试自动化测试,并且提示正在有效地停止脚本。 – 2010-07-21 15:04:52

+0

@iobrien:简单地删除它所说的“WScript.Echo”。 – Sarfraz 2010-07-21 15:15:24

-1

尝试下面用批处理脚本

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

从CMD线使用

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