Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Command Line Windows Services Manager

0.00/5 (No votes)
23 Jun 2003 1  
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