Changing Service State

Microsoft® Windows® 2000 Scripting Guide

You can use WMI to automatically start and stop services based on certain conditions. This enables you to create scripts that can query the services on multiple computers and then either start or stop specific services based on the criteria specified in the script.

For example, if you have a service with a known memory leak, you might need to periodically stop the service, wait a few minutes, and then restart it. Instead of doing this manually, you can schedule a WMI script to run at a specified interval (for example, every 72 hours) and stop and restart the service.

Alternatively, you can create a script that continuously monitors the memory usage of the process responsible for the service and then automatically stops and restarts the service if memory use exceeds a specified threshold.

The approach you take depends on the nature of the problem. If the service consistently runs for 72 hours without a problem, a scheduled script is the optimal solution: It can stop and restart the service before the memory leak occurs. If the memory leak occurs at more random intervals, continuous monitoring can enable you to identify the problem and fix it immediately, without having to wait until the next time the script is scheduled to run.

In addition to starting and stopping services, WMI can be used to pause services and to resume services that have been paused. The difference between a stopped service and a paused service depends on how the service is created. In general, a stopped service quits functioning and disconnects all users currently using the service. A paused service no longer accepts new connections but continues to support users already connected to the service.

When you control services by using WMI, an error code is returned indicating the success or failure of the operation. These error codes are shown in Table 15.2.

Table 15.2 Service Method Error Codes

Value

Description

0

The request was accepted.

1

The request is not supported.

2

The user did not have the necessary access.

3

The service cannot be stopped because other services that are running are dependent on it.

4

The requested control code is not valid, or it is unacceptable to the service.

5

The requested control code cannot be sent to the service because the state of the service (Win32_BaseService State property) is equal to 0, 1, or 2.

6

The service has not been started.

7

The service did not respond to the start request in a timely fashion.

8

Unknown failure when starting the service.

9

The directory path to the service executable file was not found.

10

The service is already running.

11

The database to add a new service is locked.

12

A dependency this service relies on has been removed from the system.

13

The service failed to find the service needed from a dependent service.

14

The service has been disabled from the system.

15

The service does not have the correct authentication to run on the system.

16

This service is being removed from the system.

17

The service has no execution thread.

18

The service has circular dependencies when it starts.

19

A service is running under the same name.

20

The service name has invalid characters.

21

Invalid parameters have been passed to the service.

22

The account under which this service runs is either invalid or lacks the permissions to run the service.

23

The service exists in the database of services available from the system.

24

The service is currently paused in the system.