Starting or Resuming Services

Microsoft® Windows® 2000 Scripting Guide

microsoft windows 2000 scripting guide

« Previous | Next »   

Although there might appear to be no practical difference between a service that is stopped and a service that is paused, the two states appear differently to the SCM. A stopped service is a service that is not running and must go through the entire service start procedure. A paused service, however, is still running but has had its functioning is suspended. Because of this, a paused service does not need to go through the entire service start procedure but needs a different procedure to resume functioning.

You must use the proper method to start a service that has been stopped or to resume a service that has been paused. The Win32_Service methods StartService and ResumeService should be used in the following situations:

  • If a service is currently stopped, you must use the StartService method to restart it; ResumeService cannot start a service that is currently stopped.
  • If a service is paused, you must use ResumeService. If you use the StartService method on a paused service, you receive the message, "The service is already running." However, the service remains paused until the resume service control code is sent to it.

Scripting Steps

The scripts for starting services that are stopped or resuming services that are paused are similar.

Starting automatic services that are stopped

Listing 15.11 contains a script that starts all automatic services that have been stopped. 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 restrict data retrieval to a subset of services, the query includes a Where clause that limits data retrieval only to autostart services that are currently stopped.
  4. For each service in the collection, use the StartService method to start the stopped service.

Listing 15.11   Starting Automatic Services That Are Stopped

1 2 3 4 5 6 7 8 9 
strComputer = "." Set objWMIService = GetObject("winmgmts:" _     & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set colListOfServices = objWMIService.ExecQuery _     ("SELECT * FROM Win32_Service WHERE State = 'Stopped' and StartMode = " _         & "'Auto'") For Each objService in colListOfServices     objService.StartService() Next

Resuming automatic services that are paused

To resume services that have been paused, use a script similar to Listing 15.12 but substitute the Paused property for the Stopped property in the Where clause and the ResumeService method for the StartService method, as shown in Listing 15.12.

Listing 15.12   Resuming Automatic Services That Are Paused

1 2 3 4 5 6 7 8 9 
strComputer = "." Set objWMIService = GetObject("winmgmts:" _     & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set colListOfServices = objWMIService.ExecQuery _     ("SELECT * FROM Win32_Service WHERE State = 'Paused' and StartMode = " _         & "'Auto'") For Each objService in colListOfServices     objService.ResumeService() 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