protected override void OnStop() { if (_serviceSettings.HardSuppressSCNotifications) { RestoreSCNotification(); } _pipeClient.Send("Close", "01DB94E3-90F1-43F4-8DDA-8AEAF6C08A8E"); if (SqlCe.GetAutoEnforceFlag()) { var reqApps = CcmUtils.RequiredApps.Where(x => !x.InstallState.Equals("Installed")).ToList(); var pendingUpdates = CcmUtils.Updates.Where(x => x.EvaluationState == 1).ToList(); if (reqApps.Count() == 0 && pendingUpdates.Count() == 0 && !RebootChecker.RebootRequired(new RestartChecks() { PendingFileOperations = true }).Any) { Globals.Log.Information("Resetting Auto Enforce Flag, nothing left to enforce."); SqlCe.SetAutoEnforceFlag(false); _pipeClient.Send("SetBlue", "01DB94E3-90F1-43F4-8DDA-8AEAF6C08A8E"); _iEnforcementInterval = 5; if (UnsafeNativeMethods.IsUserLoggedOn()) { UnsafeNativeMethods.Run(_userApp, $"/ToastServiceEndNotification", false); } if (_settings.LegalNotice.UseLegalNotice) { Reg.RemoveLegalNotice(); } } } Globals.Log.Information("Service is stopping"); }
private bool RestartRequired() { if (CcmUtils.IsUpdatesEnforcing() || CcmUtils.IsAppsEnforcing()) { Globals.Log.Information("Detected ongoing installation, suppressing reboot checks."); return(false); } var checks = _settings.RestartChecks; var updatesStatus = _rebootServicingCheck && !SqlCe.GetUpdatesInstallStatusFlag(); if (!updatesStatus) { checks.ComponentBasedServicing = updatesStatus; checks.PendingFileOperations = updatesStatus; checks.WindowsUpdate = updatesStatus; } var result = RebootChecker.RebootRequired(checks); var rs = SqlCe.GetRestartSchedule(); if (!result.Any) { SqlCe.DeleteRestartSchedule(); } else if (rs != null) { if (rs.IsAcknowledged) { return(false); } } if (result.ConfigMgrClient && (_allowFastReboot || !UnsafeNativeMethods.IsUserLoggedOn())) { SqlCe.DeleteRestartSchedule(); SqlCe.SetRestartSchedule(new RestartSchedule { DeadLine = DateTime.Now.MinutePrecission().AddMinutes(5), RestartTime = DateTime.Now.MinutePrecission().AddMinutes(5), IsAcknowledged = true, IsExpress = true, }); return(result.Any); } if (result.Any && _rebootToastDelay-- <= 1) { if (rs == null) { SqlCe.SetRestartSchedule(new RestartSchedule { DeadLine = DateTime.Today.AddDays(_serviceSettings.RestartDeadlineAfterInstall.InDays).AddHours(_serviceSettings.RestartDeadlineAfterInstall.AtHour), RestartTime = DateTime.Today.AddDays(_serviceSettings.RestartDeadlineAfterInstall.InDays).AddHours(_serviceSettings.RestartDeadlineAfterInstall.AtHour), IsAcknowledged = false, IsExpress = false, }); } if (SqlCe.GetAutoEnforceFlag()) { Globals.Log.Information($"Detected auto enforce flag 'true', skipping reboot toast."); } else if (!UnsafeNativeMethods.IsSessionLocked()) { UnsafeNativeMethods.Run(_userApp, $"/ToastReboot", false); } else { Globals.Log.Information($"Detected a locked or non existing user session, a Toast notification was suppressed."); } _rebootToastDelay = _fiveMinuteIntervalsPerDay / _serviceSettings.NumberOfRestartToastsPerDay; } return(result.Any); }
private void EnforcementTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { _enforcementTimer.Stop(); _enforcementTimer.Interval = AutoInterval(); _enforcementTimer.Start(); SaveAssignmentsToDB(); if (SqlCe.GetAutoEnforceFlag()) { Globals.Log.Information("Checking Service cycle status."); var reqApps = CcmUtils.RequiredApps.Where(x => !x.InstallState.Equals("Installed") && x.EvaluationState == 3 && !x.IsIpuApplication).ToList(); var pendingUpdates = CcmUtils.Updates.ToList(); if (reqApps.Count() == 0 && pendingUpdates.Count() == 0 && !(CcmUtils.IsUpdatesEnforcing() || CcmUtils.IsAppsEnforcing()) && !RebootChecker.RebootRequired(new RestartChecks() { PendingFileOperations = true }).Any) { Globals.Log.Information("Service cycle - Resetting Auto Enforce Flag, nothing left to enforce and no reboot pending."); SqlCe.SetAutoEnforceFlag(false); _pipeClient.Send("SetBlue", "01DB94E3-90F1-43F4-8DDA-8AEAF6C08A8E"); _iEnforcementInterval = 5; if (UnsafeNativeMethods.IsUserLoggedOn()) { UnsafeNativeMethods.Run(_userApp, $"/ToastServiceEndNotification", false); } if (_settings.LegalNotice.UseLegalNotice) { Reg.RemoveLegalNotice(); } return; } else if (CcmUtils.IsRebootPending().HardRebootPending&& !(CcmUtils.IsUpdatesEnforcing() || CcmUtils.IsAppsEnforcing())) { Globals.Log.Information("Service cycle - Pending hard reboot detected - restarting computer."); if (UnsafeNativeMethods.IsUserLoggedOn()) { UnsafeNativeMethods.Run(_userApp, $"/ToastServiceRestartNotification", false); RestartComputer(); } else { RestartComputer(true); } return; } else if (!(CcmUtils.IsUpdatesEnforcing() || CcmUtils.IsAppsEnforcing()) && (reqApps.Count() != 0 || pendingUpdates.Count() != 0)) { Globals.Log.Information("Service cycle - Cycle running but required installations not enforcing - pushing enforcement."); CcmUtils.InstallAllAppsAndUpdates(); return; } else if (reqApps.Count() == 0 && pendingUpdates.Count() == 0 && !(CcmUtils.IsUpdatesEnforcing() || CcmUtils.IsAppsEnforcing()) && RebootChecker.RebootRequired(new RestartChecks() { PendingFileOperations = true }).Any) { Globals.Log.Information("Service cycle - nothing left to enforce but a pending reboot (Any) was detected - restarting computer."); if (UnsafeNativeMethods.IsUserLoggedOn()) { UnsafeNativeMethods.Run(_userApp, $"/ToastServiceRestartNotification", false); RestartComputer(); } else { RestartComputer(true); } return; } Globals.Log.Information("Service cycle not yet finished."); return; } if (CcmUtils.IsUpdatesEnforcing()) { return; } var rs = SqlCe.GetRestartSchedule(); if (rs != null) { if (rs.RestartTime <= DateTime.Now) { SqlCe.DeleteRestartSchedule(); if (UnsafeNativeMethods.IsUserLoggedOn()) { UnsafeNativeMethods.Run(_userApp, $"/ShowRestartWindow", false); Globals.Log.Information($"Launched Restart Window."); } else { RestartComputer(true); } return; } } RestartRequired(); if (CcmUtils.IsRebootPending().HardRebootPending) { Globals.Log.Warning($"Detected pending hard reboot - postponing further enforcement(s)"); return; } try { var serviceSchedule = CommonUtils.GetNextServiceCycleAsDateTime(); if (serviceSchedule != null) { if (serviceSchedule <= DateTime.Now) { SqlCe.SetAutoEnforceFlag(true); _pipeClient.Send("SetRed", "01DB94E3-90F1-43F4-8DDA-8AEAF6C08A8E"); CcmUtils.InstallAllAppsAndUpdates(); _iEnforcementInterval = 1; _enforcementTimer.Stop(); _enforcementTimer.Interval = AutoInterval(); _enforcementTimer.Start(); if (_settings.LegalNotice.UseLegalNotice) { Reg.SetLegalNotice(_settings.LegalNotice); } SqlCe.DeleteServiceSchedule(); SqlCe.UpdateSupData("STD", string.Empty); if (UnsafeNativeMethods.IsUserLoggedOn()) { UnsafeNativeMethods.Run(_userApp, $"/ToastServiceInitNotification", false); } Globals.Log.Information($"Found and triggered a Service Schedule (Install All)"); return; } } } catch (Exception ex) { Globals.Log.Error(ex.Message); } try { var sch = SqlCe.GetDueSchedules(); if (sch.Count() > 0) { Globals.Log.Information($"Found {sch.Count()} objects due for enforcement."); sch.ForEach(x => _objectsToEnforce.Add(x)); } else { Globals.Log.Information($"No scheduled enforcements found at this time"); } } catch (Exception ex) { Globals.Log.Error(ex.Message); } try { if (CcmUtils.IsApplicationEvaluationRequired()) { CcmUtils.EvaluateAllPolicies(); } } catch (Exception ex) { Globals.Log.Error(ex.Message); } }