Friday 4 October 2013

Count the Running instance of a process




howmanyareRunning("notepad.exe")

Function howmanyareRunning(iprocessName)


Dim objWMIService, objProcess, colProcess, strComputer, processName, instances

strComputer = "."
instances = 0
processName = iprocessName '"firefox.exe"

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

Set colProcess = objWMIService.ExecQuery _
("Select * from Win32_Process")

For Each objProcess in colProcess
If objProcess.Name = processName Then instances = instances + 1
Next

If processName = "wscript.exe" Then
    If instances = 1 Then
    WScript.Echo "There is no other VBScript running except this one!"
    Else
    WScript.Echo "There are currently " & "(" & instances _
       & ") " & "VBScript" & " Instances running!"
    End If
Else
    WScript.Echo "There are currently " & "(" & instances & ") " & _
            """" & processName & """" & " Instances running!"
End If

End Function


OUTPUT
=======