Check if a process is running

Check if the Windows calculator is running on the local computer. Easily customized to check if any process is running on the local or a remote computer.

Check if a process is running
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
OPTION EXPLICIT
DIM strComputer,strProcess

strComputer = "." ' local computer
strProcess = "calc.exe"

' Check if Calculator is running on specified computer (. = local computer)
IF isProcessRunning(strComputer,strProcess) THEN
	wscript.echo strProcess & " is running on computer '" & strComputer & "'"
ELSE
	wscript.echo strProcess & " is NOT running on computer '" & strComputer & "'"
END IF

' Function to check if a process is running
FUNCTION isProcessRunning(BYVAL strComputer,BYVAL strProcessName)

	DIM objWMIService, strWMIQuery

	strWMIQuery = "Select * from Win32_Process where name like '" & strProcessName & "'"
	
	SET objWMIService = GETOBJECT("winmgmts:" _
		& "{impersonationLevel=impersonate}!\\" _ 
			& strComputer & "\root\cimv2") 

	IF objWMIService.ExecQuery(strWMIQuery).Count > 0 THEN
		isProcessRunning = TRUE
	ELSE
		isProcessRunning = FALSE
	END IF

END FUNCTION
Posted in VBScript