Solved

VBS Script to Launch Multiple Applications if the Process is not running

Posted on 2013-02-06
1
657 Views
Last Modified: 2013-02-07
I have a script that I am using that launches multiple applications based on if the process is running or not.  If the process is not running, it does not launch.  I break them apart, but I know it can be coded into 2 functions vs 4 and I am having trouble putting it into code.  Help, below is my code.

Option Explicit
' ---------------------------------------------------------------------------------
'Globals
Dim objProcess, objWMIService, colProcesses, Process, strComputer
Dim programName1, programName2, programPath1, programPath2, processName, temp

'Set Configs
processName = Array("notepad.exe", "calc.exe")
programName1 = "notepad.exe"
programName2 = "calc.exe"
programPath1 = "%SystemRoot%\" & programName1
programPath2 = "%SystemRoot%\system32\" & programName2

If IsRunning1 = False Then
	Call RestartProcess1
Else
	'Process is running fine, we are good!
End If

If IsRunning2 = False Then
	Call RestartProcess2
Else
	'Process is running fine, we are good!
End If


' ---------------------------------------------------------------------------------
Function IsRunning1()
    Dim processRunning
    processRunning = false
    strComputer = "."
    Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer& "\root\cimv2")
    Set colProcesses = objWMIService.ExecQuery("SELECT * FROM Win32_Process WHERE Name='" & programName1 & "'")
    For Each Process in colProcesses
        If Process.Name = programName1 Then
            processRunning = True
        End If
    Next
    IsRunning1 = processRunning
End Function


' ---------------------------------------------------------------------------------
Sub RestartProcess1()
    Dim oShell
    Set oShell = WScript.CreateObject ("WSCript.shell")
    oShell.run "" & programPath1 & ""
	Set oShell = Nothing
End Sub


' ---------------------------------------------------------------------------------
Function IsRunning2()
    Dim processRunning
    processRunning = false
    strComputer = "."
    Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer& "\root\cimv2")
    Set colProcesses = objWMIService.ExecQuery("SELECT * FROM Win32_Process WHERE Name='" & programName2 & "'")
    For Each Process in colProcesses
        If Process.Name = programName2 Then
            processRunning = True
        End If
    Next
    IsRunning2 = processRunning
End Function


' ---------------------------------------------------------------------------------
Sub RestartProcess2()
    Dim oShell
    Set oShell = WScript.CreateObject ("WSCript.shell")
    oShell.run "" & programPath2 & ""
	Set oShell = Nothing
End Sub

Open in new window

0
Comment
Question by:irishgx80
1 Comment
 
LVL 83

Accepted Solution

by:
oBdA earned 500 total points
ID: 38863085
Try this:
Option Explicit
' ---------------------------------------------------------------------------------
'Globals
Dim objProcess, objWMIService, colProcesses, Process, strComputer
Dim strPathComponents, strProgram, strProgramPath, strProgramName, strProgramList

'Set Configs
strProgramList = Array( _
	"%SystemRoot%\notepad.exe", _
	"%SystemRoot%\system32\calc.exe" _
)

For Each strProgramPath In strProgramList
	strPathComponents = Split(strProgramPath, "\")
	strProgramName = strPathComponents(Ubound(strPathComponents))
	If IsRunning(strProgramName) Then
		'Process is running fine, we are good!
	Else
		RestartProcess strProgramPath
	End If
Next

' ---------------------------------------------------------------------------------
Function IsRunning(strProgram)
	Dim bProcessRunning
	bProcessRunning = false
	strComputer = "."
	Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer& "\root\cimv2")
	Set colProcesses = objWMIService.ExecQuery("SELECT * FROM Win32_Process WHERE Name='" & strProgram & "'")
	For Each objProcess in colProcesses
		If objProcess.Name = strProgram Then
			bProcessRunning = True
		End If
	Next
	Set colProcesses = Nothing
	Set objWMIService = Nothing
	IsRunning = bProcessRunning
End Function


' ---------------------------------------------------------------------------------
Sub RestartProcess(strProgramPath)
	Dim objShell
	Set objShell = WScript.CreateObject("WScript.Shell")
	objShell.Run "" & strProgramPath & ""
	Set objShell = Nothing
End Sub 

Open in new window

0

Featured Post

Are end users causing IT problems again?

You’ve taken the time to design and update all your end user’s email signatures, only to find out they’re messing up the HTML, changing the font and ruining the imagery. What can you do to prevent this? Find out how you can save your signatures from end users today.

Join & Write a Comment

How can you create a game plan that lets you focus on special projects instead of running from cubicle to cubicle every day and feeling like you’ve accomplished nothing? Try these strategies for prioritizing your tasks, offloading what you can, and …
In this article, I will show you HOW TO: Perform a Physical to Virtual (P2V) Conversion the easy way from a computer backup (image).
This video Micro Tutorial explains how to clone a hard drive using a commercial software product for Windows systems called Casper from Future Systems Solutions (FSS). Cloning makes an exact, complete copy of one hard disk drive (HDD) onto another d…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

746 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now