Compartilhar via


How to Enumerate Through the Configuration Manager Site Control File by Using Managed Code

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

To enumerate through the Configuration Manager site control file, in Microsoft System Center Configuration Manager 2007, you create a query for each subclass of SMS_ControlItem.

To enumerate through the site control file

  1. Set up a connection to the SMS Provider. For more information, see How to Connect to an SMS Provider in Configuration Manager by Using Managed Code.

  2. Create the SmsBackgroundWorker object and populate the QueryProcessorObjectReady and QueryProcessorCompleted properties with the callback method names.

  3. Get the subclasses of the SMS_ControlItem class.

  4. Query each class returned in step three to get the site control file resource IResultObject collection.

  5. From the WqlConnectionManager object you obtain in step one, call the QueryProcessor object ProcessQuery method to start the asynchronous query for each resource.

Example

The following C# example enumerates through the entire Configuration Manager site control file by obtaining the subclasses for SMS_ControlItem with WqlConnectionManager.GetSubclasses. Alternatively, you could query for specific site control file items by using WqlConnectionManager.GetInstance. For an example, see How to Read and Write to the Configuration Manager Site Control File by Using Managed Code.

The following example has three functions:

  • EnumerateSiteControl takes a valid WqlConnectionManager object, enumerates through the SMS_ControlItem subclasses, and for each one, sets up an asynchronous query to retrieve all configuration items of the Configuration Manager site control file.

  • Bw1_QueryProcessorObjectRead is called for each configuration item of the Configuration Manager site control file that is found. For each item, the item properties, embedded properties, embedded property lists, and multiple-string lists are displayed.

  • Bw1_QueryProcessorCompleted is called when an asynchronous query is completed.

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

        public void EnumerateSiteControlFile(WqlConnectionManager connection)
        {
            try //set event handlers for each SCF class
            {
                List<IResultObject> l = connection.GetSubclasses("SMS_SiteControlItem", true);
                string siteCode = connection.NamedValueDictionary["ConnectedSiteCode"].ToString();

                foreach (IResultObject r in l)
                {
                                      
                    SmsBackgroundWorker bw1 = new SmsBackgroundWorker();

                    bw1.QueryProcessorObjectReady += new EventHandler<QueryProcessorObjectEventArgs>(bw1_QueryProcessorObjectReady);
                    bw1.QueryProcessorCompleted += new EventHandler<RunWorkerCompletedEventArgs>(bw1_QueryProcessorCompleted);

                    connection.QueryProcessor.ProcessQuery(bw1, "select * FROM " + r.ObjectClass + " WHERE SiteCode = '" + siteCode+"'");

                   // Pause while query runs.
                   Console.ReadLine();
                }
            }
            catch (SmsException ex)
            {
                Console.WriteLine("Failed to setup event handlers. Error: " + ex.Message);
                return;
            }
        }

        void bw1_QueryProcessorCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            Console.WriteLine("Done...");
        }
        void bw1_QueryProcessorObjectReady(object sender, QueryProcessorObjectEventArgs e)
        {
            try
            {
                IResultObject scfItem = (IResultObject)e.ResultObject;
                Console.WriteLine(scfItem.ObjectClass);
                Console.WriteLine("-------------------");
                // Add properties to node.
                Console.WriteLine("Properties");
                Console.WriteLine("    " + scfItem["ItemName"].StringValue);
                
                foreach (KeyValuePair<string, string> kvp in scfItem.PropertyList)
                {
                    if (kvp.Key != "PropLists" && kvp.Key != "Props")
                    {
                        Console.WriteLine("    " + kvp.Key + " : " + kvp.Value);
                    }
                }
               // Add embedded property lists.
 
                if (scfItem.EmbeddedPropertyLists.Count > 0) // there's some embedded properties
                {
                    Console.WriteLine("Embedded Property Lists");

                    foreach (KeyValuePair<string, IResultObject> kvp in scfItem.EmbeddedPropertyLists)
                    {
                        Console.WriteLine("    " + kvp.Key + " : " + kvp.Value.PropertyList["Values"]);
                    }
                }

                // Add embedded properties.

                if (scfItem.EmbeddedProperties.Count > 0)
                {
                    Console.WriteLine("Embedded Properties");
                    foreach (KeyValuePair<string, IResultObject> kvp in scfItem.EmbeddedProperties)
                    {
                        Console.WriteLine("    " + kvp.Value.PropertyList["PropertyName"]);
                        foreach (KeyValuePair<string, string> kvpValue in kvp.Value.PropertyList)
                        {
                            Console.WriteLine("        " + kvpValue.Key + " : " + kvpValue.Value);
                        }
                    }
                }
                // Add multString List.
                if (scfItem.RegMultiStringLists.Count > 0)
                {
                    Console.WriteLine("RegMultiStringLists");
                    foreach (KeyValuePair<string, IResultObject> kvp in scfItem.RegMultiStringLists)
                    {
                        Console.WriteLine("    " + kvp.Value.PropertyList["ValueName"]);
                        foreach (KeyValuePair<string, string> kvpValue in kvp.Value.PropertyList)
                        {
                            Console.WriteLine("        "+ kvpValue.Key + " : " + kvpValue.Value);
                        }

                    }
                }
                Console.WriteLine();

            }
            catch (SmsException ex)
            {
                Console.WriteLine("Query Error: " + ex.Message);
            }

        }

The example method has the following parameters:

Parameter Type Description

connection

  • WqlConnectionManager

A valid connection to the SMS Provider.

Compiling the Code

Namespaces

System

System.Collections.Generic

System.Collections

System.ComponentModel

Microsoft.ConfigurationManagement.ManagementProvider

Microsoft.ConfigurationManagement.ManagementProvider.WqlQueryEngine

Assembly

microsoft.configurationmanagement.managementprovider

adminui.wqlqueryengine

Robust Programming

The Configuration Manager exceptions that can be raised are SmsConnectionException and SmsQueryException. These can be caught together with SmsException.

See Also

Concepts

How to Use Configuration Manager Objects with Managed Code
About the Configuration Manager Site Control File
How to Read and Write to the Configuration Manager Site Control File by Using Managed Code
How to Connect to an SMS Provider in Configuration Manager by Using Managed Code
How to Use Configuration Manager Objects with Managed Code