StartService method of the Win32_Service Class
Applies to: desktop apps only
The StartService method attempts to place the referenced service into its startup state.
This topic uses Managed Object Format (MOF) syntax. For more information about using this method, see Calling a Method.
Syntax
uint32 StartService();
Parameters
This method has no parameters.
Return value
| Return code | Description |
|---|---|
|
Success |
|
Not Supported |
|
Access Denied |
|
Dependent Services Running |
|
Invalid Service Control |
|
Service Cannot Accept Control |
|
Service Not Active |
|
Service Request Timeout |
|
Unknown Failure |
|
Path Not Found |
|
Service Already Running |
|
Service Database Locked |
|
Service Dependency Deleted |
|
Service Dependency Failure |
|
Service Disabled |
|
Service Logon Failure |
|
Service Marked For Deletion |
|
Service No Thread |
|
Status Circular Dependency |
|
Status Duplicate Name |
|
Status Invalid Name |
|
Status Invalid Parameter |
|
Status Invalid Service Account |
|
Status Service Exists |
|
Service Already Paused |
Remarks
If you start a stopped service that depends on another service, then both services are started. When a service is started with this method, any dependent services are not automatically started. You must use the association class Win32_DependentService and the Associators Of query to locate the dependents and start them separately.
Examples
For script code examples, see WMI Tasks for Scripts and Applications and the TechNet ScriptCenter Script Repository.
For C++ code examples, see WMI C++ Application Examples.
The following VBScript code example, NetDDE, is dependent on the NetDDEDSDM service. The script locates the class on which NetDDE depends and starts it, which does not automatically start NetDDE.
strComputer = "." Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" & strComputer & _ "\root\cimv2") ' Stop NetDDE if it is running Set objNetDDEService = _ objWMIService.Get("Win32_Service.Name='NetDDE'") Return = objNetDDEService.StopService() ' NetDDE is in the dependent role to another service Set colServiceList = objWMIService.ExecQuery("Associators of " _ & "{Win32_Service.Name='NetDDE'} Where " _ & "AssocClass=Win32_DependentService " & "Role=Dependent" ) ' start the service on which NetDDE is dependent For Each objService in colServiceList WScript.Echo "Starting " & objService.Name Return = objService.StartService() If Return = 0 Then WScript.Echo "Parent service " & objService.Name & _ " started successfully" Else WScript.Echo "Parent service " & objService.Name & _ " did not start. Return = " & Return End If Next ' NetDDE is still stopped Set objNetDDEService = _ objWMIService.Get("Win32_Service.Name='NetDDE'") WScript.Echo "Dependent NetDDE service is " & _ objNetDDEService.State
Requirements
|
Minimum supported client | Windows 2000 Professional |
|---|---|
|
Minimum supported server | Windows 2000 Server |
|
Namespace |
\root\CIMV2 |
|
MOF |
|
|
DLL |
|
See also
Send comments about this topic to Microsoft
Build date: 3/9/2012