Пример #1
0
        /// <summary>
        /// Determine if a reboot is needed to apply updates
        /// </summary>
        /// <returns></returns>
        internal static bool IsRebootPending()
        {
            Logger.FunctionEnter();
            const string psFile = @".\CheckRebootState.ps1";
            bool         rc     = false;

            try
            {
                // next requires wu service to be running
                //WUApiLib.UpdateInstaller inst = new UpdateInstaller();
                //rc = inst.RebootRequiredBeforeInstallation;

                if (!rc && File.Exists(psFile) && !CloudUtilities.IsCloudOS())
                {
                    if (PS.RunPSCommand(@"-File " + psFile, out string outstr, true))
                    {
                        Logger.Debug("output string from PS: " + outstr);
                        rc = outstr.ToLower().Contains("true");
                    }
                }

                if (!rc)
                {
                    Logger.Debug("Com lookup returned false, checking registry to confirm");

                    // check registry keys for potential pending updates
                    const string _rebootKey1         = @"SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\";
                    const string _rebootKey2         = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing\";
                    const string _rebootValue        = "RebootRequired";
                    const string _sessionMgrKey      = @"SYSTEM\CurrentControlSet\Control\Session Manager\";
                    const string _pendingRenameValue = "PendingFileRenameOperations";

                    Logger.Debug("Checking for {0}", _sessionMgrKey);
                    RegistryKey baseKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Default);
                    RegistryKey subKey  = baseKey.OpenSubKey(_sessionMgrKey);
                    if (subKey != null)
                    {
                        Logger.Debug("Getting Subkey {0}", _pendingRenameValue);
                        var value = subKey.GetValue(_pendingRenameValue);
                        if (value != null)
                        {
                            var stringList = new List <string>(value as string[]);
                            if (stringList.Count > 0)
                            {
                                Logger.Comment("Pending Reboot Detected: Pending rename registry key setting found");
                                rc = true;
                            }
                        }
                    }

                    // NOTE: the next may fail on domain-joined machines due to MS domain admin policies.
                    //       this should run ok for WTT machines which is the intended target.
                    Logger.Debug("Checking {0}", _rebootKey1);
                    subKey = baseKey.OpenSubKey(_rebootKey1);

                    if (subKey != null)
                    {
                        string[] subKeys = subKey.GetSubKeyNames();
                        if (subKeys.Contains(_rebootValue))
                        {
                            Logger.Comment("Pending Reboot Detected: Pending reboot required registry key found");
                            rc = true;
                        }
                    }

                    Logger.Debug("Checking {0}", _rebootKey2);
                    subKey = baseKey.OpenSubKey(_rebootKey2);
                    if (subKey != null)
                    {
                        string[] subKeys = subKey.GetSubKeyNames();
                        if (subKeys.Contains(_rebootValue))
                        {
                            Logger.Comment("Pending Reboot Detected: Pending reboot required registry key found");
                            rc = true;
                        }
                    }
                }
            }

            catch (Exception ex)
            {
                GetData.GetExceptionMessage(ex);
            }


            Logger.Debug("Reboot Pending Query Returning {0}", rc);
            Logger.FunctionLeave();

            return(rc);
        }