Restart a process

This is a script that will monitor if a process is running more that once, if it is this script will kill them and restart it.  This example check if notepad.exe is running more than twice.

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colProcesses = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name = 'notepad.exe'")
'mineapp = "notepad.exe"
If colProcesses.Count => 2 Then
' Wscript.Echo mineapp & " is running 2 or more instances"
Set colProcessList = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name = 'notepad.exe'")
For Each objProcess in colProcessList
objProcess.Terminate()
Next

Set objShell = CreateObject("WScript.Shell")
objShell.Run "notepad.exe"

Else
' Wscript.Echo mineapp & " is running less than 2 instances"
End If

About

Microsoft

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.