Sometimes it may be necessary to find out whether a Windows computer needs to be restarted. There is an article that describes exactly which values have to be queried in the system. The result is a PowerShell script Get-PendingReboot. For administrators this is usually the perfect solution. However, there are scenarios where you might want to have a similar function in C#. Even after intensive internet research it seemed that there is no ready-made solution like for PowerShell.

During the research I came across an article describing various scripts in the Windows Update environment, such as determining whether the automatic updates service is enabled or a computer needs to be rebooted.

There you will find a reference to the Microsoft.Update.SystemInfo Object, which finally led to ISystemInformation interface having the method ISystemInformation::get_RebootRequired (Gets a Boolean value that indicates whether a system restart is required to complete the installation or uninstallation of one or more updates.).

Now it’s time for coding… =)

Add a reference to wuapi.dll


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WUApiLib;


namespace TestPendingReboot
{
    static class Program
    {
        static void Main(string[] args)
        {
            ISystemInformation systemInfo = new SystemInformation();

            Console.WriteLine(systemInfo.RebootRequired.ToString());

            Console.ReadLine();
        }
    }
}

Note: Does not cover all scenarios, like „application based“ pending reboots, e.g. when you install NET.Framework that requires a restart to be working properly.

Schreibe einen Kommentar