Recipe7.4.Setting the Service Startup Type


Recipe 7.4. Setting the Service Startup Type

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

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 unless another automatic service is dependent upon it, but it can be started later by someone with sufficient privileges. 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 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, if not, don't worry about it).

See Also

MSDN: Change Method of the Win32_Service Class



Windows Server Cookbook
Windows Server Cookbook for Windows Server 2003 and Windows 2000
ISBN: 0596006330
EAN: 2147483647
Year: 2006
Pages: 380
Authors: Robbie Allen

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