Dela via


How to Create a Subcollection

Applies To: System Center Configuration Manager 2007, System Center Configuration Manager 2007 R2, System Center Configuration Manager 2007 R3, System Center Configuration Manager 2007 SP1, System Center Configuration Manager 2007 SP2

In Microsoft System Center Configuration Manager 2007, your application uses the SMS_Collection Server WMI Class to define the attributes of a collection, such as the membership rules and the refresh schedule. The MemberClassName property contains the system-generated class name that contains the members of the collection.

Members of a collection are specified by using direct rules, query rules, or both. Direct rules define an explicit resource, and query rules define a dynamic collection that is regularly evaluated based on the current state of the site. Rules are added to a collection by using AddMembershipRule Method in Class SMS_Collection and AddMembershipRules Method in Class SMS_Collection.

Note

When creating a direct membership rule, remember that the rule must always have the same name as the computer that the rule specifies.

Subcollections are defined by the subordinate relationship, which is defined as the parent collection ID in the code example below.

Collections are closely tied to packages, programs and advertisements. For more information, see Software Distribution Overview.

The following examples require the following values:

  • A Windows Management Instrumentation (WMI) connection object.

  • An existing parent collection ID.

  • A new subcollection name.

  • A new subcollection comment.

  • The 'owned by this site' flag.

  • A resource class name.

  • A query (string).

  • A query rule name.

Example of the subroutine call in Visual Basic:

Call CreateSubCollection(swbemconnection, "SMS00001", "New Subcollection Name", "New subcollection comment.", true, "SELECT * from SMS_R_System", "New Rule Name")

Example of the method call in C#:

CreateSubCollection(WMIConnection, "SMS00001", "New Subcollection Name", "New subcollection comment.", true, "SELECT * from SMS_R_System", "New Rule Name")

To create a subcollection

  1. Set up a connection to the SMS Provider. For more information, see About the SMS Provider in Configuration Manager.

  2. Create the new collection object by using the SMS_Collection Server WMI Class.

  3. Define the collection to which the new collection is subordinate.

  4. Create the query rule by using the SMS_CollectionRuleQuery Server WMI Class.

  5. Add the rule to the collection.

  6. Refresh the collection.

Example

The following example method creates a subcollection.

For information about calling the sample code, see Calling Configuration Manager Code Snippets.

' Set up a connection to the local provider.
Set swbemLocator = CreateObject("WbemScripting.SWbemLocator")
Set swbemconnection= swbemLocator.ConnectServer(".", "root\sms")
Set providerLoc = swbemconnection.InstancesOf("SMS_ProviderLocation")

For Each Location In providerLoc
    If location.ProviderForLocalSite = True Then
        Set swbemconnection = swbemLocator.ConnectServer(Location.Machine, "root\sms\site_" + Location.SiteCode)
        Exit For
    End If
Next

Call CreateSubCollection(swbemconnection, "SMS00001", "New Subcollection Name", "New subcollection comment.", true, "SELECT * from SMS_R_System", "New Rule Name")


Sub CreateSubCollection(connection, existingParentCollectionID, newCollectionName, newCollectionComment, ownedByThisSite, queryForRule, ruleName)

    
    ' Create the collection.
    Set newCollection = connection.Get("SMS_Collection").SpawnInstance_
    newCollection.Comment = newCollectionComment
    newCollection.Name = newCollectionName
    newCollection.OwnedByThisSite = ownedByThisSite
    
    ' Save the new collection and the collection path for later.
    Set collectionPath = newCollection.Put_    
    
   ' Define to what collection the new collection is subordinate.
   ' IMPORTANT: If you do not specify the relationship, the new collection will not be visible in the console. 
    Set newSubCollectToSubCollect = connection.Get("SMS_CollectToSubCollect").SpawnInstance_
    newSubCollectToSubCollect.parentCollectionID = existingParentCollectionID
    newSubCollectToSubCollect.subCollectionID = CStr(collectionPath.Keys("CollectionID"))
    
    ' Save the subcollection information.
    newSubCollectToSubCollect.Put_

    ' Create a new collection rule object for validation.
    Set queryRule = connection.Get("SMS_CollectionRuleQuery")
    
    ' Validate the query (good practice before adding it to the collection). 
    validQuery = queryRule.ValidateQuery(queryForRule)
    
    ' Continue with processing, if the query is valid.
    If validQuery Then
        
        ' Create the query rule.
        Set newQueryRule = QueryRule.SpawnInstance_
        newQueryRule.QueryExpression = queryForRule
        newQueryRule.RuleName = ruleName
        
        ' Add the new query rule to a variable.
        Set newCollectionRule = newQueryRule
        
        ' Get the collection.
        Set newCollection = connection.Get(collectionPath.RelPath)
        
        ' Add the rules to the collection.
        newCollection.AddMembershipRule newCollectionRule

        ' Call RequestRefresh to initiate the collection evaluator. 
        newCollection.RequestRefresh False
     
    End If
    
