private void addCMDeviceVariable(
            int strResourceID,
            string strVariableName,
            string strVariableValue)
        {

            // Connect to CM Site
            SmsNamedValuesDictionary namedValues = new SmsNamedValuesDictionary();
            WqlConnectionManager connection = new WqlConnectionManager(namedValues);
            connection.Connect("CM01");

            // Get the computer settings.
            IResultObject computerSettings = null;

            IResultObject computerSettingsQuery = connection.QueryProcessor.ExecuteQuery(
                "Select * from SMS_MachineSettings where ResourceId = '" + strResourceID + "'");

            foreach (IResultObject settings in computerSettingsQuery)
            {
                settings.Get();
                computerSettings = settings;
            }

            if (computerSettings == null) // It does not exist, so create it.
            {
                computerSettings = connection.CreateInstance(@"SMS_MachineSettings");
                computerSettings["ResourceID"].IntegerValue = strResourceID;
                computerSettings["SourceSite"].StringValue = "PS1";
                computerSettings.Put();
                computerSettings.Get();
            }

            // Create the computer variable.
            List<IResultObject> computerVariables = computerSettings.GetArrayItems("MachineVariables");
            IResultObject computerVariable = connection.CreateEmbeddedObjectInstance("SMS_MachineVariable");
            computerVariable["Name"].StringValue = strVariableName;
            computerVariable["Value"].StringValue = strVariableValue;
            computerVariable["IsMasked"].BooleanValue = false;

            // Add the computer variable to the computer settings.
            computerVariables.Add(computerVariable);
            computerSettings.SetArrayItems("MachineVariables", computerVariables);
            computerSettings.Put();

            
            // Close Connection
            connection.Dispose();
        }
        private string getCmCollection(string strCollectionName, int strResourceID, string strRuleName)
        {
            // Connect to CM Site
            SmsNamedValuesDictionary namedValues = new SmsNamedValuesDictionary();
            WqlConnectionManager connection = new WqlConnectionManager(namedValues);
            connection.Connect("CM01");

            // Create DirectMembership rules
            IResultObject addDeviceToCollection = connection.CreateInstance("SMS_CollectionRuleDirect");
            addDeviceToCollection["ResourceClassName"].StringValue = strCollectionName;
            addDeviceToCollection["Comment"].StringValue = null;
            addDeviceToCollection["OwnedByThisSite"].BooleanValue = true;
            addDeviceToCollection["LimitToCollectionID"].StringValue = null;

            // Add Rule to Collection

            Dictionary<string, object> addMembershipRuleParameters = new Dictionary<string, object>();
            addMembershipRuleParameters.Add("collectionRule", newStaticRule);
            IResultObject staticID = newCollection

            return strCollectionName;
        }