From the command line, how can I tell if a reboot is pending?

Todd Chester 706 Reputation points
2021-12-27T03:58:23.49+00:00

Hi All,

From the command line, how can I tell if a reboot is pending or delayed?

Many thanks,
-T

Windows for business | Windows Client for IT Pros | Devices and deployment | Configure application groups
Windows for business | Windows Server | User experience | Other
Windows for business | Windows Client for IT Pros | User experience | Other
0 comments No comments
{count} votes

Accepted answer
  1. Sean Liming 4,766 Reputation points Volunteer Moderator
    2021-12-27T18:01:49.127+00:00
    2 people found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Museum Systeembeheer | Liemers Kunstwerk 0 Reputation points
    2024-10-12T12:40:05.75+00:00

    I made a utility that might be helpfull

    //
    // RestartOrShutdown.cs, Aad Slingerland, oktober 2024.
    // install-package System.Diagnostics.EventLog
    // See also:
    // https://morgantechspace.com/2013/08/convert-datetime-to-ticks-and-ticks-to.html
    // Limitations:
    // English locale only, but the texts "restart" and "power off" can be changed to another locale.  
    //
    using System.Diagnostics;
    namespace RestartOrShutdown;
    internal class Program
    {
        static int Main(string[] args)
        {
            int rc = 0;
            int minutes = 1;
            try
            {
                if (args.Length > 0)
                {
                    minutes = int.Parse(args[0]);
                }
    #pragma warning disable CA1416 // Validate platform compatibility
                EventLog syslog = new("System");
                //
                DateTime _now = DateTime.Now;
                long minus = 10000000;           // one second in ticks
                DateTime _past = _now.AddTicks(-(minus * 60 * minutes));
                //
                Debug.WriteLine(_now.Ticks);
                Debug.WriteLine(_past.Ticks);
                //
                var entries = syslog.Entries.Cast<EventLogEntry>()
                    .Where(x => x.TimeWritten.Ticks > _past.Ticks && x.EventID == 1074)
                    .ToList();
    #pragma warning restore CA1416 // Validate platform compatibility
                //
                foreach (EventLogEntry e in entries)
                {
                    Debug.WriteLine(e.TimeWritten.Ticks);
                    Debug.WriteLine(e.Message);
                    if (e.Message.Contains("restart"))
                    {
                        rc = 1;
                    }
                    else if (e.Message.Contains("power off"))
                    {
                        rc = 2;
                    }
                }
            }
            catch (System.FormatException)
            {
                Usage();
                rc = 9;
            }
            Console.WriteLine($"Returncode={rc}");
            return rc;
        }
        static void Usage()
        {
            Console.WriteLine(@"
    Usage: RestartOrShutdown [minutes]
    Queries the Operating System wether it is in the process of Restart or Shutdown.
    Return code 0 = neither.
    Return code 1 = Restarting in progress.
    Return code 2 = Shutdown in progress.
    Return code 9 = invalid argument value. 
    The argument [minutes] defaults to 1 minute.
    The argument [minutes] tells this program how far to look back in the Windows 
    System Event log for a record with EventID 1074.");
        }
    }
    
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.