-1

I am trying to write a code which would run a command from command prompt and show the output of the command. Basically the command would check the status of a window service if its running or stopped. Here is the code:

Process process = new System.Diagnostics.Process();
ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();

startInfo.FileName = @"cmd.exe";
startInfo.Arguments = "sc query \"My Service\" | findstr STATE";
process.StartInfo = startInfo;
process.StartInfo.RedirectStandardOutput = true;
process.Start();

while (!process.StandardOutput.EndOfStream)
{
    string line = process.StandardOutput.ReadLine();
}

If I run the command on command line:

sc query "My Service" | findstr STATE

This returns me:

STATE              : 1  STOPPED

But if I run my c# code above, nothing happens and after some time my browser throws error:

HTTP Error 502.3 - Bad Gateway The specified CGI application encountered an error and the server terminated the process.

Not sure what else is missing here.

Rand Random
  • 7,300
  • 10
  • 40
  • 88
kaka1234
  • 770
  • 3
  • 9
  • 30
  • 1
    Why are you executing `cmd.exe` rather than `sc`? – mjwills Apr 17 '18 at 14:08
  • 1
    Are you trying to do this within a web app? You may have authorisation problems. Typically for a 500 status there will be an error report (including far more detail) in the application event log. – Richard Apr 17 '18 at 14:14
  • @mjwills see below response why I am not using SC – kaka1234 Apr 17 '18 at 14:15
  • Remember that you get a localized response here. STATE could be spelled differently – Steve Apr 17 '18 at 14:15
  • @Richard yes its a mvc web application. – kaka1234 Apr 17 '18 at 14:16
  • That is confusing to me @kaka1234. If sc isn't running successfully, why would you running it **via cmd** help? What account is your app pool running as? Does it have rights to use `ServiceController`? – mjwills Apr 17 '18 at 14:16
  • @mjwills when looking out solutions for the SC error most of the solution mentioned the need to have specific rights to access service controller which I am struggling to resolve. So while looking for alternative solution I saw accessing via cmd and wanted to try it out incase this works. – kaka1234 Apr 17 '18 at 14:20
  • If there is a permissions / rights issue, then accessing the tool / functionality through different avenues won't help. You need to address the underlying permission / rights issue. – mjwills Apr 17 '18 at 14:21
  • @mjwills app pool is running under a service account. I have given local admin and remote access rights to this account as well – kaka1234 Apr 17 '18 at 14:21
  • Access – authorisation – is controlled by the user's account's settings. All processes executed with that user's identity have the same access. (UAC adds some complexity, but ultimately just creates elevated and non-elevated cases). – Richard Apr 17 '18 at 14:22
  • You can find another possibility in this QA https://stackoverflow.com/questions/6711726/c-sharp-query-windows-service (Look for WMI example) – Steve Apr 17 '18 at 14:24
  • @Steve I just tried with the Management class also and that also throws the same access denied error. Looks like permission issues need to be sorted for both of these solutions. My only issue is I am not aware which permission to give to the user. I have raised this issue with our windows admin and they are also unsure about which specific permission if for service controller – kaka1234 Apr 17 '18 at 15:16

1 Answers1

4

Why use cmd when it can be done with simple codes ?

 ServiceController[] services = ServiceController.GetServices();
 foreach(ServiceController service in services)
 {
  If (service.ServiceName == "name here")
  { 
Console.WriteLine(service.ServiceName+"=="+ service.Status);
 }}

Or a better version :

  try
{
using( ServiceController sc = new ServiceController(name here) )
{
    return sc.Status == ServiceControllerStatus.Running;
}
}
catch( ArgumentException ) { return false; }
catch( Win32Exception ) { return false; }

Another way(OP finally mentioned he's doing it in a web app :)) :

  using System.ServiceProcess;

  ServiceController sc = new ServiceController(SERVICENAME);

  switch (sc.Status)
  {
    case ServiceControllerStatus.Running:
    return "Running";
  }
Software Dev
  • 5,368
  • 5
  • 22
  • 45
  • I have been struggling to get the "Service Controller" worked. I first tried with service controller class. But it keeps on throwing error: "Cannot open Service Control Manager on computer ''. This operation might require other privileges." on our webserver. I have added the account which is running to the admin group and have given remote access permissions but nothing works. So I thought of looking into other possibilities of checking the service status. – kaka1234 Apr 17 '18 at 14:13
  • and one more thing , make sure to include that you are doing this in `web app` :( – Software Dev Apr 17 '18 at 14:16
  • take a look at my answer now :) – Software Dev Apr 17 '18 at 14:19
  • the issue is as soon as it tries to access service controller it throws the access denied error. So it does not matter if I pass service name or not. I will update my ques including web app – kaka1234 Apr 17 '18 at 14:23