Recipe 10.18. Setting the Startup Type of a Service


Problem

You want to configure the startup type (automatic, manual, or disabled) for a service.

Solution

Using a graphical user interface

  1. Open the Services snap-in.

  2. In the left pane, double-click on the service you want to configure.

  3. Choose the startup type under the General tab.

  4. Click OK.

Using a command-line interface
> sc config <ServiceName> start= [boot | system | auto | demand | disabled]

The following command disables the Messenger service:

> sc config Messenger start= disabled

Note that the space after start= is required.

Using VBScript
' This code sets the startup type for a service. ' ------ SCRIPT CONFIGURATION ------ strSvcName     = "MyMonitor" strStartupType = "Automatic"  ' can be "Automatic", "Manual", or "Disabled" strComputer    = "." ' ------ END CONFIGURATION --------- set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") set objService = objWMI.Get("Win32_Service.Name='" & strSvcName & "'") intRC = objService.Change(,,,,strStartupType) ' can alternatively use objService.ChangeStartup(strStartupType) method if intRC > 0 then    WScript.Echo "Error setting service startup type: " & intRC else    WScript.Echo "Successfully set service startup type" end if

Discussion

The startup type of a service determines whether the service starts when the system boots and whether it can be started at all. The Automatic startup type causes the service to automatically start at system boot up. The Manual startup type means the service will not be started automatically at system boot up, but can be started later manually. The Disabled startup type means that the service is not started at system boot up and cannot be manually started. You have to change the startup type to either Manual or Automatic before you can start a service that is set to Disabled.

You may have noticed that there are two other startup options you can configure with the sc utility. These additional startup options are in fact available with the scripting solution as well. They are, however, not applicable to system services. They are used for starting device drivers and can only be configured for drivers. The Boot startup type indicates that the driver is started by the operating system loader. The System startup type means that the driver will be started by the IoInitSystem method (if you are a driver programmer you know what this is).

See Also

MSDN: Change Method of the Win32_Service Class



Windows XP Cookbook
Windows XP Cookbook (Cookbooks)
ISBN: 0596007256
EAN: 2147483647
Year: 2006
Pages: 408

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net