Solved

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

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

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

Portable, direct connect server access

The ATEN CV211 connects a laptop directly to any server allowing you instant access to perform data maintenance and local operations, for quick troubleshooting, updating, service and repair.

Question has a verified solution.

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

When you try to extract and to view the contents of a Microsoft Update Standalone Package (MSU) for Windows Vista, you cannot extract the files from the MSU. Here we are going to explain how to extract those hotfix details without using any third pa…
Read about why it is more lucrative for an IT company to participate in government projects.
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…
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

726 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