Kill a running program





3
Date Submitted Wed. Mar. 1st, 2006 5:07 AM
Revision 1 of 1
Syntax Master dannyboy
Tags KILL | VBSCRIPT
Comments 0 comments
Kill a running program on a wmi enabled compute

' Initialize variables
strMsg   = ""
numFound = 0

'
Check command line parameters
Select Case WScript.Arguments.Count
        Case 0
                ' At least a process should be specified
                Syntax
        Case 1
                '
First parameter should be the process name
                ' or "/?" to request online help
                strP2k = Wscript.Arguments(0)
                if InStr( strP2k, "?" ) > 0 Then Syntax
                strComputer = "."
        Case 2
                '
First parameter should be the process name
                strP2k = Wscript.Arguments(0)
                if InStr( strP2k, "?" ) > 0 Then Syntax
                ' Second parameter should be a computer name
                strComputer = Wscript.Arguments(1)
                if InStr( strComputer, "?" ) > 0 Then Syntax
        Case Else
                '
Maximum is 2 command line parameter
                Syntax
End Select

On Error Resume Next
' Query and running processes
Set objWMIService = GetObject( "winmgmts://" & strComputer & "/root/cimv2" )
'
Display error number and description if applicable
If Err.Number Then
        strMsg = vbCrLf & "Error # " & Err.Number & vbCrLf & _
                 Err.Description & vbCrLf & vbCrLf
        Syntax
End If
On Error GoTo 0
' Kill specified process
Set colItems = objWMIService.ExecQuery( "Select * from Win32_Process", , 48 )
For Each objItem in colItems
        If LCase( objItem.Caption ) = LCase( strP2k ) Then
                numFound = numFound + 1
                Wscript.Echo "Terminating " & objItem.Caption
                objItem.Terminate
        End If
Next

'
Check if any process was found
If numFound = 0 Then
        WScript.Echo "No process named " & Chr(34) & strP2k & Chr(34) & " was found"
End If

' Done
WScript.Quit(0)


Sub Syntax
        strMsg = strMsg & vbCrLf & "KillProg.vbs,  Version 1.01" & vbCrLf & _
                 "Terminate (" & Chr(34) & "kill" & Chr(34) & _
                 ") a running program." & vbCrLf & vbCrLf & _
                 "Usage:  CSCRIPT  KILLPROG.VBS  prog_name  [ computer_name ]" & _
                 vbCrLf & vbCrLf & _
                 "Where:  " & Chr(34) & "prog_name" & Chr(34) & _
                 "     is the name of the program to be terminated" & vbCrLf & _
                 "        " & Chr(34) & "computer_name" & Chr(34) & _
                 " is the name of the computer where " & Chr(34) & _
                 "prog_name" & Chr(34) & " is" & vbCrLf & _
                 "                        to be terminated" & _
                         vbCrLf
        WScript.Echo strMsg
        '
Abort with return code 1
        WScript.Quit(1)
End Sub

 

Comments

There are currently no comments for this snippet.

Voting