How to remotely restart a service using a script?

This is to find out the script to restart a service of one of the remote server - MS WIndows 2008 R2. Preferrably to use vbscript. After that, send a notification by email. Please show step-by-step.

Thanks in advance.
MezzutOzilAsked:
Who is Participating?
 
Randy DownsOWNERCommented:
Try this

http://www.motobit.com/tips/detpg_vbs-wmi-restart-service/

RestartServices ".", "W3SVC,HTTPFilter,IISADMIN"
RestartServices ".", "SQLSERVERAGENT,MSSQLSERVER"


Sub RestartServices(Computer, ServiceNames)
  Dim ServiceName, Counter, aServiceNames

  'Get the array of service names  
  aServiceNames = split(ServiceNames,",")
  
  'loop services from beginning, stop them 
  For Each ServiceName In aServiceNames 
    StopService Computer, ServiceName, True
  Next 

  'loop services from end, start them 
  For Counter = ubound(aServiceNames) To 0 Step -1 
    StartService Computer, aServiceNames(Counter), True 
  Next 
End Sub

Sub StopService(Computer, ServiceName, Wait)
  Dim cimv2, oService, Result

  'Get the WMI administration object    
  Set cimv2 = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
    Computer & "\root\cimv2")

  'Get the service object
  Set oService = cimv2.Get("Win32_Service.Name='" & ServiceName & "'")
  
  'Check base properties
  If Not oService.Started Then
    ' the service is Not started
    wscript.echo "The service " & ServiceName & " is Not started"
    exit Sub
  End If

  If Not oService.AcceptStop Then
    ' the service does Not accept stop command
    wscript.echo "The service " & ServiceName & " does Not accept stop command"
    exit Sub
  End If
  
  'wscript.echo oService.getobjecttext_

  'Stop the service
  Result  = oService.StopService
  If 0 <> Result Then
    wscript.echo "Stop " & ServiceName & " error: " & Result
    exit Sub 
  End If 
  
  Do While oService.Started And Wait
    'get the current service state
    Set oService = cimv2.Get("Win32_Service.Name='" & ServiceName & "'")

    wscript.echo now, "StopService", ServiceName, oService.Started, _
      oService.State, oService.Status
    Wscript.Sleep 200
  Loop   
End Sub


Sub StartService(Computer, ServiceName, Wait)
  Dim cimv2, oService, Result

  'Get the WMI administration object    
  Set cimv2 = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
    Computer & "\root\cimv2")

  'Get the service object
  Set oService = cimv2.Get("Win32_Service.Name='" & ServiceName & "'")
  
  
  'Path = "winmgmts:{impersonationLevel=impersonate}!\\" & Computer & _
  '  "\root\cimv2:Win32_Service.Name='" & ServiceName & "'" 

  'Get the WMI administration object of the service    
  'Set oService = GetObject(Path)

  'Check base properties
  If oService.Started Then
    ' the service is Not started
    wscript.echo "The service " & ServiceName & " is started."
    exit Sub
  End If
  
  'Start the service
  Result = oService.StartService
  If 0 <> Result Then
    wscript.echo "Start " & ServiceName & " error:" & Result
    exit Sub 
  End If 
  
  Do While InStr(1,oService.State,"running",1) = 0 And Wait 
    'get the current service state
    Set oService = cimv2.Get("Win32_Service.Name='" & ServiceName & "'")
    
    wscript.echo now, "StartService", ServiceName, oService.Started, _
      oService.State, oService.Status
    Wscript.Sleep 200
  Loop   
End Sub

Open in new window

0
 
Steve KnightIT ConsultancyCommented:
I know you said preferably vbscript, which you have, but a batch file is one liner, using sc.exe btw:

sc \\servername stop "Service Name" && sc \\servername start "Service Name"

or to add a little structure:

@echo off
REM use variables to save repetition
set server=\\servername
set service="Service Name"

REM Stop, start, then check service is running.
sc %server% stop %service%
sc %server% start %service%
sc %server% query %service% | find "RUNNING" || echo SERVICE %service% NOT RESTARTED on %server%
0
 
Steve KnightIT ConsultancyCommented:
Sending the email depends upon your infrastructure but assuming you have an SMTP server available (amazing how many people do a script to restart their mail server and wonder why they never get an alert...) then you can use code like the attachment on my page here from VBScript or batch, or use a util. such as BLAT, also shown on there.

http://scripts.dragon-it.co.uk/links/email-from-batch

Steve
0
 
MezzutOzilAuthor Commented:
Splendid
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.