共用方式為


How to Export 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, to export a configuration baseline or configuration item using the Configuration Manager SDK, read the relevant SMS_ConfigurationItem instance and write the SDMPackageXML property (string) to a file.

Important

The encoding of the XML file must be set to UTF-16 encoded Unicode.

Example

The following code example shows how to read an instance of a configuration baseline or configuration item and then export it to a file.

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

Sub DCMExportBaselineOrCI(swbemServices, _
                          pathToFile,    _
                          configurationItemId)

' Set required variables.
fileContents          =    ""
configurationItemXML  =    null

' Get specified configuration item (configurationItemId variable).
Set getCIInfo = swbemServices.Get("SMS_ConfigurationItem.CI_ID=" & configurationItemId )
 
' Copy configuration item XML into variable. 
configurationItemXML = getCIInfo.SDMPackageXML

Wscript.Echo configurationItemXML

' Open file for write (Unicode option enabled by second true).
Set FSO = CreateObject("Scripting.FileSystemObject")
Set textFile = FSO.CreateTextFile(pathToFile, true, true)

' Write XML content to file specified by pathToFile.     
textFile.Write configurationItemXML
textFile.Close 

Wscript.Echo " "
Wscript.Echo "Successfully wrote " & pathToFile
    
End Sub
public void DCMExportBaselineOrCI(WqlConnectionManager connection,
                                  string pathToOutputFile,
                                  string configurationItemId)
{

    // Set required variables.
    string configurationItemXML = null;

    try
    {
        // Get the specified configuration item (configurationItemId variable).
        IResultObject getCIInfo = connection.GetInstance(@"SMS_ConfigurationItem.CI_ID=" + configurationItemId);

        // Copy configuration item XML into variable. 
        configurationItemXML = getCIInfo["SDMPackageXML"].StringValue;
    }
    catch (SmsException ex)
    {
        Console.WriteLine("Failed to retrieve configuration item xml. " + "\\n" + ex.Message);
        throw;
    }

    StreamWriter sw = null;
    try
    {
        // Open file for output.
        sw = new StreamWriter(pathToOutputFile, false, System.Text.Encoding.Unicode);

        // Write XML to output file.
        sw.Write(configurationItemXML);
    }
    catch (Exception ex)
    {
        Console.WriteLine("Failed to write configuration item XML to: " + pathToOutputFile + "\\n" + ex.Message);
        throw;
    }
    finally
    {
        if (sw != null)
        {
            sw.Close();
        }
    }

    Console.WriteLine("Wrote configuration item XML to: " + pathToOutputFile);
}

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