Here is part of a script that I poached from somewhere. It's only set to
stop a list of services, but you could include a second step in the
For...Next loop that calls the oInstance.ExecMethod_("StartService")
after you've stopped the service.

Watch for line wraps and such...


sComputer = '<enter the target computer name here>
' In 'services_list', add your sevices in the order you want them to
stop. 
' If some of the services have comma in their names, 
' you must choose another delimiter 
services_list =
"IMAP4Svc,POP3Svc,MSExchangeES,MSExchangeIS,MSExchangeMTA,MSExchangeSA,M
SExchangeMGMT,MSExchangeSRS,RESvc" 
services_array = Split(services_list,",") 


For i = 0 to UBound(services_array) 
  sService = Trim(services_array(i))
  'fileTxt.WriteLine("ServiceName = " & sService)
  Set oInstance =
GetObject("winmgmts:{impersonationLevel=impersonate}//" & sComputer & _
        "/root/cimv2:Win32_Service=" & Chr(34) & sService & Chr(34))
  'fileTxt.WriteLine("ServiceState = " &
oInstance.Properties_("State").Value)
  fileTxt.WriteLine(oInstance.Name & ": " &
oInstance.Properties_("State").Value)
  If (oInstance.Properties_("State").Value = "Running") Then
   Set oOutParam = oInstance.ExecMethod_("StopService")

   If oOutParam.ReturnValue = 0 Then
        fileTxt.WriteLine(oInstance.Name & " stopped successfully")
   Else
        fileTxt.WriteLine(oInstance.Name & " failed to stop")
        Select Case oOutParam.ReturnValue
                Case 1  fileTxt.WriteLine("The request is not
supported.")
                Case 2  fileTxt.WriteLine("The user did not have the
necessary access.")
                Case 3  fileTxt.WriteLine("The service cannot be stopped
because other " & _
                                        "services that are running are
dependent on it.")
                Case 4  fileTxt.WriteLine("The requested control code is
not valid, or " & _
                                        "it is unacceptable to the
service.")
                Case 5  fileTxt.WriteLine("The requested control code
cannot be sent to " & _
                                        "the service because the state
of the service.")
                Case 6  fileTxt.WriteLine("The service has not been
started.")
                Case 7  fileTxt.WriteLine("The service did not respond
to the stop request " & _
                                        "in a timely fashion.")
                Case 8  fileTxt.WriteLine("Unknown failure when stopping
the service.")
                Case 9  fileTxt.WriteLine("The directory path to the
service executable was not found.")
                Case 10 fileTxt.WriteLine("The service is already
stopped")
                Case 11 fileTxt.WriteLine("The service database is
locked.")
                Case 12 fileTxt.WriteLine("A dependency which this
service relies on " & _
                                        "has been removed from the
system.")
                Case 13 fileTxt.WriteLine("The service failed to find
the service needed " & _
                                        "from a dependent service.")
                Case 14 fileTxt.WriteLine("The service has been disabled
from the system.")
                Case 15 fileTxt.WriteLine("The service does not have the
correct authentication " & _
                                        "to run on the system.")
                Case 16 fileTxt.WriteLine("This service is being removed
from the system.")
                Case 17 fileTxt.WriteLine("There is no execution thread
for the service.")
                Case 18 fileTxt.WriteLine("There are circular
dependencies when stopping the service.")
                Case 19 fileTxt.WriteLine("There is a service running
under the same name.")
                Case 20 fileTxt.WriteLine("There are invalid characters
in the name of the service.")
                Case 21 fileTxt.WriteLine("Invalid parameters have been
passed to the service.")
                Case 22 fileTxt.WriteLine("The account, which this
service is to run under is " & _
                                        "either invalid or lacks the
permissions to run the service.")
                Case 23 fileTxt.WriteLine("The service exists in the
database of services " & _
                                        "available from the system.")
                Case 24 fileTxt.WriteLine("The service is currently
paused in the system.")
        End Select
   End If


      Do 
        ' state will be "Stop Pending" until "Stopped". 
        ' Adjust sleep as necessary, but do *not* remove it! 
        WScript.Sleep 1000 
       Set oInstance =
GetObject("winmgmts:{impersonationLevel=impersonate}//" & sComputer & _
            "/root/cimv2:Win32_Service=" & Chr(34) & sService & Chr(34))
       'fileTxt.WriteLine( "ServiceState = " &
oInstance.Properties_("State").Value
         Stopped = False
          if oInstance.Properties_("State") = "Stopped" Then 
            'fileTxt.WriteLine( sService & ": " &
oInstance.Properties_("State").Value 
            Stopped = True 
          end if 
      Loop until Stopped 

 End If
Next  

-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Mark Parris
Sent: Friday, January 20, 2006 9:06 AM
To: ActiveDir.org
Subject: [ActiveDir] OT: Script Request - Restart Remote Service

Does anyone have a nice applet to enable the remote manual restart of a
service on a server? The service permissions have been delegated as the
app that uses it is not very good and needs to be restarted numerous
times a day - it never hangs so the inbuilt stuff is no good. 

I have had a look but can find no examples to achieve my end goal.

Regards,

Mark
List info   : http://www.activedir.org/List.aspx
List FAQ    : http://www.activedir.org/ListFAQ.aspx
List archive:
http://www.mail-archive.com/activedir%40mail.activedir.org/
List info   : http://www.activedir.org/List.aspx
List FAQ    : http://www.activedir.org/ListFAQ.aspx
List archive: http://www.mail-archive.com/activedir%40mail.activedir.org/

Reply via email to