How to Enumerate the Members of a Collection

System Center

Updated: October 28, 2009

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, the preferred method to enumerate through a collection is to use SMS_FullCollectionMembership Server WMI Class.

Query 1: SMS_FullCollectionMembership: This example shows how to enumerate the members of the All Systems (SMS00001) collection by using the SMS_FullCollectionMembership Server WMI Class.

Query 2: SMS_CollectionMember_a: This example shows a slower alternative, by using the SMS_CollectionMember_a Server WMI Class class.

Query 3: SMS_Collection: This example shows a further alternative, which is to query the members by using the actual collection class name that is specified in the MemberClassName property of SMS_Collection Server WMI Class. Querying the actual class offers performance advantages and lets you create more complex queries, such as JOINs. The example is equivalent to the earlier queries.

noteNote
When the SMS Provider first initializes, it registers and dynamically loads the SMS collection class into memory. If a WQL query is made against the collection class before it is loaded, an empty query result set will be returned.

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

These examples require the following values:

  • A Windows Management Instrumentation (WMI) connection object.

Example of the subroutine call in Visual Basic:

Call EnumerateCollectionMembers(swbemServices)

Example of the method call in C#:

EnumerateCollectionMembers(WMIConnection)

To enumerate the members of a collection

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

  2. Define a query to select the resources for the collection.

  3. Execute the query and enumerate the results.

Example

The following example method enumerates the members of a collection.

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 swbemServices= swbemLocator.ConnectServer(".", "root\sms")
Set providerLoc = swbemServices.InstancesOf("SMS_ProviderLocation")

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

Call EnumerateCollectionMembers(swbemServices)

Sub EnumerateCollectionMembers(connection)

' Set required variables.
' Note:  Values must be manually added to the queries below.

    ' The following example shows how to enumerate the members of the All Systems (SMS00001) collection.
    Query1 = "SELECT ResourceID FROM SMS_FullCollectionMembership WHERE CollectionID = 'SMS00001'" 
    
    ' Run query.
    Set ListOfResources1 = connection.ExecQuery(Query1, , wbemFlagForwardOnly Or wbemFlagReturnImmediately)

    ' The query returns a collection that needs to be enumerated.
    Wscript.Echo " "
    Wscript.Echo "Query: " & Query1
    For Each Resource1 In ListOfResources1     
        Wscript.Echo Resource1.ResourceID   
    Next
    
    ' A slower alternative is to use the SMS_CollectionMember_a association class.
    Query2 = "SELECT ResourceID FROM SMS_CollectionMember_a WHERE CollectionID = 'SMS00001'" 
    
    ' Run query.
    Set ListOfResources2 = connection.ExecQuery(Query2, , wbemFlagForwardOnly Or wbemFlagReturnImmediately)

    ' The query returns a collection that needs to be enumerated.
    Wscript.Echo " "
    Wscript.Echo "Query: " & Query2
    For Each Resource2 In ListOfResources2     
        Wscript.Echo Resource2.ResourceID   
    Next
    
    ' A further alternative is to query the members by using the actual collection class name specified in the MemberClassName property of SMS_Collection. 
    Query3 = "SELECT ResourceID FROM SMS_CM_Res_Coll_SMS00001" 
    
    ' Run query.
    Set ListOfResources3 = connection.ExecQuery(Query3, , wbemFlagForwardOnly Or wbemFlagReturnImmediately)

    ' The query returns a collection that needs to be enumerated.
    Wscript.Echo " "
    Wscript.Echo "Query: " & Query3
    For Each Resource3 In ListOfResources3     
        Wscript.Echo Resource3.ResourceID   
    Next

End Sub
public void EnumerateCollectionMembers(WqlConnectionManager connection)
{
    // Set required variables.
    // Note:  Values must be manually added to the queries below.

    try
    {
        // The following example shows how to enumerate the members of the All Systems (SMS00001) collection.
        string Query1 = "SELECT ResourceID FROM SMS_FullCollectionMembership WHERE CollectionID = 'SMS00001'";

        // Run query.
        IResultObject ListOfResources1 = connection.QueryProcessor.ExecuteQuery(Query1);

        // The query returns a collection that needs to be enumerated.
        Console.WriteLine(" ");
        Console.WriteLine("Query: " + Query1);
        foreach (IResultObject Resource1 in ListOfResources1)
        {
            Console.WriteLine(Resource1["ResourceID"].IntegerValue);
        }

        // A slower alternative is to use the SMS_CollectionMember_a association class.
        string Query2 = "SELECT ResourceID FROM SMS_CollectionMember_a WHERE CollectionID = 'SMS00001'";

        // Run query.
        IResultObject ListOfResources2 = connection.QueryProcessor.ExecuteQuery(Query2);

        // The query returns a collection that needs to be enumerated.
        Console.WriteLine(" ");
        Console.WriteLine("Query: " + Query2);
        foreach (IResultObject Resource2 in ListOfResources2)
        {
            Console.WriteLine(Resource2["ResourceID"].IntegerValue);
        }

        // A further alternative is to query the members by using the actual collection class name specified in the MemberClassName property of SMS_Collection.
        string Query3 = "SELECT ResourceID FROM SMS_CM_Res_Coll_SMS00001";

        // Run query.
        IResultObject ListOfResources3 = connection.QueryProcessor.ExecuteQuery(Query3);

        // The query returns a collection that needs to be enumerated.
        Console.WriteLine(" ");
        Console.WriteLine("Query: " + Query3);
        foreach (IResultObject Resource3 in ListOfResources3)
        {
            Console.WriteLine(Resource3["ResourceID"].IntegerValue);
        }
    }

    catch (SmsException eX)
    {
        Console.WriteLine("Failed to run queries. Error: " + eX.Message);
        throw;
    }
}

The example method has the following parameters:

 

Parameter Type Description

connection

swbemServices

  • Managed: WqlConnectionManager

  • VBScript: SWbemServices

A valid connection to the SMS Provider.

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

Show: