How to Read and Write to 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 write to the Configuration Manager 2007 site control file by using the managed SMS Provider, you get the site definition file by querying for the required resource or component. You then update the embedded property, embedded property list, or multi-string list as required.
Observação
You can also use connection manager GetInstance to get the required resource or component.
The managed Configuration Manager 2007 manages the connection session to the site control file automatically for you. Therefore you treat the IResultObject objects returned from the query in the same way as you treat IResultObject objects retrieved from the SMS Provider.
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.
Use the Connection Manager QueryProcessor object ExecQuery or GetInstance method to get the required site control file resource or component IResultObject object.
Using the IResultObject update the site control file.
Use the IResultObject object Put method to commit the changes.
The following C# example accesses the client agent component of the site control file and creates a dummy property, property list and multi-string list. It then removes the updates that were made. The example demonstrates how to query the site control file, make updates, and commit changes to the site control file. The methods that are used to update the various embedded property types are documented in the following topics.
Method | Topic |
---|---|
WriteScfEmbeddedProperty |
How to Write a Configuration Manager Site Control File Embedded Property |
GetScfEmbeddedProperty |
How to Read a Configuration Manager Site Control File Embedded Property |
WriteScfEmbeddedPropertyList |
How to Write a Configuration Manager Site Control File Embedded Property List |
GetScfEmbeddedPropertyList |
How to Read a Configuration Manager Site Control File Embedded Property List |
WriteScfRegMultiStringList |
How to Write a Configuration Manager Site Control File Embedded RegMultiString List |
GetScfRegMultiStringList |
How to Read a Configuration Manager Site Control File Embedded RegMultiString List |
For information about calling the sample code, see Calling Configuration Manager Code Snippets.
public void ReadWriteSCF(WqlConnectionManager connection,string siteCode)
{
try
{
// Query for the site's site control file client agent settings.
IResultObject resources =
connection.QueryProcessor.ExecuteQuery
("SELECT * FROM SMS_SCI_ClientComp WHERE ClientComponentName = 'Client Agent' AND SiteCode = '" +
siteCode + "'");
foreach (IResultObject resource in resources)
{
// Embedded Properties
Console.WriteLine("Embedded property");
Console.WriteLine("-----------------");
int value = 0;
string value1 = "";
string value2 = "";
// Write a dummy embedded property.
this.WriteScfEmbeddedProperty(resource, "Test", 10, "Hello", "World");
// Get the embedded property back and display the values.
if (this.GetScfEmbeddedProperty(resource, "Test", ref value, ref value1, ref value2))
{
Console.WriteLine("Value: " + value);
Console.WriteLine("Value1: " + value1);
Console.WriteLine("Value2: " + value2);
// Remove the dummy embedded property.
Dictionary<string, IResultObject> EmbeddedProperties = resource.EmbeddedProperties;
EmbeddedProperties.Remove("Test");
resource.EmbeddedProperties = EmbeddedProperties;
resource.Put();
// See if the dummy embedded property is still there.
if (this.GetScfEmbeddedProperty(resource, "Test", ref value, ref value1, ref value2))
{
Console.WriteLine("Test exists");
}
else
{
Console.WriteLine("Test does not exist");
}
}
else
{
Console.WriteLine("Property not found");
}
Console.WriteLine();
// Embedded property list.
Console.WriteLine("Embedded property list");
Console.WriteLine("----------------------");
// values contains the embedded property list.
ArrayList values = new ArrayList();
values.Add("Elephant");
values.Add("Giraffe");
// Write to the resource.
this.WriteScfEmbeddedPropertyList(resource, "Animals", values);
ArrayList retrievedValues;
// Get the embedded property list and display.
if (this.GetScfEmbeddedPropertyList(resource, "Animals", out retrievedValues))
{
foreach (string retrievedValue in retrievedValues)
{
Console.WriteLine(retrievedValue);
}
// Remove one of the entries.
retrievedValues.Remove("Elephant");
Console.WriteLine();
// Update the list.
this.WriteScfEmbeddedPropertyList(resource, "Animals", retrievedValues);
// Display the list again.
this.GetScfEmbeddedPropertyList(resource, "Animals", out retrievedValues);
foreach (string retrievedValue in retrievedValues)
{
Console.WriteLine(retrievedValue);
}
}
else
{
Console.WriteLine("None");
}
Console.WriteLine();
// RegMultiStringList.
Console.WriteLine("RegMultiStringList");
Console.WriteLine("------------------");
// valuesStrings is the RegMultiString List.
ArrayList valueStrings = new ArrayList();
valueStrings.Add("Tom");
valueStrings.Add("Harry");
this.WriteScfRegMultiStringList(resource, "Names", valueStrings);
ArrayList retrievedValuesStrings;
if (this.GetScfRegMultiStringList(resource, "Names", out retrievedValuesStrings))
{
foreach (string retrievedValue in retrievedValuesStrings)
{
Console.WriteLine(retrievedValue);
}
// Remove one of the entries.
retrievedValuesStrings.Remove("Tom");
Console.WriteLine();
// Update the list.
this.WriteScfRegMultiStringList(resource, "Names", retrievedValuesStrings);
// Display the list again.
this.GetScfRegMultiStringList(resource, "Names", out retrievedValuesStrings);
foreach (string retrievedValue in retrievedValuesStrings)
{
Console.WriteLine(retrievedValue);
}
}
else
{
Console.WriteLine("None");
}
}
}
catch (SmsException e)
{
Console.WriteLine("Failed: " + e.Message);
throw;
}
}
The example method has the following parameters:
Parameter | Type | Description |
---|---|---|
connection |
|
A valid connection to the SMS Provider. |
siteCode |
|
The site code for the Configuration Manager site. |
System
System.Collections.Generic
System.Collections
System.ComponentModel
Microsoft.ConfigurationManagement.ManagementProvider
Microsoft.ConfigurationManagement.ManagementProvider.WqlQueryEngine
microsoft.configurationmanagement.managementprovider
adminui.wqlqueryengine
The Configuration Manager exceptions that can be raised are SmsConnectionException and SmsQueryException. These can be caught together with SmsException.
How to Write a Configuration Manager Site Control File Embedded Property
How to Write a Configuration Manager Site Control File Embedded Property List
About the SMS Provider in Configuration Manager
About the Configuration Manager Site Control File
How to Connect to an SMS Provider in Configuration Manager by Using Managed Code
How to Read a Configuration Manager Site Control File Embedded Property
How to Read a Configuration Manager Site Control File Embedded Property List
How to Read a Configuration Manager Site Control File Embedded RegMultiString List
How to Use Configuration Manager Objects with Managed Code
How to Write a Configuration Manager Site Control File Embedded RegMultiString List