Killing processes in Vbscript

后端 未结 3 1780
日久生厌
日久生厌 2021-01-28 07:57

I am trying to kill all instances of a process called \"AetherBS.exe\" but the following VBscript is not working. I am not exactly sure where/why this is failing.

So ho

相关标签:
3条回答
  • 2021-01-28 08:14

    The problem is in the following line:

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

    Here you convert the Win32_Process.Name property value to uppercase, but don't convert the Appname to uppercase. By default, InStr performs a case-sensitive search, so if the input strings are the same but differ in case, you won't get a match.

    To fix the problem, you can convert Appname to uppercase as well:

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

    or you can use the vbTextCompare parameter to ignore the letter case:

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


    However, there's actually no need in this check at all as you can incorporate it directly in your query:

    Set colItems = objWMIService.ExecQuery( _
        "SELECT * FROM Win32_Process WHERE Name='" & Appname & "'", , 48)
    
    0 讨论(0)
  • 2021-01-28 08:23

    Here is the function to kill the process:

    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
    

    Usage:

    KillProc "AetherBS.exe"
    
    0 讨论(0)
  • 2021-01-28 08:24

    Try out below with batch script

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

    from cmd line use

    wmic path win32_process Where "Caption Like '%AetherBS.exe%'" Call Terminate
    
    0 讨论(0)
提交回复
热议问题