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 ...

Get Active Directory, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.