WMI Tasks: Services
WMI tasks for services obtain information about services, including dependent or antecedent services. For other examples, see the TechNet ScriptCenter at http://www.microsoft.com/technet.
The script examples shown in this topic obtain data only from the local computer. For more information about how to use the script to obtain data from remote computers, see Connecting to WMI on a Remote Computer.
The following procedure describes how to run a script.
To run a script
- Copy the code and save it in a file with a .vbs extension. Ensure that your text editor does not add a .txt extension to the file.
- Open a command prompt window and navigate to the directory where you saved the file.
- Type cscript scriptfile.vbs at the command prompt.
Note By default, cscript displays the output of a script in the command prompt window. Because WMI scripts can produce large amounts of output, you might want to redirect the output to a file. Type cscript scriptfile.vbs > outfile.txt at the command prompt to redirect the output of the filename.vbs script to outfile.txt.
The following table lists script examples that can be used to obtain various types of data from the local computer.
| How do I... | WMI classes or methods |
|---|---|
| ...determine which services are running and which ones are not? | Use the Win32_Service class to check the state of all of the services. The state property lets you know if a service is stopped or running.
strComputer = "." Set objWMIService = GetObject( _ "winmgmts:\\" & strComputer & "\root\CIMV2") Set colItems = objWMIService.ExecQuery( _ "SELECT * FROM Win32_Service",,48) For Each objItem in colItems Wscript.Echo "Service Name: " & objItem.Name & VBNewLine _ & "State: " & objItem.State Next |
| ...stop Power Users from starting certain services? | Use the Win32_Service class and the ChangeStartMode method to set the StartMode property to Disabled. Disabled services cannot be started, and, by default, Power Users cannot change the start mode of a service.
strComputer = "." Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" & _ strComputer & "\root\cimv2") Set colServiceList = objWMIService.ExecQuery _ ("Select * from Win32_Service where StartMode = 'Manual'") For Each objService in colServiceList errReturnCode = objService.Change( , , , , "Disabled") WScript.Echo "Changed manual service to disabled: " & objService.Name Next |
| ...start and stop services? | Use the Win32_Service class and the StopService and StartService methods.
strComputer = "." Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" _ & strComputer & "\root\cimv2") Set colListOfServices = objWMIService.ExecQuery _ ("Select * from Win32_Service Where Name ='Alerter'") For Each objService in colListOfServices objService.StartService() Wscript.Echo "Started Alerter service" Next |
| ...change service account passwords using a script? | Use the Win32_Service class and the Change method.
strComputer = "." Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" _ & strComputer & "\root\cimv2") Set colServiceList = objWMIService.ExecQuery _ ("Select * from Win32_Service") For Each objservice in colServiceList If objService.StartName = ".\netsvc" Then errReturn = objService.Change( , , , , , , , "password") End If Next |
| ..determine which services I can stop? | Use the Win32_Service class, and check the value of the AcceptStop property.
strComputer = "." Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" _ & strComputer & "\root\cimv2") Set colServices = objWMIService.ExecQuery _ ("Select * from Win32_Service Where AcceptStop = True") For Each objService in colServices Wscript.Echo objService.DisplayName Next |
| ...find the services that must be running before I can start the DHCP service? | Query for ASSOCIATORS OF the Win32_Service class named "DHCP" that are in the Win32_DependentService class and have "Dependent" in the Role property. Role means the role of the DHCP service: in this case, it is dependent on the other services that are being started.
strComputer = "." Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\"_ & strComputer & "\root\cimv2") Set colServiceList = _ objWMIService.ExecQuery("Associators Of " _ & "{Win32_Service.Name='dhcp'} Where " _ & "AssocClass=Win32_DependentService " _ & "Role=Dependent") For Each objService in colServiceList Wscript.Echo objService.DisplayName Next |
| ...find the services that require the WMI service (Winmgmt) service to be running before they can start? | Query for ASSOCIATORS OF the Win32_Service class named "DHCP" that are in the Win32_DependentService class and have "Antecendent" in the Role property. Role means the role of the rasman service: in this case, it is antecedent to—must be started before—the dependent services.
strComputer = "." Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\"_ & strComputer & "\root\cimv2") Set colServiceList = _ objWMIService.ExecQuery("Associators of " _ & "{Win32_Service.Name='winmgmt'} Where " _ & "AssocClass=Win32_DependentService " _ & "Role=Antecedent" ) For Each objService in colServiceList Wscript.Echo "Name: " & objService.Name _ & VBTab & "Display Name: " & objService.DisplayName Next |
Related topics
Send comments about this topic to Microsoft
Build date: 11/19/2012