代码之家  ›  专栏  ›  技术社区  ›  Ian R. O'Brien Mamedov

在Vbscript中终止进程

  •  0
  • Ian R. O'Brien Mamedov  · 技术社区  · 14 年前

    那我怎么才能杀掉“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
    
    3 回复  |  直到 14 年前
        1
  •  4
  •   Helen    14 年前

    问题出现在以下行中:

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

    在这里你可以转换 Win32_Process.Name Appname 改为大写。默认情况下, InStr 执行区分大小写的搜索,因此如果输入字符串相同但大小写不同,则不会得到匹配。

    要解决此问题,可以将 应用程序名称 也要大写:

    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)
    
        2
  •  8
  •   Sarfraz    14 年前

    以下是终止进程的函数:

    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"
    
        3
  •  -1
  •   Maxime Lorant    10 年前

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

    从命令行使用

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