Esempio n. 1
0
        private string VerifyPolicy()
        {
            if (string.IsNullOrEmpty(_policy.Name))
            {
                return("The Policy's Name Is Not Valid.");
            }
            if (string.IsNullOrEmpty(_policy.Guid))
            {
                return("The Policy's Guid Is Not Valid.");
            }

            if (_policy.Frequency != EnumPolicy.Frequency.OncePerComputer &&
                _policy.Frequency != EnumPolicy.Frequency.OncePerDay &&
                _policy.Frequency != EnumPolicy.Frequency.OncePerMonth &&
                _policy.Frequency != EnumPolicy.Frequency.OncePerUserPerComputer &&
                _policy.Frequency != EnumPolicy.Frequency.OncePerWeek &&
                _policy.Frequency != EnumPolicy.Frequency.Ongoing &&
                _policy.Frequency != EnumPolicy.Frequency.EveryXdays &&
                _policy.Frequency != EnumPolicy.Frequency.EveryXhours)
            {
                return("The Policy's Frequency Is Not Valid.");
            }
            if (_policy.Trigger != EnumPolicy.Trigger.Checkin && _policy.Trigger != EnumPolicy.Trigger.Login &&
                _policy.Trigger != EnumPolicy.Trigger.Startup &&
                _policy.Trigger != EnumPolicy.Trigger.StartupOrCheckin)
            {
                return("The Policy's Trigger Is Not Valid.");
            }
            if (_policy.RunInventory != EnumPolicy.InventoryAction.After &&
                _policy.RunInventory != EnumPolicy.InventoryAction.Before &&
                _policy.RunInventory != EnumPolicy.InventoryAction.Both &&
                _policy.RunInventory != EnumPolicy.InventoryAction.Disabled)
            {
                return("The Policy's Run Inventory Is Not Valid.");
            }
            int value;

            if (!int.TryParse(_policy.SubFrequency.ToString(), out value))
            {
                return("The Policy's Sub Frequency Is Not Valid.");
            }
            if (_policy.CompletedAction != EnumPolicy.CompletedAction.DoNothing &&
                _policy.CompletedAction != EnumPolicy.CompletedAction.Reboot && _policy.CompletedAction != EnumPolicy.CompletedAction.RebootIfNoLogins)
            {
                return("The Policy's Completed Action Is Not Valid.");
            }
            if (_policy.ExecutionType != EnumPolicy.ExecutionType.Cache &&
                _policy.ExecutionType != EnumPolicy.ExecutionType.Install)
            {
                return("The Policy's Execution Type Is Not Valid.");
            }
            if (_policy.ErrorAction != EnumPolicy.ErrorAction.AbortCurrentPolicy &&
                _policy.ErrorAction != EnumPolicy.ErrorAction.AbortRemainingPolicies &&
                _policy.ErrorAction != EnumPolicy.ErrorAction.Continue)
            {
                return("The Policy's Error Action Is Not Valid");
            }
            if (_policy.MissedAction != EnumPolicy.FrequencyMissedAction.NextOpportunity &&
                _policy.MissedAction != EnumPolicy.FrequencyMissedAction.ScheduleDayOnly)
            {
                return("The Policy's Frequency Missed Action Is Not Valid.");
            }
            if (_policy.LogLevel != EnumPolicy.LogLevel.Full &&
                _policy.LogLevel != EnumPolicy.LogLevel.HiddenArguments &&
                _policy.LogLevel != EnumPolicy.LogLevel.None)
            {
                return("The Policy's Log Level Is Not Valid.");
            }
            if (_policy.WuType != EnumPolicy.WuType.Disabled && _policy.WuType != EnumPolicy.WuType.Microsoft &&
                _policy.WuType != EnumPolicy.WuType.MicrosoftSkipUpgrades &&
                _policy.WuType != EnumPolicy.WuType.Wsus && _policy.WuType != EnumPolicy.WuType.WsusSkipUpgrades)
            {
                return("The Policy's Windows Update Type Is Not Valid.");
            }
            if (_policy.AutoArchiveType != EnumPolicy.AutoArchiveType.AfterXdays &&
                _policy.AutoArchiveType != EnumPolicy.AutoArchiveType.None &&
                _policy.AutoArchiveType != EnumPolicy.AutoArchiveType.WhenComplete)
            {
                return("The Policy's Auto Archive Type Is Not Valid.");
            }

            if (_policy.AutoArchiveType == EnumPolicy.AutoArchiveType.AfterXdays)
            {
                int autoArchiveSub;
                if (!int.TryParse(_policy.AutoArchiveSub, out autoArchiveSub))
                {
                    return("The Policy's Auto Archive Sub Is Not Valid.");
                }
            }

            if (_policy.WindowStartScheduleId != -1)
            {
                var schedule = new ServiceSchedule().GetSchedule(_policy.WindowStartScheduleId);
                if (schedule == null)
                {
                    return("The Policy's Start Schedule Id Is Not Valid.");
                }
            }
            if (_policy.WindowEndScheduleId != -1)
            {
                var schedule = new ServiceSchedule().GetSchedule(_policy.WindowEndScheduleId);
                if (schedule == null)
                {
                    return("The Policy's End Schedule Id Is Not Valid.");
                }
            }

            if (_policy.PolicyComCondition != EnumPolicy.PolicyComCondition.Any &&
                _policy.PolicyComCondition != EnumPolicy.PolicyComCondition.Selective)
            {
                return("The Policy's Com Server Condition Is Not Valid");
            }
            if (_policy.PolicyComCondition == EnumPolicy.PolicyComCondition.Selective)
            {
                var policyComServers = _policyService.GetPolicyComServers(_policy.Id);
                if (policyComServers == null)
                {
                    return("The Policy's Selected Com Servers Are Not Valid");
                }
                if (policyComServers.Count == 0)
                {
                    return("The Policy's Selected Com Servers Are Not Valid.  At Least One Server Must Be Selected.");
                }
                foreach (var policyComServer in policyComServers)
                {
                    var comServer = new ServiceClientComServer().GetServer(policyComServer.ComServerId);
                    if (comServer == null)
                    {
                        return("The Policy's Selected Com Servers Are Not Valid.  A Specified Com Server Does Not Exist");
                    }
                }
            }
            if (_policy.ConditionId != -1) // -1 = disabled
            {
                var conditionScript = new ServiceScriptModule().GetModule(_policy.ConditionId);
                if (conditionScript == null)
                {
                    return($"Condition Script For {_policy.Name} Does Not Exist");
                }

                if (!conditionScript.IsCondition)
                {
                    return($"The Condition Script For {_policy.Name} Is Not Currently Set As A Condition");
                }

                if (string.IsNullOrEmpty(conditionScript.Name))
                {
                    return($"A Condition Script For {_policy.Name} Has An Invalid Name");
                }

                if (conditionScript.Archived)
                {
                    return($"A Condition Script For {_policy.Name} Is Archived");
                }

                if (string.IsNullOrEmpty(conditionScript.ScriptContents))
                {
                    return("Condition Script: " + conditionScript.Name + " Has An Invalid Script.  It Cannot Be Empty.");
                }

                if (string.IsNullOrEmpty(conditionScript.Guid))
                {
                    return("Condition Script: " + conditionScript.Name + " Has An Invalid GUID");
                }

                if (conditionScript.ScriptType != EnumScriptModule.ScriptType.Batch &&
                    conditionScript.ScriptType != EnumScriptModule.ScriptType.Powershell &&
                    conditionScript.ScriptType != EnumScriptModule.ScriptType.VbScript)
                {
                    return("Condition Script: " + conditionScript.Name + " Has An Invalid Type");
                }

                if (!int.TryParse(conditionScript.Timeout.ToString(), out value))
                {
                    return("Condition Script: " + conditionScript.Name + " Has An Invalid Timeout");
                }

                List <string> successCodes = new List <string>();
                foreach (var successCode in conditionScript.SuccessCodes.Split(','))
                {
                    successCodes.Add(successCode);
                }

                if (successCodes.Count == 0)
                {
                    return("Condition Script: " + conditionScript.Name + " Has An Invalid Success Code");
                }

                if (successCodes.Any(code => !int.TryParse(code, out value)))
                {
                    return("Condition Script: " + conditionScript.Name + " Has An Invalid Success Code");
                }

                if (!string.IsNullOrEmpty(conditionScript.WorkingDirectory))
                {
                    try
                    {
                        Path.GetFullPath(conditionScript.WorkingDirectory);
                    }
                    catch
                    {
                        return("Condition Script: " + conditionScript.Name + " Has An Invalid Working Directory");
                    }
                }

                if (conditionScript.ImpersonationId != -1)
                {
                    var impAccount = new ServiceImpersonationAccount().GetAccount(conditionScript.ImpersonationId);
                    if (impAccount == null)
                    {
                        return("Condition Script: " + conditionScript.Name + " Has An Invalid Impersonation Account");
                    }
                }


                if (_policy.ConditionFailedAction != EnumCondition.FailedAction.MarkFailed &&
                    _policy.ConditionFailedAction != EnumCondition.FailedAction.MarkNotApplicable && _policy.ConditionFailedAction != EnumCondition.FailedAction.MarkSkipped &&
                    _policy.ConditionFailedAction != EnumCondition.FailedAction.MarkSuccess)
                {
                    return($"The Condition Failed Action For {_policy.Name} Is Not Valid");
                }
            }

            return(null);
        }
Esempio n. 2
0
 public IEnumerable <EntityPolicyComServer> GetPolicyComServers(int id)
 {
     return(_policyServices.GetPolicyComServers(id));
 }