Check if a process is running in vbs

Hi everybody,

I'm looking how to write a code in vbs to check if a windows process is running.

If the process is running, then, a msgbox, and loop until the process is not running.

When the process is not running, then, begin the code.

Many thanks in advance for your assistance.
OPatinyAsked:
Who is Participating?
 
yehudahaConnect With a Mentor Commented:
enter the process name here:

strProcess = "notepad.exe"

i commented the part you can insert your other code here:

' other code
strProcess = "notepad.exe"
 
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colProcessList = objWMIService.ExecQuery _
    ("Select * from Win32_Process")
For Each objProcess in colProcessList
    if objProcess.Name = strProcess Then
    msgbox objProcess.Name & " IS Running"
    check()
    End if
Next
 
' other code
MsgBox "other code"
 
 
Sub check()
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colMonitoredProcesses = objWMIService. _
    ExecNotificationQuery("select * from __instancedeletionevent " _ 
            & "within 1 where TargetInstance isa 'Win32_Process'")
i = 0
 
Do While i = 0
    Set objLatestProcess = colMonitoredProcesses.NextEvent
    if objLatestProcess.TargetInstance.Name = strProcess Then
    Exit Do
    End if
Loop
End Sub

Open in new window

0
 
OPatinyAuthor Commented:
Hi yehudaha,

Many thanks for your assistance.

It's working, but not exactly how i would.

With your code, if you run notepad and then, run your script, the script is stopped.

What i would is checking if notepad is running, then, show a msg if it's running, click on OK on the message, re-check if notepad is running, show the message box if notepad is still running, or begin a new code if notepad has been closed.

Many many thanks for your assistance.
0
 
OPatinyAuthor Commented:
Finnally, it's working, many thanks for your assistance.
0
 
yehudahaCommented:
no problem thanks for the grade

i hope i understand you but this what i wanted to post just now

i added a line to delay betwwen the checking of the 2 loop, play with it

WScript.Sleep 2000
strcomputer = "."
 
strProcess = "notepad.exe"
 
check1()
WScript.Sleep 2000
check1()
If check1 = True then
check2()
End if
 
MsgBox "other code"
 
function check1()
 
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colProcessList = objWMIService.ExecQuery _
    ("Select * from Win32_Process where name = 'notepad.exe'")
 
For Each objProcess in colProcessList
    if objProcess.Name = strProcess Then
    MsgBox strProcess & " Running"
    check1 = True
    Else
    check1 = false
    End If
Next
i = 1
End function
 
sub check2()
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colMonitoredProcesses = objWMIService. _
    ExecNotificationQuery("select * from __instancedeletionevent " _ 
            & "within 1 where TargetInstance isa 'Win32_Process'")
i = 0
 
Do While i = 0
    Set objLatestProcess = colMonitoredProcesses.NextEvent
    if objLatestProcess.TargetInstance.Name = strProcess Then
    Exit Do
    End if
Loop
End sub

Open in new window

0
All Courses

From novice to tech pro — start learning today.