C
C#14mo ago
Philogogus

❔ Returning a bool value when searching for the status of a service running on a remote server

Howdy, I am trying to return the status of a remote service. I can get it to work fine with something like Print Spooler but when I try to return the status of the service I actually WANT... it doesn't work.
public static bool IsServiceRunning(string serviceName, string remoteMachineName)
{
ServiceController sc = new ServiceController(serviceName, remoteMachineName);

try
{
ServiceControllerStatus status = sc.Status;
return (status == ServiceControllerStatus.Running);
}
catch (InvalidOperationException)
{

return false;
}
}
public static bool IsServiceRunning(string serviceName, string remoteMachineName)
{
ServiceController sc = new ServiceController(serviceName, remoteMachineName);

try
{
ServiceControllerStatus status = sc.Status;
return (status == ServiceControllerStatus.Running);
}
catch (InvalidOperationException)
{

return false;
}
}
Got this off of StackOverflow, and it seems to work because when I check for Print Spooler it returns YES.
try
{
bool serverup = IsServiceRunning(".BLARG Server", "TESTSERVER-T06");

if (serverup)
{
this.EventLog.WriteEntry("It's up!");
}
else
{
this.EventLog.WriteEntry("It's down!");
}
}
catch (Exception ex)
{
this.EventLog.WriteEntry(ex.ToString());
}
try
{
bool serverup = IsServiceRunning(".BLARG Server", "TESTSERVER-T06");

if (serverup)
{
this.EventLog.WriteEntry("It's up!");
}
else
{
this.EventLog.WriteEntry("It's down!");
}
}
catch (Exception ex)
{
this.EventLog.WriteEntry(ex.ToString());
}
3 Replies
fæ
14mo ago
unrelated but if you add "cs" (short for csharp) at the start of your code block it will make it highlighted (for easier visibility) like this
No description
Philogogus
PhilogogusOP14mo ago
Fixed. Looked up the Markup for it and it wasn't clear to do that...
Accord
Accord14mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server