Partager via


How to Import Configuration Baselines and Configuration Items

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, 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 one of the following:

  • DCM Digest XML only

  • Service Modeling Language (SML) XML only

  • SML XML that contains some DCM Digest XML schema

Imported configuration data that contains only DCM Digest XML is converted into SML XML during the import process, and all configuration data is displayed and can be modified.

When the imported configuration data consists of SML XML only or contains some DCM Digest XML, some configuration item details might not be displayed in the Configuration Manager console. However, you can still add those configuration items to configuration baselines, and the configuration data is correctly interpreted by client computers when they evaluate their compliance.

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.

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
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; 
        }
    }
}

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

Concepts

About Configuration Baselines and Configuration Items
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
SMS_BaselineAssignment Server WMI Class
SMS_ConfigurationItem Server WMI Class