The second article in the “Windows Services Management With WMI” series discusses methods that allow parameters of Windows services to be modified.
The second article in Marcin Policht’s ‘Windows Services Management With WMI’ series discusses methods that allow parameters of Windows services to be modified.
The Win32_Service class offers two methods, ChangeStartMode and Change, which provide this functionality.
The ChangeStartMode method takes one input parameter, which determines the start mode of the target service and can take one of the following values:
- Boot
- System
- Automatic
- Manual
- Disabled
Only the last three are permitted for Windows services (the Boot and System are applicable only to Windows driver services).
The script below sets the StartMode of the Browser service to Disabled.
sComputer = "REPLACE ME !!!!" 'replace with name of target computer sService = "Browser" 'replace with name of the service to be started sStartMode = "Disabled" 'replace with desired start mode Set oInstance = GetObject("winmgmts:{impersonationLevel=impersonate}//" & sComputer & _ "/root/cimv2:Win32_Service=" & Chr(34) & sService & Chr(34)) Set oMethod = oInstance.Methods_("ChangeStartMode") Set oInParam = oMethod.inParameters.SpawnInstance_() oInParam.StartMode = sStartMode Set oOutParam = oInstance.ExecMethod_("ChangeStartMode", oInParam) If oOutParam.ReturnValue = 0 Then WScript.Echo "Startup mode of " & oInstance.Name & " changed successfully" Else WScript.Echo "Change of startup mode of " & oInstance.Name & " failed to start" Select Case oOutParam.ReturnValue Case 1 WScript.Echo "Not Supported" Case 2 WScript.Echo "Access Denied" Case 3 WScript.Echo "Dependent Services Running" Case 4 WScript.Echo "Invalid Service Control" Case 5 WScript.Echo "Service Cannot Accept Control" Case 6 WScript.Echo "Service Not Active" Case 7 WScript.Echo "Service Request Timeout" Case 8 WScript.Echo "Unknown Failure" Case 9 WScript.Echo "Path Not Found" Case 10 WScript.Echo "Service Already Running" Case 11 WScript.Echo "Service Database Locked" Case 12 WScript.Echo "Service Dependency Deleted" Case 13 WScript.Echo "Service Dependency Failure" Case 14 WScript.Echo "Service Disabled" Case 15 WScript.Echo "Service Logon Failure" Case 16 WScript.Echo "Service Marked For Deletion" Case 17 WScript.Echo "Service No Thread" Case 18 WScript.Echo "Status Circular Dependency" Case 19 WScript.Echo "Status Duplicate Name" Case 20 WScript.Echo "Status Invalid Name" Case 21 WScript.Echo "Status Invalid Parameter" Case 22 WScript.Echo "Status Invalid Service Account" Case 23 WScript.Echo "Status Service Exists" Case 24 WScript.Echo "Service Already Paused" End Select End If |
The Change method is much more flexible. Using it, you can set the following characteristics of a service:
- DisplayName — Displays the name of the service
- PathName — Fully qualified path to the executable that implements the service
- ServiceType: Type of service, which can take one of the following values:
- 1 – kernel driver
- 2 – file system driver
- 4 – adapter
- 8 – recognized driver
- 16 – own process
- 32 – share process
- 256 – interactive process
- ErrorControl — The severity in which the failure of the service affects the operating system; it can take one of the following values:
- 0 – ignore
- 1 – normal: The user is notified
- 2 – severe: The system is restarted with the last known good configuration
- 3 – critical: The system attempts to restart using the last known good configuration; if this causes error, the system halts
- StartMode — Indicates the start mode of the service and takes on the same values as in ChangeStartMode method
- DesktopInteract — True or False, depending on whether the service can communicate with Windows on the desktop
- StartName — The name of the account that the service runs under; the account can be specified in the format DomainUsername (for domain accounts) or .Username (for local computer accounts). On Windows XP systems in an Active Directory environment, you can also use the UPN format (i.e., username@domainname). When using LocalSystem or NetworkService accounts, ensure that the password is set to an empty string:
StartName = "LocalSystem" StartPassword = ""
or
StartName = "NT AUTHORITYNetworkService" StartPassword = ""
- StartPassword — Password of the account specified in the StartName parameter
- LoadOrderGroup — Group that the service is a member of; by grouping services together, you can control the startup sequence, and sequence can be determined by looking up its list in the HKEY_LOCAL_MACHINESystemCurrentControlSetControlServiceGroupOrder registry key
- LoadOrderGroupDependencies — An array containing a list of groups that contain services that must be started prior to the start of target service
- ServiceDependencies — An array containing a list of services that must be started prior to the start of target service
Note that when setting a domain or local user account as a service account, you must also grant this account the rights to log on as a service (NetworkService is granted this right by default, while LocalSystem account has unrestricted access to all local system resources). In Windows 2000 and later, this is controlled by Group Policy and can be set either locally or through Active Directory (in the Computer Configuration, Windows Settings, Security Settings, Local Policies, User Rights Assignment node by modifying the value of Log on as a service entry).
The following script changes the service account (including password information) and modifies the StartMode parameter. Within the script other parameters, not used in the example, have been commented out.
sComputer = "REPLACE ME !!!!" 'replace with name of target computer sStartMode = "Automatic" sStartName = ".SQLAgent" sStartPassword = "$w0rdFi$h" Set oInstance = GetObject("winmgmts:{impersonationLevel=impersonate}//" & sComputer & _ "/root/cimv2:Win32_Service=" & Chr(34) & sService & Chr(34)) Set oMethod = oInstance.Methods_("Change") Set oInParam = oMethod.inParameters.SpawnInstance_() '--oInParam.DisplayName = sDisplayName '--oInParam.PathName = sPathName '--oInParam.ServiceType = iServiceType '--oInParam.ErrorControl = iErrorControl oInParam.StartMode = sStartMode '--oInParam.DesktopInteract = bDesktopInteract oInParam.StartName = sStartName oInParam.StartPassword = sStartPassword '--oInParam.LoadOrderGroup = sLoadOrderGroup '--oInParam.LoadOrderGroupDependencies = aLoadOrderGroupDependencies '--oInParam.ServiceDependencies = aServiceDependencies Set oOutParam = oInstance.ExecMethod_("Change", oInParam) If oOutParam.ReturnValue = 0 Then WScript.Echo "Parameter(s) for " & oInstance.Name & " service changed successfully" Else WScript.Echo "Change of parameter(s) for " & oInstance.Name & " service failed" Select Case oOutParam.ReturnValue Case 1 WScript.Echo "Not Supported" Case 2 WScript.Echo "Access Denied" Case 3 WScript.Echo "Dependent Services Running" Case 4 WScript.Echo "Invalid Service Control" Case 5 WScript.Echo "Service Cannot Accept Control" Case 6 WScript.Echo "Service Not Active" Case 7 WScript.Echo "Service Request Timeout" Case 8 WScript.Echo "Unknown Failure" Case 9 WScript.Echo "Path Not Found" Case 10 WScript.Echo "Service Already Running" Case 11 WScript.Echo "Service Database Locked" Case 12 WScript.Echo "Service Dependency Deleted" Case 13 WScript.Echo "Service Dependency Failure" Case 14 WScript.Echo "Service Disabled" Case 15 WScript.Echo "Service Logon Failure" Case 16 WScript.Echo "Service Marked For Deletion" Case 17 WScript.Echo "Service No Thread" Case 18 WScript.Echo "Status Circular Dependency" Case 19 WScript.Echo "Status Duplicate Name" Case 20 WScript.Echo "Status Invalid Name" Case 21 WScript.Echo "Status Invalid Parameter" Case 22 WScript.Echo "Status Invalid Service Account" Case 23 WScript.Echo "Status Service Exists" Case 24 WScript.Echo "Service Already Paused" End Select End If |