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

x
?
Solved

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

Posted on 2013-02-06
1
Medium Priority
?
683 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
[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
1 Comment
 
LVL 85

Accepted Solution

by:
oBdA earned 2000 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

Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Question has a verified solution.

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

If you're a modern-day technology professional, you may be wondering if certifications are really necessary. They are. Here's why.
By default Outlook 2016 displays only one time zone in the Calendar. The following article explains how to display two time zones in one calendar view.
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…
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.

636 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