Stopping or Pausing Services

Microsoft® Windows® 2000 Scripting Guide

microsoft windows 2000 scripting guide

« Previous | Next »   

After you have determined which services can be stopped or paused, you can use the StopService and PauseService methods to stop and pause services. The decision to stop a service rather than pause it, or vice versa, depends on several factors, including the following:

  • Is the service capable of being paused? If not, your only option is the stop the service.
  • Do you need to continue handling client requests for anyone already connected to the service? If so, pausing a service typically allows it to handle existing clients while denying access to new clients. By contrast, when you stop a service, all clients are immediately disconnected.
  • Do you need to reconfigure a service and have the changes take effect immediately? Although service properties can be changed while a service is paused, most of them do not take effect until the service is actually stopped and restarted.

The scripting code required to stop a service is almost identical to the code required to pause the service.

Scripting Steps

The scripts for stopping services and pausing services are similar.

Stopping services

Listing 15.9 contains a script that stops all the services running under a specified user account. To carry out this task, the script must perform the following steps:

  1. Create a variable to specify the computer name.
  2. Use a GetObject call to connect to the WMI namespace root\cimv2, and set the impersonation level to "impersonate."
  3. Use the ExecQuery method to query the Win32_Service class. To limit data retrieval to a specific set of services, a Where clause is included restricting the collection to those services with the StartName .\\Netsvc.
  4. For each service in the collection, stop the service using the StopService method.

    The return code for the StopService method is stored in the errReturnCode variable. In a production script, it is a good idea to check the value of that variable to ensure that the method was successfully applied.

Listing 15.9   Stopping Services Running Under a Specified Account

1 2 3 4 5 6 7 8 
strComputer = "." Set objWMIService = GetObject("winmgmts:" _     & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set colServices = objWMIService.ExecQuery _     ("SELECT * FROM win32_Service WHERE StartName = '.\\Netsvc'") For Each objService in colServices     errReturnCode = objService.StopService() Next

Pausing services

To pause services, use a script similar to Listing 15.9 but substitute the PauseService method for the StopService method, as shown in Listing 15.10.

Listing 15.10   Pausing Services Running Under a Specified Account

1 2 3 4 5 6 7 8 
strComputer = "." Set objWMIService = GetObject("winmgmts:" _     & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set colProcesses = objWMIService.ExecQuery _     ("SELECT * FROM Win32_Service WHERE StartName = '.\\Netsvc'") For Each objService in colServices     errReturnCode = objService.PauseService() Next

send us your feedback Send us your feedback « Previous | Next »   


Microsoft Windows 2000 Scripting Guide(c) Automating System Administration 2003
Microsoft Windows 2000 Scripting Guide(c) Automating System Administration 2003
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 635

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net