private void btnAddRequirement_Click(object sender, EventArgs e) { this.SetNamedScopeForSCCMRequest(); ////retrieve the global condition to be associated by name WqlQueryResultsObject results = this.sccmConnection.QueryProcessor.ExecuteQuery(string.Format("SELECT * FROM SMS_GlobalCondition Where LocalizedDisplayName = '{0}'", cmbConditions.Items[cmbConditions.SelectedIndex])) as WqlQueryResultsObject; IResultObject result = null; foreach (IResultObject item in results) { result = item; break; } ////retrieve the application to add the global condition onto be name sccm.Application application = GetApplicationFromName(txtSCCMApplicationName.Text); if (result != null && application != null && application.DeploymentTypes.Any()) { ////need to get all the information for the result otherwise the call to the dcmobjectwrapper wont work result.Get(); ////Get the full id from the property of the result object string ciuniqueid = result.PropertyList["CI_UniqueID"]; ////It will contain the scope and ID which, both of which are needed seperately so split them on their seperator string[] ids = ciuniqueid.Split("/".ToCharArray()); string scope = ids[0]; string id = ids[1]; ////wrap up the wmi object in a wrapper object so it is possible to read out particular seetings DcmObjectWrapper dcmwrapper = DcmObjectWrapper.WrapExistingConfigurationItem(result) as DcmObjectWrapper; ComplexSetting complexsettings = dcmwrapper.InnerConfigurationItem.Settings; string name = complexsettings.ChildSimpleSettings[0].LogicalName; ////create the operands object which will set up ther comparison information CustomCollection <ExpressionBase> operands = new CustomCollection <ExpressionBase>(); ////Add the reference information to the global setting - note the assumption it is a string (this could also be read out of the object above) GlobalSettingReference setting = new GlobalSettingReference(scope, id, ScalarDataType.String, name, ConfigurationItemSettingSourceType.CIM); setting.SettingSourceType = ConfigurationItemSettingSourceType.Registry; setting.MethodType = ConfigurationItemSettingMethodType.Value; ////add the value into the value for the registry entry, note this example just focuses on string registry settings ConstantValue value = new ConstantValue(txtRequirementValue.Text, ScalarDataType.String); operands.Add(setting); operands.Add(value); ////for this example this requirement will check to see if something is of a particular value Expression exp = new Expression(ExpressionOperator.IsEquals, operands); ////Create a rule to add to the deplyment types requirement list Microsoft.SystemsManagementServer.DesiredConfigurationManagement.Rules.Rule rule = new Microsoft.SystemsManagementServer.DesiredConfigurationManagement.Rules.Rule("Rule_" + Guid.NewGuid().ToString("B").Replace("{", string.Empty).Replace("}", string.Empty), Microsoft.SystemsManagementServer.DesiredConfigurationManagement.Rules.NoncomplianceSeverity.None, null, exp); application.DeploymentTypes[0].Requirements.Add(rule); ////save the changes back to SCCM SaveApplication(application); } }
/// <summary> /// Add a registry requirement to an existing deployment type /// </summary> public static void AddRequirement(string applicationName, string authoringScopeId, string logicalName, string settingLogicalName, string value, string dataType, string expressionOperator, string server) { Application app = CMApplication.GetApplicationByName(applicationName, server); DeploymentType deploymentType = app.DeploymentTypes[0]; CustomCollection <ExpressionBase> settingsOperands = new CustomCollection <ExpressionBase>(); GlobalSettingReference settingReferences = null; ConstantValue constant = null; switch (dataType) { case "Version": settingReferences = new GlobalSettingReference(authoringScopeId, logicalName, DataType.Version, settingLogicalName, ConfigurationItemSettingSourceType.Registry); constant = new ConstantValue(value, DataType.Version); break; default: break; } settingsOperands.Add(settingReferences); settingsOperands.Add(constant); Expression expression = null; switch (expressionOperator) { case "IsEquals": expression = new Expression(ExpressionOperator.IsEquals, settingsOperands); break; case "LessEquals": expression = new Expression(ExpressionOperator.LessEquals, settingsOperands); break; default: break; } Rule rule = new Rule(Guid.NewGuid().ToString("N"), NoncomplianceSeverity.Critical, null, expression); deploymentType.Requirements.Add(rule); CMApplication.Save(app, server); }
/// <summary> /// Add a registry requirement to an existing deployment type /// </summary> public static void AddRequirement(string applicationName, string authoringScopeId, string logicalName, string settingLogicalName, string value, string dataType, string expressionOperator, string server) { Application app = CMApplication.GetApplicationByName(applicationName, server); DeploymentType deploymentType = app.DeploymentTypes[0]; CustomCollection<ExpressionBase> settingsOperands = new CustomCollection<ExpressionBase>(); GlobalSettingReference settingReferences = null; ConstantValue constant = null; switch (dataType) { case "Version": settingReferences = new GlobalSettingReference(authoringScopeId, logicalName, DataType.Version, settingLogicalName, ConfigurationItemSettingSourceType.Registry); constant = new ConstantValue(value, DataType.Version); break; default: break; } settingsOperands.Add(settingReferences); settingsOperands.Add(constant); Expression expression = null; switch (expressionOperator) { case "IsEquals": expression = new Expression(ExpressionOperator.IsEquals, settingsOperands); break; case "LessEquals": expression = new Expression(ExpressionOperator.LessEquals, settingsOperands); break; default: break; } Rule rule = new Rule(Guid.NewGuid().ToString("N"), NoncomplianceSeverity.Critical, null, expression); deploymentType.Requirements.Add(rule); CMApplication.Save(app, server); }