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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

yehudahaCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.