End Sub
public void CreateSubCollection(WqlConnectionManager connection, 
                                string existingParentCollectionID, 
                                string newCollectionName, 
                                string newCollectionComment, 
                                bool ownedByThisSite, 
                                string query, 
                                string ruleName)
{
    try
    {
        // Create a new SMS_Collection object.
        IResultObject newCollection = connection.CreateInstance("SMS_Collection");

        // Populate new collection properties.
        newCollection["Name"].StringValue = newCollectionName;
        newCollection["Comment"].StringValue = newCollectionComment;
        newCollection["OwnedByThisSite"].BooleanValue = ownedByThisSite;

        // Save the new collection object and properties.  
        // In this case, it seems necessary to 'get' the object again to access the properties.  
        newCollection.Put();
        newCollection.Get();

        // Create subcollection object (holds information defining subcollection). 
        IResultObject newSubCollectToSubCollect = connection.CreateInstance("SMS_CollectToSubCollect");

        // Define parent relationship (in this case off the 'All Systems' collection node). 
        newSubCollectToSubCollect["parentCollectionID"].StringValue = existingParentCollectionID;
        newSubCollectToSubCollect["subCollectionID"].StringValue = newCollection["CollectionID"].StringValue;
        newSubCollectToSubCollect.Put();

        // Validate the query.
        Dictionary<string, object> validateQueryParameters = new Dictionary<string, object>();
        validateQueryParameters.Add("WQLQuery", query);
        IResultObject validationResult = connection.ExecuteMethod("SMS_CollectionRuleQuery", "ValidateQuery", validateQueryParameters);

        // Create query rule.
        IResultObject newQueryRule = connection.CreateInstance("SMS_CollectionRuleQuery");
        newQueryRule["QueryExpression"].StringValue = query;
        newQueryRule["RuleName"].StringValue = ruleName;

        // Add the rule. Although not used in this sample, queryID contains the query identifier.                   
        Dictionary<string, object> addMembershipRuleParameters = new Dictionary<string, object>();
        addMembershipRuleParameters.Add("collectionRule", newQueryRule);
        IResultObject queryID = newCollection.ExecuteMethod("AddMembershipRule", addMembershipRuleParameters);

        // Get parent collection handle for collection refresh.
        IResultObject parentCollectionForRefresh = connection.GetInstance(@"SMS_Collection.CollectionID='" + existingParentCollectionID + "'");

        // Start collection evaluator on parent collection.
        Dictionary<string, object> requestRefreshParameters = new Dictionary<string, object>();
        requestRefreshParameters.Add("IncludeSubCollections", false);
        parentCollectionForRefresh.ExecuteMethod("RequestRefresh", requestRefreshParameters);

        // Output new subcollection name.
        Console.WriteLine("Created subcollection: " + newCollectionName);
    }
    catch (SmsException ex)
    {
        Console.WriteLine("Failed to create subcollection. Error: " + ex.Message);
        throw; 
    }
}

The example method has the following parameters:

Parameter Type Description

connection

swbemconnection

  • Managed: WqlConnectionManager

  • VbScript: SWbemServices

A valid connection to the SMS Provider.

newCollectionName

  • Managed: String

  • VBScript: String

The unique name that represents the collection in the Configuration Manager console.

newCollectionComment

  • Managed: String

  • VBScript: String

General comment or note that documents the collection.

ownedByThisSite

  • Managed: Boolean

  • VBScript: Boolean

true if the collection originated at the local Configuration Manager site.

query

  • Managed: String

  • VBScript: String

WQL SELECT statement with results that are used to populate the collection. The statement must specify a resource class name.

ruleName

  • Managed: String

  • VBScript: String

Descriptive name that identifies the rule.

Compiling the Code

The C# example requires:

Namespaces

System

System.Collections.Generic

System.ComponentModel

Microsoft.ConfigurationManagement.ManagementProvider

Microsoft.ConfigurationManagement.ManagementProvider.WqlQueryEngine

Assembly

adminui.wqlqueryengine

microsoft.configurationmanagement.managementprovider

Robust Programming

For more information about error handling, see About Configuration Manager Errors.

Security

For more information about securing Configuration Manager applications, see Securing Configuration Manager Applications.

See Also

Reference

SMS_Collection Server WMI Class

Concepts

SMS_CollectionRuleQuery Server WMI Class
Configuration Manager Collections
Configuration Manager Software Distribution
Software Distribution Packages
Software Distribution Programs
Software Distribution Advertisements
How to Use Configuration Manager Objects with WMI
How to Use Configuration Manager Objects with Managed Code
How to Connect to an SMS Provider in Configuration Manager by Using Managed Code
How to Connect to an SMS Provider in Configuration Manager by Using WMI