Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Check if a process is running in vbs

Posted on 2009-03-11
4
Medium Priority
?
4,059 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
Welcome back!  My apologies for taking so long to write part two of this series; it's been a long time coming!  As I promised in Part 1, this article will focus on how to locate those elusive AD properties that you are searching for.  Why is this us…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

618 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