How do I Restart a Windows Service Remotely - VBScript
If you are every in need to restart a Windows service on a remote computer then the following script maybe useful:
Dim strCredentials, arrCredentials, strUser, strPassword, strNamespace, strLocalComputer, strComputer, strService, objService
On Error Resume Next
Const WbemAuthenticationLevelPktPrivacy = 6
strCredentials = InputBox _
(”Please enter the user name, a blank space, and then the password:”, _
“Enter User Credentials”)
If strCredentials = “” Then
Wscript.Quit
End If
arrCredentials = Split(strCredentials,” “)
strUser = arrCredentials(0)
strPassword = arrCredentials(1)
strNamespace = “root\cimv2″
Set objNetwork = CreateObject(”Wscript.Network”)
strLocalComputer = objNetwork.ComputerName
strComputer = InputBox _
(”Please enter the name of the computer you want to connect to:”, _
“Enter Computer Name”, strLocalComputer)
If strComputer = “” Then
Wscript.QUit
End If
Set objWbemLocator = CreateObject(”WbemScripting.SWbemLocator”)
Set objWMIService = objwbemLocator.ConnectServer _
(strComputer, strNamespace, strUser, strPassword)
objWMIService.Security_.authenticationLevel = WbemAuthenticationLevelPktPrivacy
‘ =====================================================================
‘Step 4. List running services
Set objWMIService = GetObject(”winmgmts:” _
& “{impersonationLevel=impersonate}!\\” & strComputer & “\root\cimv2″)
Set colRunningServices = objWMIService.ExecQuery(”Select * from Win32_Service”)
For Each objService in colRunningServices
Wscript.Echo objService.Name & VbTab & objService.State
Next
‘ =====================================================================
‘Step 5. User enters service name to be restarted
strService = InputBox _
(”Please enter the name of the service that you want to restart:”, _
“Enter Service Name”)
If strService = “” Then
WScript.Echo “No service name was given or you clicked Cancel”
WScript.Quit
End If
‘Step 6. Restart service
RestartServices ()
Sub RestartServices()
‘Set objWMIService = GetObject(”winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2″)
Set colServiceList = objWMIService.ExecQuery(”ASSOCIATORS OF {Win32_Service.Name=’” & strService & “‘} WHERE AssocClass=Win32_DependentService Role=Antecedent” )
For Each objService in colServiceList
WScript.Echo “Stopping: ” & objService.DisplayName
intResult = objService.StopService()
WScript.Echo “Result: ” & intResult
Next
WScript.sleep 10000
Set colServiceList = objWMIService.ExecQuery(”SELECT * FROM Win32_Service WHERE Name=’” & strService & “‘”)
For Each objService in colServiceList
WScript.Echo “Stopping: ” & objService.DisplayName
intResult = objService.StopService()
‘Step 7. Echo confirmatiom message that service stopped
WScript.Echo “Result: ” & intResult
WScript.Sleep 10000
intResult = objService.StartService()
‘Step 8. Echo confirmatiom message that service started
WScript.Echo “Starting: ” & objService.DisplayName
WScript.Echo “Result: ” & intResult
Next
End Sub
‘ =====================================================================
‘ End
‘ =====================================================================
Restart a Windows Service Remotely UncategorizedRestart a Windows Service Remotely Uncategorized


Leave a Reply