Programming4us
         
 
 
Windows

Windows 7 : Controlling Services with a Script

12/1/2010 7:57:52 PM
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. Windows Management Instrumentation (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 7. 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.

Note

You can find this script on my website at www.mcfedries.com/Windows7Unleashed.


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 = "Remote Registry"
'
' 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 whether 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 7 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 2.

Table 2. Return Codes Generated by the StartService and StopService Methods
Return CodeDescription
0Success
1Not supported
2Access denied
3Dependent services running
4Invalid service control
5Service cannot accept control
6Service not active
7Service request timeout
8Unknown failure
9Path not found
10Service already stopped
11Service database locked
12Service dependency deleted
13Service dependency failure
14Service disabled
15Service logon failed
16Service marked for deletion
17Service no thread
18Status circular dependency
19Status — duplicate name
20Status — invalid name
21Status — invalid parameter
22Status — invalid service account
23Status — service exists
24Service already paused

For both the StopService and StartService methods, the script stores the return code in the intReturn variable and then checks to see whether it’s a number other than 0. If so, 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).

Other -----------------
- Windows 7 : Controlling Services at the Command Prompt
- Windows 7 : Controlling Services with the Services Snap-In
- Windows Azure : Access Control Service Usage Scenarios (part 3)
- Windows Azure : Access Control Service Usage Scenarios (part 2)
- Windows Azure : Access Control Service Usage Scenarios (part 1)
- Windows Azure : Access Control Service - Claims-Based Identity Model
- Windows Azure : Access Control Service - Concepts and Terminology
- Windows 7 : Configuring the MMC - Creating a Custom Taskpad View
- Windows 7 : Configuring the MMC - Controlling Snap-Ins with Group Policies
- Windows 7 : Configuring the MMC - Adding a Snap-In
- Configuring the Microsoft Management Console : Reviewing the Windows 7 Snap-Ins
- Windows 7 : Enabling the Shutdown Event Tracker
- Windows 7 : Increasing the Size of the Recent Documents List
- Windows 7 : Customizing the Places Bar
- Windows 7 : Customizing the Windows Security Window
- Windows 7 : Working with Group Policies
- Policing Windows 7 with Group Policies
- Windows Azure Storage : Queue Scenarios
- Windows Azure Storage : Message Operations (part 2) - Get Messages
- Windows Azure Storage : Message Operations (part 1) - Put Message
 
 
Most View
- Performing SharePoint 2010 Installations (part 1) - SharePoint 2010 Standalone Installation
- Microsoft Exchange Server 2003: Configuring Recipient Objects (part 2) - Managing Mailboxes
- Windows7: Managing Network Connections (part 5) - Using a Network Connection to Wake Up a Sleeping Computer
- Windows Server 2008 : Schedule Regular Full Server Backups of a Domain Controller by Using the Windows Interface
- Windows 7: Managing Wireless Network Connections (part 3) - Reordering Wireless Connections
- Microsoft SQL Server 2008 R2 : Monitoring Replication (part 1) - Replication Monitoring SQL Statements
- Programming Windows Phone 7 : The Phone’s Photo Library
- SQL Server Programmability Objects
- Windows Server 2008 : Configuring FTP (part 12) - Managing FTP Site Settings
- SharePoint 2010 : Authoring Pages - Create a New Page (part 1)
Top 10
- Implementing Edge Services for an Exchange Server 2007 Environment : Utilizing the Basic Sender and Recipient Connection Filters (part 3) - Configuring Recipient Filtering
- Implementing Edge Services for an Exchange Server 2007 Environment : Utilizing the Basic Sender and Recipient Connection Filters (part 2)
- Implementing Edge Services for an Exchange Server 2007 Environment : Utilizing the Basic Sender and Recipient Connection Filters (part 1)
- Implementing Edge Services for an Exchange Server 2007 Environment : Installing and Configuring the Edge Transport Server Components
- What's New in SharePoint 2013 (part 7) - BCS
- What's New in SharePoint 2013 (part 6) - SEARCH
- What's New in SharePoint 2013 (part 6) - WEB CONTENT MANAGEMENT
- What's New in SharePoint 2013 (part 5) - ENTERPRISE CONTENT MANAGEMENT
- What's New in SharePoint 2013 (part 4) - WORKFLOWS
- What's New in SharePoint 2013 (part 3) - REMOTE EVENTS