Configuring Service Start Options

Microsoft® Windows® 2000 Scripting Guide

One key aspect of service management is determining when or if services run. The three options for configuring service start options are shown in Table 15.4.

Table 15.4 Service Start Options

Option

Description

Auto

Service is started automatically by the SCM during system startup. Autostart services start before a user logs on to the computer and run even if no user logs on to the computer.

Manual

Service is started by the SCM when a process calls the StartService method. Although manual services must be specifically started by a user (or by a script), they continue to run even if the user logs off.

Manual services are often referred to as on-demand services.

Disabled

Service that can no longer be started. To start a disabled service, you must first change the startup option to either Auto or Manual.

Start options are often changed in order to accomplish one of the following:

  • Ensure that a service automatically restarts when a computer restarts.

    Services set to Manual startup can be started only if a user logs on and either manually starts the service or runs a script to start the service. If the power fails, manual services do not automatically restart when the computer restarts.

  • Prevent Power Users from starting a service.

    Although you can use Group Policy to prevent Power Users from starting a service, you can also set the service start mode to Disabled. A disabled service cannot start unless it is reconfigured as either Manual or Auto. Because Power Users do not have the right to change service configurations, disabling a service also prevents these users from starting that service.

Scripting Steps

Listing 15.18 contains a script that disables all the on-demand services on a computer. To carry out this task, the script must perform the following steps:

  1. Create a variable to specify the computer name.

  2. Use a GetObject call to connect to the WMI namespace root\cimv2, and set the impersonation level to "impersonate."

  3. Use the ExecQuery method to query the Win32_Service class. To restrict data retrieval to a subset of services, a Where clause is used to limit data retrieval only to those services for which the StartMode property is equal to "Manual."

  4. For each service in the collection, use the Change method to set StartMode to "Disabled."

    With the Change method, the StartMode property must be the fifth parameter in the list of arguments supplied. Because of this, four empty arguments (represented by the four commas) must precede the new value for StartMode.

    If the service is currently running, StartMode is changed to Disabled. However, the service continues to run until it is stopped. If you want to immediately disable a service, your script can first stop the service and then change StartMode to Disabled. That both stops the service and prevents it from being restarted.

Listing 15.18 Configuring Service Start Options

  
1
2
3
4
5
6
7
8
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
 & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colServiceList = objWMIService.ExecQuery _
 ("Select * from Win32_Service where StartMode = 'Manual'")
For Each objService in colServiceList
 errReturnCode = objService.Change( , , , , "Disabled")
Next