Recipe7.10.Viewing the Antecedent and Dependent Services for a Service


Recipe 7.10. Viewing the Antecedent and Dependent Services for a Service

Problem

You want to view the services that a particular service depends on (i.e., antecedent services) and services that are dependent on that service. This is helpful to know when you want to stop a service and determine the impact it would have on other services.

Solution

Using a graphical user interface

  1. Open the Services snap-in.

  2. In the left pane, double-click on the service you want to view.

  3. Click the Dependencies tab.

Using a command-line interface:

The following command displays the services that depend on the specified service:

> sc \\<ServerName> enumdepend <ServiceName>

You can also use the following command:

> psservice \\<ServerName> <ServiceName> depend

The following command displays the services that the specified service depends on:

> sc \\<ServerName> qc <ServiceName>

Using VBScript
' This code lists the antecedent and dependent services for a service ' ------ SCRIPT CONFIGURATION ------ strService = "<ServiceName>"  ' e.g., TapiSrv strComputer = "<ServerName>"  ' e.g., fs-rtp01 (use . for local server) ' ------ END CONFIGURATION --------- set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") set colServices = objWMI.ExecQuery("Associators of " _                       & "{Win32_Service.Name='" & strService & "'} Where " _                       & "AssocClass=Win32_DependentService Role=Antecedent" ) WScript.Echo "Antecedent services for " & strService & ":" for each objService in colServices    Wscript.Echo vbTab & objService.DisplayName  next WScript.Echo      set colServices = objWMI.ExecQuery("Associators of " _                       & "{Win32_Service.Name='" & strService & "'} Where " _                       & "AssocClass=Win32_DependentService Role=Dependent" ) WScript.Echo "Dependent services for " & strService & ":" for each objService in colServices    Wscript.Echo vbTab & objService.DisplayName  next

Discussion

Service dependencies play a role in how you can stop a service. For example, if ServiceA depends on ServiceB, then ServiceB must be running before ServiceA can start. Similarly, ServiceB cannot be stopped until ServiceA is stopped due to the dependency. A good practical example of this is the Logical Disk Manager Administrative Service. It depends on the Logical Disk Manager service. It wouldn't make a lot of sense for the administrative service to be running, but not the underlying disk manager service (the thing it manages).

Service dependences are configured in each service's registry entry. In the case of the Logical Disk Manager Administrative Service, you can find its registry entry in the following key:

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\dmadmin

If you open Registry Editor (regedit.exe) and look at that key, you'll see a DependOnService value. The data for this REG_MULTI_SZ value is a list of the services it depends on. One of them is dmserver, which corresponds to the Logical Disk Manager service.

You may also see a DependOnGroup value under a service's registry key. This is similar to DependOnService except that DependOnGroup corresponds to a group of services. For more on service groups, check out Recipe 7.11.

Using VBScript

In the code I needed to use something called an associator. The Associators of clause in a WQL query is similar to a table join in a relational database. It allows you to relate two different types of WMI classes. For services, WMI supports a class called Win32_DependentService, which defines the service dependences for a given service. The first query finds all of the antecedent services (those that depend on the target service):

"Associators of {Win32_Service.Name='" & strService & "'} Where " _     "AssocClass=Win32_DependentService Role=Antecedent"

The Associators of clause tells WMI we are going to associate or join another class to the one specified within the curly braces. I set Win32_Service.Name equal to the target service. The Where clause then has two parts, the first sets the associated class, which in this case is Win32_DependentService. The second part (Role=Antecedent) limits the dependent services that are returned to just antecedent services, or ones that depend on the target service. Now look at the second query. The only difference is Role=Dependent returns all of the dependent services of the target service.



Windows Server Cookbook
Windows Server Cookbook for Windows Server 2003 and Windows 2000
ISBN: 0596006330
EAN: 2147483647
Year: 2006
Pages: 380
Authors: Robbie Allen

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