In System Center 2012 Configuration Manager, importing a configuration baseline or configuration item by using the Configuration Manager SDK requires a properly formatted XML file. Unlike the Configuration Manager console, the Configuration Manager SDK does not support directly importing a CAB file.
Important |
---|
The encoding of the XML file must be set to UTF-16 encoded
Unicode. The XML encoding can be identified in the XML header:
<?xml version="1.0" encoding="utf-16" ?> |
When configuration data is imported into Configuration Manager, the format can be the following:
- DCM Digest XML only
To import Configuration Baselines and Configuration Items
-
Set up a connection to the SMS Provider.
-
Read the source XML file into a variable.
-
Create an instance the SMS_ConfigurationItem class.
-
Copy the source file contents (XML) into the SMS_ConfigurationItem property SDMPackageXML.
-
Save the configuration item instance.
Example
The following code examples show how to create an instance of a configuration baseline or a configuration item and then populate it by importing a configuration baseline or a configuration item XML definition.
For information about calling the sample code, see Calling Configuration Manager Code Snippets.
Visual Basic Script | Copy Code |
---|---|
Sub DCMImportBaselineOrCI(swbemServices, _ pathToFile) ' Set required variables. readFile = 1 'constant fileContents = "" initialReadSucceeded = "" triStateTrue = -1 ' This sets the file read to Unicode. ' Check if source xml file exists. set fileSytemObject = CreateObject("Scripting.FileSystemObject") If fileSytemObject.FileExists(pathToFile) Then set textFile = fileSytemObject.OpenTextFile(pathToFile, readFile, False, triStateTrue) fileContents = textFile.ReadAll textFile.Close initialReadSucceeded = true set textFile = Nothing Wscript.Echo " " Wscript.Echo "Successfully read " & pathToFile Else initialReadSucceeded = false Wscript.Echo " " Wscript.Echo "File does not exist." End If set fileSytemObject = Nothing If initialReadSucceeded Then On Error Resume Next ' Create an instance of configuration item. set newCI = swbemServices.Get("SMS_ConfigurationItem").SpawnInstance_() ' Copy specified file contents (XML) into SMS_ConfigurationItem property. newCI.SDMPackageXML = fileContents ' Save configuration item. newCI.Put_ If Err.Number<>0 Then Wscript.Echo "Couldn't create configuration item." Wscript.Echo "Possible duplicate configuration item or invalid XML." Wscript.Quit End If On Error Goto 0 Else Wscript.Echo " " Wscript.Echo "Failed to create configuration item." End If End Sub |
C# | Copy Code |
---|---|
public void DCMImportBaselineOrCI(WqlConnectionManager connection, string pathToFile) { // Set required variables. string fileContents = null; bool initialReadSucceeded = false; // Load XML file using pathToFile variable. try { // Open the file specified by the pathToFile variable and read the contents into a string. using (StreamReader sr = new StreamReader(pathToFile, System.Text.Encoding.Unicode)) { fileContents = sr.ReadToEnd(); } Console.WriteLine("Successfully read " + pathToFile + "."); initialReadSucceeded = true; } catch (Exception ex) { Console.WriteLine("Unable to read " + pathToFile + "." + "\n" + ex.Message); throw; } // Run only if the initial read was successful. if (initialReadSucceeded) { try { // Create an instance of Configuration Item. IResultObject newCI = connection.CreateInstance("SMS_ConfigurationItem"); // Copy specified file contents (XML) into SMS_ConfigurationItem property. newCI["SDMPackageXML"].StringValue = fileContents; // Save new SMS_ConfigurationItem object. newCI.Put(); } catch (SmsException ex) { Console.WriteLine("Failed to create configuration item using " + pathToFile + "."); Console.WriteLine(ex.Details); throw; } } } |
The example method has the following parameters:
Parameter |
Type |
Description |
|
|
A valid connection to the SMS Provider. |
pathToFile |
|
Path of the XML file to import. |
Compiling the Code
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
Tasks
How to Connect to an SMS Provider in Configuration Manager by Using Managed CodeHow to Connect to an SMS Provider in Configuration Manager by Using WMI
Reference
SMS_BaselineAssignment Server WMI ClassSMS_ConfigurationItem Server WMI Class
Concepts
About Configuration Baselines and Configuration ItemsHow to Use Configuration Manager Objects with WMI
How to Use Configuration Manager Objects with Managed Code