2. Controlling Services at the Command Prompt
If you regularly stop and start certain services,
loading the Services snap-in and manually stopping and then restarting
each service can be time-consuming. A better method is to take advantage
of the NET STOP and NET START command-line tools,
which enable you to stop and start any service that isn’t disabled. If a
service can be paused and restarted, you can also use the NET PAUSE and NET CONTINUE commands to control the service. Each of these commands uses the same syntax:
NET STOP Service
NET START Service
NET PAUSE Service
NET CONTINUE Service
Service | The
name of the service you want to control. Use the same value that
appears in the Name column of the Services snap-in. If the name contains
a space, surround the name with quotation marks. |
Here are some examples:
net start Telephony
net stop "Themes"
net pause "World Wide Web Publishing Service"
net continue "Windows Management Instrumentation"
You can combine multiple commands in a batch file to easily control several services with a single task.
Tip
To see a list of the currently running services, open a command-line session and enter the command net start without the Service parameter.
3. Controlling Services with a Script
If you want to automate service control, but you want
to also control the startup type, you need to go beyond the command
line and create scripts that manage your services. WMI has a class
called Win32_Service that represents a Windows service. You can
return an instance of this class to work with a specific service on
Windows Home Server. After you have the service object, you can query
its current status with the State property; determine whether the service is running with the Started property; and return the service’s startup type with the StartMode property. You can also change the service state using the StartService, StopService, PauseService, and ResumeService methods.
Listing 1 presents a script that uses most of these properties and methods.
Listing 1. A WMI Script That Toggles a Service’s State Between Started and Stopped
Option Explicit
Dim strComputer, strServiceName, intReturn
Dim objWMI, objServices, objService
'
' Get the WMI service
'
strComputer = "localhost"
Set objWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
strComputer & "\root\cimv2")
'
' Specify the service name
'
strServiceName = "Distributed File System"
'
' Get the service instance
'
Set objServices = objWMI.ExecQuery("SELECT * FROM Win32_Service " & _
"WHERE DisplayName = '" & strServiceName & "'")
For Each objService In objServices
'
' Save the service name
'
strServiceName = objService.DisplayName
'
' Is the service started?
'
If objService.Started Then
'
' Can it be stopped?
'
If objService.AcceptStop Then
'
' Attempt to stop the service
'
intReturn = objService.StopService
'
' Check the return value
'
If intReturn <> 0 Then
'
' Display the error message
'
WScript.Echo "ERROR: The " & strServiceName & " service " & _
"failed to stop. The return code is " & intReturn
Else
'
' Display the current state
'
WScript.Echo "The " & strServiceName & " service is now " & _
objService.State
End If
Else
'
' Display the error message
'
WScript.Echo "ERROR: The " & strServiceName & " service " & _
"cannot be stopped."
End If
Else
'
' Attempt to start the service
'
intReturn = objService.StartService
'
' Check the return value
'
If intReturn <> 0 Then
'
' Display the error message
'
WScript.Echo "ERROR: The " & strServiceName & " service " & _
"failed to start. The return code is " & intReturn
Else
'
' Display the current state
'
WScript.Echo "The " & strServiceName & " service is now " & _
objService.State
End If
End If
Next
'
' Release the objects
'
Set objWMI = Nothing
Set objServices = Nothing
Set objService = Nothing
|
This script gets the WMI service object and uses its ExecQuery method to return an instance of the Win32_Service class by using the WHERE clause to look for a specific service name. That name was earlier stored in the strServiceName variable. In the For Each...Next loop, the script first checks to see if the service is currently started by checking its Started property:
If the Started property returns True, the service is running, so we want to stop it. The script then checks the service’s AcceptStop property, which returns False for essential Windows Home Server services that can’t be stopped. In this case, the script returns an error message. If AcceptStop returns True, the script attempts to stop the service by running the StopService method.
If the Started property returns False, the service is stopped, so we want to start it. The script attempts to start the service by running the StartService method.
The StopService and StartService methods generate the return codes shown in Table 1.
Table 1. Return Codes Generated by the StartService and StopService Methods
Return Code | Description |
---|
0 | Success |
1 | Not supported |
2 | Access denied |
3 | Dependent services running |
4 | Invalid service control |
5 | Service cannot accept control |
6 | Service not active |
7 | Service request timeout |
8 | Unknown failure |
9 | Path not found |
10 | Service already stopped |
11 | Service database locked |
12 | Service dependency deleted |
13 | Service dependency failure |
14 | Service disabled |
15 | Service logon failed |
16 | Service marked for deletion |
17 | Service no thread |
18 | Status circular dependency |
19 | Status—duplicate name |
20 | Status—invalid name |
21 | Status—invalid parameter |
22 | Status—invalid service account |
23 | Status—service exists |
24 | Service already paused |
For both the StopService and StartService methods, the script stores the return code in the intReturn
variable and then checks to see if it’s a number other than 0. If it
is, the script displays an error message that includes the return code;
otherwise, the script displays the new state of the service (as given by
the State property).