Click here to Skip to main content
15,881,803 members
Articles / Programming Languages / C#
Article

Command Line Windows Services Manager

Rate me:
Please Sign up or sign in to vote.
4.69/5 (32 votes)
23 Jun 20033 min read 242.2K   8.6K   97   16
How to manipulate Windows services on local or remote computers.

Image 1

Image 2

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:

C#
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:

C#
//...

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:

Image 3

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."

Image 4

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:

Image 5

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


Written By
Web Developer
United States United States
I'm an IT Professional from New York.

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

Comments and Discussions

 
QuestionExecuteCommand? Pin
Kocha16-Jun-04 21:09
Kocha16-Jun-04 21:09 
GeneralThis doesn't work for me Pin
George Nunn3-May-04 1:57
George Nunn3-May-04 1:57 
Generalsimply excellent work!! Pin
abc87619-Jul-03 5:26
abc87619-Jul-03 5:26 
GeneralRe: simply excellent work!! Pin
Skorunka2-Dec-08 10:14
Skorunka2-Dec-08 10:14 
GeneralStartup Types Pin
Heath Stewart24-Jun-03 5:14
protectorHeath Stewart24-Jun-03 5:14 
GeneralExcellent Work Pin
Jason Gerard3-Jun-03 3:08
Jason Gerard3-Jun-03 3:08 

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

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