Thursday, August 30, 2012

c# - List all Windows Services on a server/machine

I have a few c# windows services that have been hosted on multiple servers.I wanted to check the status of those services without having to log on to the server.The below code will list all the services and its status on the server.

using System.ServiceProcess;


try
{
       ServiceController[] oaServiceController = ServiceController.GetServices("myservername");
       foreach (ServiceController sc in oaServiceController)
             Console.WriteLine("Service Name: " + sc.DisplayName + " Service Status:" + sc.Status.ToString());
}
catch (Exception ex)
{
      Console.WriteLine(ex.Message);
}




No comments: