Pages

Sunday, January 31, 2010

Restart Windows services on remote computers

You can use the following script to restart services on a windows machine. Copy the script to a text file using notepad.exe and save the file as .vbs or download here.

' Restarts services on remote machines using WMI.
' Use at your own risk.
' Customize as you wish.
'------------------------------------------------

Option Explicit
Dim objWMIService, objItem, objService
Dim colListOfServices, strComputer, strService, intSleep

Dim strComputers(2)
' specify you servers here
strComputers(0) = "Server01"
strComputers(1) = "Server02"
strComputers(2) = "Server03"

intSleep = 15000

Dim i
For i = 0 to UBound(strComputers)

'On Error Resume Next
' NB strService is case sensitive.
strService = " 'Alerter' "

Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputers(i) & "\root\cimv2")

Set colListOfServices = objWMIService.ExecQuery _
("Select * from Win32_Service Where Name ="_
& strService & " ")

For Each objService in colListOfServices
objService.StopService()
WSCript.Sleep intSleep
objService.StartService()
Next
Next

WScript.Quit
' End of Example WMI script to Start / Stop services

No comments:

Post a Comment