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:
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:
StartName = "LocalSystem" StartPassword = "" |
or
StartName = "NT AUTHORITYNetworkService" StartPassword = "" |
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 |
Marcin Policht obtained his Master of Computer Science degree about 20 years ago and has been since then working in the Information Technology field, handling variety of responsibilities, but focusing primarily on the areas of identity and access management, virtualization, system management, and, more recently private, hybrid, and public cloud services. He has authored the first book dedicated to Windows Management Instrumentation and co-written several others dealing with subjects ranging from core operating system features to high-availability solutions. His articles have been published on such Web sites as ServerWatch.com and DatabaseJournal.com. For his contributions to the Microsoft technical community, he has been awarded the title of Microsoft MVP over the last ten years.
Property of TechnologyAdvice. © 2025 TechnologyAdvice. All Rights Reserved
Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.