Solved

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

Posted on 2013-02-06
1
663 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
In this article, I will show you HOW TO: Perform a Physical to Virtual (P2V) Conversion the easy way from a computer backup (image).
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…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

816 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

13 Experts available now in Live!

Get 1:1 Help Now