Solved

Check if a process is running in vbs

Posted on 2009-03-11
4
3,690 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 500 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

This is an addendum to the following article: Acitve Directory based Outlook Signature (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_24950055.html) The script is fine, and works in normal client-server domains…
Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
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: …

729 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