Exemple #1
0
        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);
            }
        }