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




The request was accepted.


The request is not supported.


The user did not have the necessary access.


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


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


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.


The service has not been started.


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


Unknown failure when starting the service.


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


The service is already running.


The database to add a new service is locked.


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


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


The service has been disabled from the system.


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


This service is being removed from the system.


The service has no execution thread.


The service has circular dependencies when it starts.


A service is running under the same name.


The service name has invalid characters.


Invalid parameters have been passed to the service.


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


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


The service is currently paused in the system.