Break on
$process = "notepad.exe"
$rc = KillProcess($process)
If $rc = "0"
?$process " successfully stopped."
?"Error code: " $rc
Else
?$process " NOT successfully stopped."
?"Error code: " $rc
EndIf
Sleep 5
;DO NOT MODIFY ANYTHING BELOW THIS LINE.
;THIS IS A UDF AND IT COMES READY MADE.
;
;Function KillProcess() - Kills the Process specified
;
;Author Arend Pronk (apronk on the forum)
;
;Contributors Chris S. (based on his ProcessMon)
; Radimus (his suggestions to change some code)
;
;Action Kills the Process specified and returns a 1 for success or 0 for failure
;
;Syntax KillProcess($sProcessName,Optional $sComputer)
; Where $sProcessName is the name of the process you are trying to kill
; and $sComputer is the name of the computer to kill the process on.
; If no computer is specified it is done on the local computer.
; Remote computers require this script to be run as an admin of the remote computer
;
;Version 1.0
;
;Date 2006-Jan-27
;
;Date Revised 2006-Jan-29
;
;Revision Reason
; Revised per Radimus's suggestions.
;
;Returns 0 for success, Errorlevel for failure
;
;Dependencies KiXtart 4.02 Recommend 4.51 or newer
;
;KiXtart Ver. Written and tested with KiXtart v4.52 Beta 2 on Windows XP/2003
;
;Example
; Dim $KP
; $KP = KillProcess("notepad.exe")
; If $KP = 0
; ? "Process killed succesfully!"
; Else
; ? "Process killing failed!"
; EndIf
;
Function KillProcess($sProcessname, Optional $sComputer)
Dim $objWMIService, $objInstance, $colItems, $objItem, $strWBEMPath
If $sComputer = ""
$sComputer = "."
EndIf
$objWMIService = GetObject("winmgmts:\\" + $sComputer + "\root\cimv2")
If @ERROR
Exit @error
EndIf
$colItems = $objWMIService.ExecQuery("Select * from Win32_Process",,48)
For Each $objItem In $colItems
If $objItem.Name = $sProcessname
$strWBEMPath = "Win32_Process.Handle=" + $objItem.ProcessID
$objInstance = $objWMIService.Get($strWBEMPath)
$KillProcess = $objInstance.Terminate(1)
EndIf
Next
Return $KillProcess
EndFunction