0

I was wondering if it's possible to tell if a remote server is up or down using C#? First I have a method to shut down the server:

private static void RestartSecondServer()
{
    Console.WriteLine("Computer details retrieved using Windows Management Instrumentation (WMI)");
    //Connect to the remote computer
    ConnectionOptions co = new ConnectionOptions();
    co.Username = @"***********";
    co.Password = "*********";
    ManagementScope ms = new ManagementScope("\\\\******\\root\\cimv2", co);

    //Query remote computer across the connection
    ObjectQuery oq = new System.Management.ObjectQuery("SELECT * FROM Win32_OperatingSystem");
    ManagementObjectSearcher query1 = new ManagementObjectSearcher(ms, oq);
    ManagementObjectCollection queryCollection1 = query1.Get();
    foreach (ManagementObject mo in queryCollection1)
    {
        string[] ss = {""};
        mo.InvokeMethod("Reboot", ss);
        Console.WriteLine(mo.ToString());
        Console.WriteLine("Reboot complete");
        //Process.Start("shutdown", "/r /t 0");
    }
}

Now I want to create a method to see if this remote server which is in the process of being restarted is down. I tried a generic pinging method but it was returning true/success.

Can a server be successfully pinged if it's in the process of being restarted? Is it possible to tell if the server is being restarted?

Cy Rossignol
  • 16,216
  • 4
  • 57
  • 83
sharsart
  • 228
  • 5
  • 14
  • Hmmm, how about listening for event log messages, like when shutting down or starting up? Ping should respond (if enabled) until the machine shuts down (generally) and start responding again as the machine starts back up (and network stack becomes available). – kvr Oct 30 '17 at 20:32
  • yes this is one possible solution. I will try it tomorrow. I hope there are more possible solutions – sharsart Oct 30 '17 at 20:43
  • Pretty sure there are more possibilities out there – kvr Oct 30 '17 at 20:44
  • Can you think of anything else? – sharsart Oct 30 '17 at 20:53
  • If you have a process/service running locally on that machine you can listen for shutdown related events, end session events etc. – kvr Oct 30 '17 at 20:58
  • you can also try looking at "the last boot up time" WMI property (and other potential WMI properties). Might be able to deduce that a machine was started somewhat within a given time window and that may be acceptable to determine if started recently. See: https://stackoverflow.com/questions/10853985/programmatically-getting-system-boot-up-time-in-c-windows – kvr Oct 30 '17 at 21:10
  • Thanks KVR, I will look into it right now :) – sharsart Oct 31 '17 at 12:21
  • This is proving to be really tricky. LastBootUptime is not a good solution since it doesn't give real time status. Right now I am using the service controller class. The question is as the server goes into refresh which of the automatic/auto start services goes down first?? Does anyone have any ideas? – sharsart Nov 01 '17 at 15:22

0 Answers0