' VBScript to kill a running program, pause for iDelay seconds then restart
' Demonstrates working with winmgmts, timer and shell
' On NT4 boxes this will require Windows Management Instrumentation to be installed
' http://www.microsoft.com/downloads/details.aspx?FamilyID=c174cfb1-ef67-471d-9277-4c2b1014a31e&displaylang=en
' ------------------------ -------------------------------'
Option Explicit
Dim oWMIService, oProcess, colProcess, oShell
Dim intErrNum
Dim strComputer, strProcessKill, strProcessFullPath, iDelay
'set Machine/Process variables here
strComputer = "Machine_Name"
strProcessKill = "wordpad.exe"
strProcessFullPath = """C:\Program Files\Windows NT\Accessories\wordpad.exe"""'Path has
spaces - needs to be sent as qutoed
'check for WMI - use WMI to connect to the registry
On Error Resume Next
Dim oReg : Set oReg = GetObject("winmgmts:root\default:StdRegProv")
intErrNum = Err.Number
On Error Goto 0
Err.Clear
'WMI connection error
If intErrNum <> 0 Then
MsgBox "This script requires " & Chr(34) & "WMI" & Chr(34) &_
", Windows Management Instrumentation, to run.", vbOKCancel + vbExclamation,"WMI Not
Installed!"
WScript.Quit
End If 'WMI execution error
iDelay = 30'30 second delay before restart'
'locate and kill the process
Set oWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
Set colProcess = oWMIService.ExecQuery ("Select * from Win32_Process" )
For Each oProcess in colProcess
if lcase(oProcess.Name) = lcase(strProcessKill) then
oProcess.Terminate()
'WSCript.Echo "Just killed process " & strProcessKill & " on " & strComputer
end if
Next
'pause for set delay
'WSCript.Echo "Pausing"
WScript.Sleep(iDelay*1000)
'WSCript.Echo "Restarting"
'run program
Set oShell = WScript.CreateObject("WScript.Shell")
oShell.run(strProcessFullPath)
Set oShell = Nothing
Set colProcess = Nothing
Set oWMIService = Nothing
WScript.Quit
Gerard's codebase
This is a personal repository of code snippets and information.
Over the years I have generated lots of little sub programs and stored away acres of useful code snippets. The problem is always to locate them.
Even more time wasting is forgetting how to do simple things when you havnt developed in an environment for a few years (or even a few months)
My new years resolution is to start putting them up in a common place as I produce them. (thanks google)
They are handy for me and, with a bit of a clean up and documentation, they might be handy for others if they wander in here.
Gerard 2008
Over the years I have generated lots of little sub programs and stored away acres of useful code snippets. The problem is always to locate them.
Even more time wasting is forgetting how to do simple things when you havnt developed in an environment for a few years (or even a few months)
My new years resolution is to start putting them up in a common place as I produce them. (thanks google)
They are handy for me and, with a bit of a clean up and documentation, they might be handy for others if they wander in here.
Gerard 2008
Tuesday, November 4, 2008
VB Script to Kill and Restart a running process
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment