Stopping and Starting Dependent Services

http://technet.microsoft.com/en-us/library/ee198770.aspx

 

Stopping and Starting Dependent Services

Microsoft® Windows® 2000 Scripting Guide

Service dependencies are especially important when you try to stop services. To stop an antecedent service, you must first stop the dependent services. For example, if you attempt to stop the IIS Admin Service without first stopping dependent services such as FTP, SMTP, and World Wide Web Publishing Service, you receive an error message, and all the services continue to run.

Dependencies also affect the order in which services start. To start a dependent service, the antecedent service must start first. If you are starting a dependent service such as FTP, the antecedent service (IIS Admin) automatically starts first. Only after IIS Admin starts does the FTP service start.

However, starting the antecedent service first does not cause a dependent service to start. If you start the IIS Admin Service, that service itself starts, but its dependent services (such as FTP) do not automatically start at the same time.

In other words, stopping and restarting an antecedent service sometimes involves stopping and restarting a number of dependent services. You could do this manually - coding all the dependencies within your script. The difficulty with this approach is twofold. First, you must determine all the dependencies and manually add them to the script. Second, if those dependencies ever change (because of a service or operating system upgrade), your script must be modified to reflect these changes.

Alternatively, you can use WMI to enumerate the dependencies, and then stop and restart the appropriate services in the appropriate order. By using WMI, you avoid the problems of hard-coding service dependencies: you do not have to determine these dependencies beforehand, and you do not have to be concerned that changes to the operating system affect these dependencies. Instead, WMI determines the appropriate dependencies each time the script runs.

Scripting Steps

The scripts for stopping and starting dependent services perform similar steps but in the opposite order.

Stopping dependent services

Listing 15.16 contains a script that stops the IIS Admin Service and all its dependents. 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.

    This query must use an Associators of query and specify the following information:

    • The instance of the service on which the query is performed (Win32_Service.Name = 'IISAdmin').

    • The name of the Association class (AssocClass = Win32_DependentService). If the class name is not specified, the query returns all associated classes and their instances.

    • The role played by the IISAdmin Service. In this case, IISAdmin is Antecedent to the services to be returned by the query.

    The query returns a collection consisting of all the services dependent on the IIS Admin Service.

  4. For each service in the collection, use the StopService method to stop the service.

  5. After a stop control has been sent to each dependent service, pause for 60 seconds (60,000 milliseconds) to give the SCM time to stop each service.

  6. Use a the ExecQuery method to retrieve the instance of the IISAdmin Service.

  7. Use the StopService method to stop the IISAdmin Service.

Listing 15.16 Stopping a Service and Its Dependents

  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
 & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colServiceList = objWMIService.ExecQuery _
 ("ASSOCIATORS OF {Win32_Service.Name='iisadmin'} WHERE " _
 & "AssocClass=Win32_DependentService Role=Antecedent" )
For Each objService in colServiceList
 errReturn = objService.StopService()
Next
Wscript.Sleep 60000
Set colServiceList = objWMIService.ExecQuery _
 ("SELECT * FROM Win32_Service WHERE Name='iisadmin'")
For Each objService in colServiceList
 errReturn = objService.StopService()
Next

Starting dependent services

To start a service and all its dependents, simply reverse the process for stopping a service and its dependents: start the antecedent service, obtain a list of dependent services, and start each one. Listing 15.17 contains a script that starts the IIS Admin Service and all its dependents.

Listing 15.17 Starting Dependent Services

  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
 & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colServiceList = objWMIService.ExecQuery _
 ("SELECT * FROM Win32_Service WHERE Name='iisadmin'")
For Each objService in colServiceList
 errReturn = objService.StartService()
Next
Wscript.Sleep 60000
Set colServiceList = objWMIService.ExecQuery _
 ("ASSOCIATORS OF {Win32_Service.Name='iisadmin'} WHERE " _
 & "AssocClass=Win32_DependentService Role=Antecedent" )
For Each objService in colServiceList
 objService.StartService()
Next

 

posted @ 2011-03-22 08:37  chunchill  阅读(293)  评论(0编辑  收藏  举报