Wednesday, 25 November 2015

Useful scripts - Processguard

Sometimes it is needed to watch running process and in case of termination execute it again. Here is simple WSH script, which uses WMI to check process name in the list of runnung processes and if "Process1name" isn't found, "Process1cmd" is executed.
Script checks processes each 10 seconds (Sleep command on the end of script). Check runs in infinite loop, so to terminate it, you have to kill it in task manager (wscript.exe or cscript.exe).

Dim strComputerName ' The Computer Name to be queried via WMI
Dim strWinMgt ' The WMI management String
Dim Processes1 'Hold Processes
Dim Count1
Count1 = 0

Const Process1name = "notepad.exe"
Const Process1cmd = "c:\windows\notepad.exe"

Set WshShell = WScript.CreateObject("WScript.Shell")

strComputerName = "LOCALHOST"

strWinMgt = "winmgmts://" & strComputerName

'connect to processes
do

Set Processes1 = GetObject(strWinMgt).ExecQuery ("Select * from Win32_Process where Caption="& chr(39) & Process1name & chr(39) &"")
'Loop through process
        for each Process in Processes1
                count1 = count1 + 1
        next

        if count1=0 then WshShell.Run Process1cmd

Set Processes1 = Nothing
 count1 = 0

  WScript.Sleep(10000)

loop until false



Save stript to file with VBS extension and HAVE FUN!