There are times when you want to know if a given process is running on a machine. For example, maybe you want to know if the "logon.scr" screensaver is running on the machine. This VBScript code will help you do that.
The function "ProcessIsRunning" below will take a single string or an array of strings as input. If you supply an array of strings, the function will return a value of "TRUE" if any of the processes named in the array are running. It will return "FALSE" if none of them are running. Similarly, if you supply a single string, if a running process matches that name, the function will return TRUE.
The code requires WMI services to be present on the machine.
'
' Lets you know if a process (or any of the processes listed in an array)
' is running on the local machine. Returns true if at least one is running,
' false if none are running.
'
Public Function ProcessIsRunning(processnames)
Dim processnamelist
Dim processname
Dim colMonitorProcesses
Dim objWMIService
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
Set WMI_ListOfProcessObjects = objWMIService.ExecQuery ("SELECT * FROM Win32_Process")
Set colMonitorProcesses = objWMIService.ExecQuery ("SELECT * FROM Win32_Process")
Dim dict : Set dict = CreateObject("Scripting.Dictionary")
If IsArray(processnames) Then
processnamelist = processnames
Else
processnamelist = Array(processnames)
End If
For Each processname In processnamelist
dict.Add lcase(processname),1
Next
Dim found
found = False
Dim process
found = False
For Each process In colMonitorProcesses
If dict.Exists(lcase(process.Name)) Then
found=True
End If
Next
If found Then
ProcessIsRunning=True
Exit Function
End If
ProcessIsRunning=False
End Function
