tags:

views:

27

answers:

2

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 how can I kill all process of "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
+1  A: 

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"
Sarfraz
That successfully killed the process. Is there a way to terminate the process without having the Windows Script Host message box prompting?I am trying to automate a test and the prompt is effectively halting the script.
iobrien
@iobrien: Simply remove where it says `WScript.Echo`.
Sarfraz
+2  A: 

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