?
Solved

Check if a process is running in vbs

Posted on 2009-03-11
4
Medium Priority
?
3,879 Views
Last Modified: 2012-05-06
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.
0
Comment
Question by:OPatiny
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 14

Accepted Solution

by:
yehudaha earned 2000 total points
ID: 23855605
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
 

Author Comment

by:OPatiny
ID: 23856617
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
 

Author Comment

by:OPatiny
ID: 23856863
Finnally, it's working, many thanks for your assistance.
0
 
LVL 14

Expert Comment

by:yehudaha
ID: 23857683
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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article we want to have a look at the directory attributes which are used by Microsoft to store the so called Security Identifiers (SID). These SIDs plays an important role in delegating and granting permissions and in authentication of trus…
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question