In Configuration Manager, the AddMembershipRule Windows Management Instrumentation (WMI) class method adds a new rule to the CollectionRules property of SMS_Collection Server WMI Class.

The following syntax is simplified from Managed Object Format (MOF) code and is intended to show the definition of the method.

SInt32 AddMembershipRule(
	 SMS_CollectionRule collectionRule,
	 UInt32 QueryID



Data type: SMS_CollectionRuleQualifiers: [in]SMS_CollectionRule Server WMI Class object to add.

Data type: UInt32Qualifiers: [out]Configuration Manager-generated query ID if the rule is a query rule. If the rule is direct, this ID is 0. Use QueryID to modify or delete a query membership rule.

Return Values

An SInt32 data type that is 0 to indicate success or non-zero to indicate failure.

For information about handling returned errors, see About Configuration Manager Errors.


This method can also be used to modify a membership rule.

Example Code

The following example shows how to add a direct rule and a query rule to a collection.

  Copy Code
	Dim instCollection As SWbemObject			'Instance of SMS_Collection
	Dim clsQueryRule As SWbemObject			'SMS_CollectionRuleQuery class method
	Dim instQueryRule As SWbemObject			 'Instance of SMS_CollectionRuleQuery
	Dim instDirectRule As SWbemObject			'Instance of SMS_CollectionRuleDirect
	Dim Query As String
	Dim ValidQuery As Boolean
	Dim QueryID As Long

	Set instCollection = Services.Get("SMS_Collection.CollectionID=""<collectionid>""")

	' Create the direct rule.
	Set instDirectRule = Services.Get("SMS_CollectionRuleDirect").SpawnInstance_
	instDirectRule.ResourceClassName = "SMS_R_System"
	instDirectRule.ResourceID = <resourceid>
	instDirectRule.RuleName = "MyDirectRule"
	' Add the direct rule to the collection.
	instCollection.AddMembershipRule instDirectRule

	' Always validate your query before adding a query rule to the collection.
	Query = "SELECT * FROM SMS_R_System WHERE SystemRoles = 'SMS Client Access Point'"
	Set clsQueryRule = Services.Get("SMS_CollectionRuleQuery")
	ValidQuery = clsQueryRule.ValidateQuery(Query)

	If ValidQuery Then
		' Create the query rule. Note that you use these same steps to 
		' modify an existing query rule, except that you set the QueryID property 
		' value to the query rule you want to modify.
		Set instQueryRule = clsQueryRule.SpawnInstance_
		instQueryRule.QueryExpression = Query
		instQueryRule.RuleName = "MyQueryRule"
		' Add the query rule to the collection. You only need to specify QueryID if
		' you want to capture the identifier for use later (you use QueryID to 
		' update or delete the rule.)
		instCollection.AddMembershipRule instQueryRule, QueryID
	End If

	' After you have added all the membership rules, call RequestRefresh to initiate
	' the collection evaluator.
	instCollection.RequestRefresh False


To delete a membership rule, the query number is needed to set the QueryID value.

To add multiple membership rules, use the AddMembershipRules Method in Class SMS_Collection.


Runtime Requirements

Development Requirements

See Also

Send comments about this topic to Microsoft.