Microsoft® Windows® 2000 Scripting Guide
« Previous | Next »
As noted earlier, dependent services are those services that cannot start unless the antecedent service is running. Likewise, you cannot stop an antecedent service if one of its dependents is still running.
These dependencies can complicate service management, particularly if you are creating automated methods for starting and stopping services. Because of this, identifying service dependencies is an important part of service management.
Scripting Steps
You can enumerate dependent services by doing the following:
- Enumerating dependent services for a single service. This approach is useful if you are creating an automated script for starting or stopping a particular service.
- Enumerating dependent services for all the services on a computer. This approach is useful if you would like to view the dependency relationships for all your services.
Enumerating dependent services for a single service
Listing 15.13 contains a script that enumerates the dependent services for the Remote Access Connection Manager service. To carry out this task, the script must perform the following steps:
- Create a variable to specify the computer name.
- Use a GetObject call to connect to the WMI namespace root\cimv2, and set the impersonation level to "impersonate."
- Use the ExecQuery method to query the Win32_Service class.
You 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 = Rasman ).
- 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 Rasman service. In this case, Rasman is antecedent to the services to be returned by the query.
This query returns a collection of all the services dependent on Remote Access Connection Manager.
- For each service in the collection, echo the service display name.
Listing 15.13 Enumerating Dependent Services for a Single Service
1 2 3 4 5 6 7 8 9
| strComputer = ". " Set objWMIService = GetObject( "winmgmts: " _ & "{impersonationLevel=impersonate}!\\ " & strComputer & "\root\cimv2 ") Set colServiceList = objWMIService.ExecQuery( "ASSOCIATORS OF " _ & "{Win32_Service.Name='rasman'} WHERE " _ & "AssocClass=Win32_DependentService " & "Role=Antecedent " ) For Each objService in colServiceList Wscript.Echo objService.DisplayName Next |
Enumerating dependent services for all the services on a computer
Listing 15.14 contains a script that enumerates the dependents for all the services installed on a computer. To carry out this task, the script must perform the following steps:
- Create a constant ForAppending, and set the value to 8.
This constant is used when opening the text file where the service dependency information is written.
- Create an instance of the FileSystemObject.
- Open the text file C:\Scripts\Service_dependencies.csv.
If the file does not exist, it is created automatically.
- Write the file header "Service Dependencies" to the text file.
- Create a variable to specify the computer name.
- Use a GetObject call to connect to the WMI namespace root\cimv2, and set the impersonation level to "impersonate."
- Use the ExecQuery method to query the Win32_Service class. This returns a collection consisting of all the services installed on the computer.
- For each service in the collection, use the ExecQuery method to retrieve the list of dependent services.
This query must use an Associators of query and specify the following information:
- The instance of the service on which the query is performed. In this script, the service name is stored in the variable strServiceRegistryName.
- 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 individual service. In this case, the service is Antecedent to the services to be returned by the query.
- Use the Count method to check the number of dependent services returned by the query.
If Count = 0, the service has no dependents, and the display name of the service and the value "None" are written to the text file.
If Count > 0, the display name of the service and the display name of its dependents are written to the text file.
- Close the text file.
Listing 15.14 Enumerating Dependent Services for All the Services on a Computer
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| Const ForAppending = 8 Set objFSO = CreateObject( "Scripting.FileSystemObject ") Set objLogFile = _ objFSO.OpenTextFile( "c:\scripts\service_dependencies.csv ", _ ForAppending, True) objLogFile.Write( "Service Dependencies ") objLogFile.Writeline strComputer = ". " Set objWMIService = GetObject( "winmgmts: " _ & "{impersonationLevel=impersonate}!\\ " & strComputer & "\root\cimv2 ") Set colListOfServiceS = objWMIService.ExecQuery _ ( "SELECT * FROM Win32_Service ") For Each objService in colListofServices objServiceRegistryName = objService.Name objServiceDisplayName = objService.DisplayName Set colServiceList = GetObject( "winmgmts: ").ExecQuery _ ( "ASSOCIATORS OF {Win32_service.Name=' " _ & objServiceRegistryName & _ "'} WHERE AssocClass=Win32_DependentService Role=Antecedent " ) If colServiceList.Count = 0 then objLogFile.Write(objServiceDisplayName) & ", None " objLogFile.Writeline Else For Each objDependentService in colServiceList objLogFile.Write(objServiceDisplayName) & ", " objLogFile.Write(objDependentService.DisplayName) objLogFile.Writeline Next End If Next objLogFile.Close |
Send us your feedback | « Previous | Next » |