Wednesday, November 30, 2011

To check if any process is running on the local or a remote computer.

Many times with automation we need to find if a particular process is running on the pc. Then we need to kill it, for example a scheduling script..

here is a sample code to see if the process is running and then kill it-
option explicit
Dim strComputer,strProcess, colProcess, runningProcess
Dim objWMIService, strWMIQuery

strComputer = "."
strProcessName = "calc.exe"


strWMIQuery = "Select * from Win32_Process where name like '" & strProcessName & "'"

Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")

Set colProcess = objWMIService.ExecQuery (strWMIQuery)

' Suppose i want to terminate all these process
For Each runningProcess in colProcess
runningProcess.Terminate()
Next