DekGenius.com
[ Team LiB ] Previous Section Next Section

26.5 Manipulating Services

Querying services is simple to do with WMI. The Win32_Service class is the WMI representation of a service. The Win32_Service class contains a lot of property methods that provide information about the service; the most useful ones have been listed in Table 26-2.

Table 26-2. Useful Win32_Service properties

Property

Description

AcceptPause

Returns a Boolean indicating whether the service can be paused.

AcceptStop

Returns a Boolean indicating whether the service can be stopped.

Description

Description of the service.

DisplayName

Display name of the service.

Name

Unique string identifier for the service.

PathName

Fully qualified path to the service executable.

Started

Boolean indicating whether the service has been started.

StartMode

String specifying the start mode of the service. Will be one of Automatic, Manual, or Disabled.

StartName

Account under which the service runs.

State

Current state of the service. Will be one of Stopped, Start Pending, Stop Pending, Running, Continue Pending, Pause Pending, Paused, or Unknown.

The following script retrieves all the running services on a machine. All we need to do is use a WQL query that finds all Win32_Service objects that have a state of "Running":

strComputer = "."
   
Set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set objServices = objWMI.ExecQuery _
          ("SELECT * FROM Win32_Service WHERE State = 'Running'")
For Each objService in objServices
   Wscript.Echo objService.DisplayName
   Wscript.Echo " Name: " & objService.Name
   Wscript.Echo " PathName: " & objService.PathName
   Wscript.Echo " Started: " & objService.Started
   Wscript.Echo " StartMode: " & objService.StartMode
   Wscript.Echo " StartName: " & objService.StartName
   Wscript.Echo " State: " & objService.State
   Wscript.Echo ""
next

Before you can start to manipulate the status of a service, you have to be able to find any dependent services. A dependent service requires the parent service to be running while it is running. If you try to stop the parent service without first stopping all dependent services, you will get an error. The following example shows how to find all dependent services for the IIS Admin service:

strService  = "IISADMIN"
strComputer = "."
   
Set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set objServiceList = objWMI.ExecQuery( _
             "Associators of {Win32_Service.Name='" & strService & "'} " & _
             "Where AssocClass=Win32_DependentServiceRole=Antecedent" )
WScript.Echo "List of dependent services for " & strService & ":"
For each objService in objServiceList
    WScript.Echo " " & objService.DisplayName
Next

You may have noticed the WQL query in this example is a little different than the ones we've used so far. We used something called the Associators for a class. One of the fundamental concepts within WMI is class association, which allows you to perform queries to retrieve objects that have dependencies or associations to a given object. Associators come into play in lot of situations, but a great example of them is with service dependencies. Some services are dependent on others in order to run. Using the Associators of clause within a WQL query allows you to find each dependent service.

Now that we can get a list of a service's dependent services, we can write scripts to stop, start, and restart a service. Table 26-3 lists the useful methods available to the Win32_Service class.

Table 26-3. Useful Win32_Service methods

Property

Description

ChangeStartMode

Changes the start mode for the service. Pass in Automatic, Manual, or Disabled.

PauseService

Pause a service.

ResumeService

Resume a service.

StartService

Start a service.

StopService

Stop a service.

Example 26-1 shows how to restart a service. Since there is no RestartService method available in WMI, you have to simulate a restart by stopping all dependent services, stopping the target service, then starting the target service and any dependent services.

Example 26-1. Using Win32_Service methods to simulate a RestartService method
strService  = "IISADMIN"
strComputer = "."
   
WScript.Echo "Restarting " & strService & "..."
   
' Stop dependent services
Set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set objServiceList = objWMI.ExecQuery("Associators of " _
                 & "{Win32_Service.Name='" & strService & "'} Where " _
                 & "AssocClass=Win32_DependentService " & "Role=Antecedent" )
for each objService in objServiceList
    WScript.Echo " Stopping " & objService.Name
    objService.StopService(  )
next
Wscript.Sleep 10000
   
' Stop target service
Set objService = objWMI.Get("Win32_Service.Name='" & strService & "'")
WScript.Echo " Stopping " & objService.Name
objService.StopService(  )
Wscript.Sleep 10000
   
' Start target service
Set objService = objWMI.Get("Win32_Service.Name='" & strService & "'")
WScript.Echo " Starting " & objService.Name
objService.StartService(  )
Wscript.Sleep 10000
   
' Start dependent services
Set objServiceList = objWMI.ExecQuery("Associators of " _
                 & "{Win32_Service.Name='" & strService & "'} Where " _
                 & "AssocClass=Win32_DependentService " & "Role=Antecedent" )
for each objService in objServiceList
    WScript.Echo " Starting " & objService.Name
    objService.StartService(  )
next
    [ Team LiB ] Previous Section Next Section