Click here to Skip to main content
11,414,234 members (71,517 online)
Click here to Skip to main content

Command Line Windows Services Manager

, 23 Jun 2003
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

Comments and Discussions

 
Generalnice job! Pin
philippdev, 21-Jun-13 23:22
memberphilippdev21-Jun-13 23:22 
GeneralWindows Service Manager Tray software, similar to this Pin
logicalregistrator, 1-Jul-09 3:05
memberlogicalregistrator1-Jul-09 3:05 
GeneralThanks Pin
Youzh, 7-Oct-08 2:43
memberYouzh7-Oct-08 2:43 
GeneralWindows service is not starting through remote machine in WORKGROUP Pin
Ananthraj, 28-May-07 16:10
memberAnanthraj28-May-07 16:10 
I am not able to start the service or get the status of the service on another machine which is in WORKGROUP network.

It works when the machines are in DOMAIN. Please help

mandar
GeneralRe: Windows service is not starting through remote machine in WORKGROUP [modified] Pin
alhambra-eidos, 29-Sep-08 22:02
memberalhambra-eidos29-Sep-08 22:02 
Generalit didnot work in my computer too Pin
vuchihieu, 2-Feb-07 2:29
membervuchihieu2-Feb-07 2:29 
QuestionIssue! Pin
moin021, 7-Nov-06 0:56
membermoin0217-Nov-06 0:56 
GeneralIntegrate window service with any web application Pin
Faheem Habib, 25-Aug-05 2:35
memberFaheem Habib25-Aug-05 2:35 
GeneralWORK!! but if Password containd blank space does not work...WHY!!! Pin
Brian Paul, 28-Jun-05 9:06
memberBrian Paul28-Jun-05 9:06 
GeneralCould this be implemented for WindowsCE Pin
mkearl, 5-Apr-05 0:00
membermkearl5-Apr-05 0:00 
QuestionExecuteCommand? Pin
Kocha, 16-Jun-04 22:09
memberKocha16-Jun-04 22:09 
GeneralThis doesn't work for me Pin
George Nunn, 3-May-04 2:57
sussGeorge Nunn3-May-04 2:57 
Generalsimply excellent work!! Pin
M.Shoaib Khan, 19-Jul-03 6:26
memberM.Shoaib Khan19-Jul-03 6:26 
GeneralRe: simply excellent work!! Pin
Skorunka, 2-Dec-08 11:14
memberSkorunka2-Dec-08 11:14 
GeneralStartup Types Pin
Heath Stewart, 24-Jun-03 6:14
editorHeath Stewart24-Jun-03 6:14 
GeneralExcellent Work Pin
Jason Gerard, 3-Jun-03 4:08
memberJason Gerard3-Jun-03 4: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
Web02 | 2.8.150427.2 | Last Updated 24 Jun 2003
Article Copyright 2003 by Haidong Chen
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid