programming4us
         
 
 
Windows Server

Windows Home Server 2011 : Controlling Services (part 2) - Controlling Services at the Command Prompt, Controlling Services with a Script

- How To Install Windows Server 2012 On VirtualBox
- How To Bypass Torrent Connection Blocking By Your ISP
- How To Install Actual Facebook App On Kindle Fire
7/16/2013 7:35:06 PM

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

ServiceThe 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 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 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).

Other -----------------
- Windows Home Server 2011 : Configuring the Microsoft Management Console (part 2) - Creating a Custom Taskpad View, Controlling Snap-Ins with Group Policies
- Windows Home Server 2011 : Configuring the Microsoft Management Console (part 1) - Adding a Snap-In
- Windows Server 2012 : Monitoring, Tuning, and Troubleshooting Hyper-V - Using VM Monitoring
- Windows Server 2012 : Monitoring, Tuning, and Troubleshooting Hyper-V - Using Perfmon for logged monitoring
- Windows Server 2012 : Monitoring, Tuning, and Troubleshooting Hyper-V - Using real-time monitoring tools
- Windows Server 2008 : Licensing and Activation - Forcing Registration of KMS Server SRV Records, Manually Creating an SRV Record for KMS
- Windows Server 2008 : Licensing and Activation - Managing Activation Tasks with slmgr
- Windows Server 2008 : Migrating Printers with printbrm, Controlling the Print Queue with prnqctl.vbs
- Windows Server 2008 : Publishing Printers to Active Directory with pubprn.vbs
- Windows Small Business Server 2011 : Deploying Network Printers (part 4) - Sharing a Printer
- Windows Small Business Server 2011 : Deploying Network Printers (part 3) - Creating a DHCP Reservation for a Printer
- Windows Small Business Server 2011 : Deploying Network Printers (part 2) - Installing a Network-Attached Printer, Installing a Network Printer Manually
- Windows Small Business Server 2011 : Deploying Network Printers (part 1) - Creating a Printer - Installing a Local Printer Manually
- Windows Small Business Server 2011 : Sharing Printers- Understanding Windows Printing
- Windows Home Server 2011 : Getting More Out of Control Panel (part 2) - Alternative Methods for Opening Control Panel Icons
- Windows Home Server 2011 : Getting More Out of Control Panel (part 1) - Understanding Control Panel Files
- Windows Home Server 2011 : Using the Local Group Policy Editor (part 3) - Increasing the Size of the Recent Documents List, Enabling the Shutdown Event Tracker
- Windows Home Server 2011 : Using the Local Group Policy Editor (part 2) - Customizing the Places Bar
- Windows Home Server 2011 : Using the Local Group Policy Editor (part 1) - Working with Group Policies, Customizing the Windows Security Screen
- Windows Server 2012 : Backup and Recovery (part 8) - Backing up and restoring Active Directory
 
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
programming4us programming4us
Video Tutorail Microsoft Access Microsoft Excel Microsoft OneNote Microsoft PowerPoint Microsoft Project Microsoft Visio Microsoft Word Active Directory Biztalk Exchange Server Microsoft LynC Server Microsoft Dynamic Sharepoint Sql Server Windows Server 2008 Windows Server 2012 Windows 7 Windows 8 Adobe Indesign Adobe Flash Professional Dreamweaver Adobe Illustrator Adobe After Effects Adobe Photoshop Adobe Fireworks Adobe Flash Catalyst Corel Painter X CorelDRAW X5 CorelDraw 10 QuarkXPress 8 windows Phone 7 windows Phone 8 BlackBerry Android Ipad Iphone iOS
Celebrity Style, Fashion Trends, Beauty and Makeup Tips.