;; 
;;=====================================================================================----- 
;; 
;;FUNCTION       WMIProcessKill() 
;; 
;;ACTION         Terminates a specific process by numeric PID 
;; 
;;AUTHOR         Glenn Barnas 
;; 
;;VERSION        1.0 - 2007/10/20 
;; 
;;HISTORY        1.0 - 2007/10/20 - Initial Release 
;; 
;;SYNTAX         WMIProcessKill(Process [, Target] [, AuthPtr]) 
;; 
;;PARAMETERS     Process - REQUIRED - Integer 
;;               - The PID of process to terminate 
;; 
;;               Target - OPTIONAL - String 
;;               - The name of system to query 
;; 
;;               AuthPtr - OPTIONAL - Object 
;;               - The pre-authenticated WMI object pointer. 
;;                 Use WMIAuthenticate() udf to create the AuthPtr value. 
;;                 AuthPtr is not needed if user has admin rights. 
;; 
;;REMARKS        Terminates the process after verifying it exists 
;; 
;;RETURNS        Int: 1 (success) or 0 (failure) 
;; 
;;DEPENDENCIES   WMI 
;; 
;;TESTED WITH    W2K, WXP, W2K3, Vista, x64 
;; 
;;EXAMPLES        
; 
Function WMIProcessKill($_Process, Optional $_Computer, OPTIONAL $_pAuth)
 
  Dim $_objWMIService, $_colItems, $_objItem	; WMI object vars 
  Dim $_					; temp var 
  Dim $_Err					; error code 
 
  ; Must be a single numeric value to terminate 
  If Val($_Process) <> $_Process
    $WMIProcessKill = 0
    Exit 87
  EndIf
 
  ; insure a properly formatted computer name, default to local computer is not specified 
  $_Computer = IIf(Not $_Computer, '.', Join(Split($_Computer,'\'),''))
 
  ; If a pre-authenticated WMI object pointer was provided, use it, otherwise create a new object pointer 
  If $_pAuth
    $_objWMIService = $_pAuth
  Else
    $_objWMIService = GetObject('winmgmts:{impersonationLevel=impersonate}!\\' + $_Computer + '\root\cimv2')
    If @ERROR Exit Val('&' + Right(DecToHex(@ERROR), 4)) EndIf
  EndIf
 
  ; get the collection of process objects  
  $_colItems = $_objWMIService.ExecQuery("Select * from Win32_Process Where ProcessID=" + '"' + $_Process + '"',,48)
  If @ERROR   $_colItems = 0 Exit Val('&' + Right(DecToHex(@ERROR), 4)) EndIf
 
  $_Err = 2	; prepare for not found 
 
  ; Enumerate the collection of process data 
  For Each $_objItem in $_colItems
    $_ = $_objItem.Terminate
    $_Err = @ERROR
  Next
  $_colItems = 0
 
  ; return appropriate values 
  $WMIProcessKill = Not $_Err
  Exit $_Err
 
EndFunction