Monitor and Start Process

By | October 13, 2006

Making my job easier through scripting is becomming a passion of mine.
I love to find problems that I can write a script for and then sit back and let the script do the work.
This script will list all running process and write tham to a text file.
Then it will search for a defined process in the file.
If the process doesn't exist, the script will start the app!

 ***This script has just been edited. In case you don't want to use a text file, I have created an array to perform the same function. It is a little faster!

 

———-COPY EVERYTHING BELOW THIS LINE———-

'This script is run as a scheduled task.
'It looks for a service and if the service is not running then it starts it.
'Created by Cheyenne Harden October 13, 2006

On Error Resume Next
Dim objWMIService, objProcess, colProcess, fso, objFSO1, objFile, objFolder
Dim strComputer, strList, strWritePath, textFile, strFile, strDirectory, i
Const OPEN_FILE_FOR_WRITING = 2
strComputer = "."
strFile = "strProcesses" &".txt"   ' Name of the text file
strWritePath = "C:\Scripts\" & strFile  'Path to write to
strDirectory = "C:\Scripts\"  'Path where the txt file will be created
i = 0

'#########

Set objFSO1 = CreateObject("Scripting.FileSystemObject")

If objFSO1.FileExists(strDirectory & strFile) Then
 Set objFolder = objFSO1.GetFile(strWritePath)

Else
 Set objFile = objFSO1.CreateTextFile(strDirectory & strFile)
 objFile = ""

End If

'#########

If strComputer > "" Then
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")

Set fso = CreateObject("Scripting.FileSystemObject")
Set textFile = fso.OpenTextFile(strWritePath, OPEN_FILE_FOR_WRITING)

Set colProcess = objWMIService.ExecQuery _
("Select * from Win32_Process")

For Each objProcess in colProcess
strList = strList & objProcess.Name & vbCrLf
Next

textFile.WriteLine(strList)

'*******************************

Const ForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strWritePath, ForReading)
strContents = objFile.ReadAll
objFile.Close

i = 0

arrLines = Split(strContents, " ")

For Each strLine in arrLines
    If InStr(strLine, "Timetrak.exe") Then  'Name of service monitoring
        i = i + 1
    End If
Next

'*******************************

If i >= 1 Then
  Wscript.Quit

 Else 
  AppToRun = "someApp.lnk"  'Name of app you want to execute
  CreateObject("Wscript.Shell").Run AppToRun  
   
End If

Else
End If
WScript.Quit

———-COPY EVERYTHING ABOVE THIS LINE———-

 

If you do not want to write to a text file to log the running processes use the script below.
This script uses an array to store the process ids.

 

———-Alternate Script: COPY EVERYTHING BELOW THIS LINE———-

On Error Resume Next
strComputer = "."
Dim strRunProcess, AppToRun, i
Dim arrProcess()
strRunProcess = "SomeProcess.exe"
i = 0

Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colProcess = objWMIService.ExecQuery _
("Select * from Win32_Process")

For Each objProcess in colProcess
 'strList = strList & objProcess.Name & vbCrLf
 ReDim Preserve arrProcess(intSize)
 arrProcess(intSize) = objProcess.Name

If arrProcess(intSize) = strRunProcess Then
'MsgBox strRunProcess & " Success!!!!!!!!!!!!!!!"
i= i + 1

Else
End If
 intSize = intSize +1
Next

Wscript.Echo i
If i > 0 Then
Wscript.Quit
Else

AppToRun = "SomeApp.lnk"
  CreateObject("Wscript.Shell").Run AppToRun
End If

———-Alternate Script: COPY EVERYTHING ABOVE THIS LINE———-

 

PLEASE MAKE SURE NO WORD WRAPPING IS HAPPENING IN YOUR SCRIPT!!!

To make this script work you will need a couople of things!

1. You will need to have Admin privlidges on the target computer.
2. Secondly, you will need the name of the process you would like to start (e.g., AppToRun = "someApp.lnk"  'Name of app you want to execute ).
3. You will need the name of the file you would like to create. (e.g., strFile = "strProcesses" &".txt"   ' Name of the text file)
4. You will need the path of the file. (e.g., strDirectory = "C:\Scripts\"  'Path where the txt file will be created)
5. The path to the txt file. (e.g., strWritePath = "C:\Scripts\" & strFile  'Path to write to)
6. The name of the service to monitor. (e.g., If InStr(strLine, "Timetrak.exe") Then  'Name of service monitoring)
7. ***If the path to the file you want to run has spaces then put a shortcut to the .exe under c:\Windows or WINNT\system32\ then you do not need a path for line:
  AppToRun = "someApp.lnk"

NOTE: Version 5.6 of the Windows Scripting Host must be installed to run this script!!!
  I have this running on a Win2K server as a scheduled task, because I do not want the wscript.exe running all the time in a loop.

This information is provided "AS IS" with no warranties expressed or implied.

Leave a Reply

Your email address will not be published. Required fields are marked *