• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 693
  • Last Modified:

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

0
irishgx80
Asked:
irishgx80
1 Solution
 
oBdACommented:
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

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now