Click here to Skip to main content
11,641,971 members (59,691 online)
Click here to Skip to main content

Command Line Windows Services Manager

, 23 Jun 2003 377.3K 6.1K 94
Rate this:
Please Sign up or sign in to vote.
How to manipulate Windows services on local or remote computers.

Introduction

The .NET Framework provides easy access to the service control manager for manipulating Windows services. In this article, I will show you the command line service manager tool that I developed using these features.

Background

The topic of managing Windows services is covered on MSDN at Monitoring Windows Services. It is also discussed on CodeProject in Agus Kurniawan's article Windows Service Management.

Mine differs from Agus's in the following areas:

  1. Agus provides a UI solution, similar to the service control panel, while mine uses a command line.
  2. Agus covered the basics of service management including starting and stopping services, while mine tries to cover areas like starting, stopping, pausing, continuing and restarting services.
  3. Mine also tries to address the timeout and impersonation issue when managing Windows services.

Basics in Windows service management

In .NET, Windows service management is achieved by the System.ServiceProcess.ServiceController. The following code demonstrates how to list all the services on the machine srv2:

using System.ServiceProcess;

public class SvcMgr {

   public static void Main( string[] args ) {
       ServiceController[] services = 
                          ServiceController.GetServices( "srv2" );
       foreach( ServiceController service in services ) {
           Console.WriteLine( string.Format( "{0} [ {1} ]",
               service.ServiceName, service.Status.ToString() ) );
       }
   }
}

How to handle timeouts

Timeouts are handled by calling the WaitForStatus method on the service controller. The following code demonstrates how to start the "IISADMIN" service on srv2 using 10 second timeout:

//...

ServiceController service = 
    new ServiceController( "IISADMIN", "srv2" );
service.Start();

TimeSpan t = TimeSpan.FromSeconds( "10" );
service.WaitForStatus( 
     ServiceControllerStatus.Running, t );

//...

When a timeout happens, a System.ServiceProcess.TimeoutException will be thrown. The command line tool that I developed handles this in the following way:

How to handle impersonation when managing services

The account that you use to manage Windows services needs the privilege to access the service control manager on the target machine; otherwise, you'll get a System.InvalidOperationException wrapped with a System.SystemException, stating, "Cannot open Service Control Manager on computer 'srv3'. This operation might require other privileges."

Note: This exception represents the fact that the service control manager on the target machine is not accessible. This could mean the current account that you use does not hold enough privileges or simply the machine is offline.

This is where impersonation comes into picture. By impersonating a different Windows logon which has the privilege to access service control manager, we can continue without having to logon as a different user. See how impersonation works in this command line service manager:

Note: The account that you are trying to impersonate has to be both in the local system and in the target machine, otherwise the program won't know whom to impersonate. To see how impersonation was implemented in this tool, please see the file SvcMgr.cs in the source code.

For more information on what impersonation is and how impersonation works, please refer to Implementing Impersonation in an ASP.NET Application on MSDN.

Putting it all together

This Command Line Service Manager utility puts all of the above topics together and is a useful tool for managing Windows services on a local or remote machine(s).

Things that are missing

This command line tool does not have the capability to change the default startup type of a service. If somebody is interested in looking further, I suggest this is the direction to dive in, although it seems ServiceController does not provide such functionalities.

References

History

  • 06/24/2003
    • Document modified.
  • 05/24/2003
    • Document written.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Haidong Chen
Web Developer
United States United States
I'm an IT Professional from New York.

Personal website - http://www.chenhaidong.com

You may also be interested in...

Comments and Discussions

 
Generalnice job! Pin
philippdev21-Jun-13 22:22
memberphilippdev21-Jun-13 22:22 
GeneralWindows Service Manager Tray software, similar to this Pin
logicalregistrator1-Jul-09 2:05
memberlogicalregistrator1-Jul-09 2:05 
GeneralThanks Pin
Youzh7-Oct-08 1:43
memberYouzh7-Oct-08 1:43 
GeneralWindows service is not starting through remote machine in WORKGROUP Pin
Ananthraj28-May-07 15:10
memberAnanthraj28-May-07 15:10 
GeneralRe: Windows service is not starting through remote machine in WORKGROUP [modified] Pin
alhambra-eidos29-Sep-08 21:02
memberalhambra-eidos29-Sep-08 21:02 
Generalit didnot work in my computer too Pin
vuchihieu2-Feb-07 1:29
membervuchihieu2-Feb-07 1:29 
QuestionIssue! Pin
moin0216-Nov-06 23:56
membermoin0216-Nov-06 23:56 
GeneralIntegrate window service with any web application Pin
Faheem Habib25-Aug-05 1:35
memberFaheem Habib25-Aug-05 1:35 
GeneralWORK!! but if Password containd blank space does not work...WHY!!! Pin
Brian Paul28-Jun-05 8:06
memberBrian Paul28-Jun-05 8:06 
GeneralCould this be implemented for WindowsCE Pin
mkearl4-Apr-05 23:00
membermkearl4-Apr-05 23:00 
QuestionExecuteCommand? Pin
Kocha16-Jun-04 21:09
memberKocha16-Jun-04 21:09 
GeneralThis doesn't work for me Pin
George Nunn3-May-04 1:57
sussGeorge Nunn3-May-04 1:57 
Generalsimply excellent work!! Pin
M.Shoaib Khan19-Jul-03 5:26
memberM.Shoaib Khan19-Jul-03 5:26 
GeneralRe: simply excellent work!! Pin
Skorunka2-Dec-08 10:14
memberSkorunka2-Dec-08 10:14 
GeneralStartup Types Pin
Heath Stewart24-Jun-03 5:14
editorHeath Stewart24-Jun-03 5:14 
Because the startup type isn't usually changed during execution of an app that uses the service, it is not part of the SCM (at least the .NET class). You can change it a number of ways, such as using the ServiceInstaller and ServiceProcessInstaller during installation (See the InstallerAttribute) to set the Startup type, or to P/Invoke several methods from the Services API and use ChangeServiceConfig to change the service type, or to modify the values in the registry (not recommended) in HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\ServiceName\Start (REG_DWORD) to 2 (Automatic), 3 (Manual), or 4 (Disabled).


Reminiscent of my younger years...
10 LOAD "SCISSORS"
20 RUN

GeneralExcellent Work Pin
Jason Gerard3-Jun-03 3:08
memberJason Gerard3-Jun-03 3:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150731.1 | Last Updated 24 Jun 2003
Article Copyright 2003 by Haidong Chen
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid