Link to home
Start Free TrialLog in
Avatar of irishgx80
irishgx80

asked on

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

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

ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial