How to Export an Operations Manager Management Pack

Applies To: Operations Manager 2007 R2, Operations Manager 2007 SP1, System Center Operations Manager 2007

You can use the Operations Manager class libraries to perform Management Pack deployment tasks. For example, you can export an existing Management Pack to an XML file.

Example

The following example demonstrates how to export the contents of a Management Pack to an XML file.

/// <summary>
/// Exports a sample Management Pack whose display name is
/// "Test Management Pack 1".
/// </summary>
using System;
using System.Collections.ObjectModel;
using Microsoft.EnterpriseManagement;
using Microsoft.EnterpriseManagement.Configuration;
using Microsoft.EnterpriseManagement.Configuration.IO;
using System.IO;
using System.Text;
using System.Xml;

namespace SDKSamples
{
    class Program
    {
        static void Main(string[] args)
        {
            ManagementGroup mg = new ManagementGroup("localhost");

            // Define the path to the directory that will store
            // the exported Management Pack.
            string mpPath = @"C:\Program Files\System Center Management Packs\";

            string query = "DisplayName = 'Test Management Pack 1'";
            ManagementPackCriteria mpCriteria = new ManagementPackCriteria(query);
            ReadOnlyCollection<ManagementPack> mpCollection = 
                mg.GetManagementPacks(mpCriteria);
            if (mpCollection.Count != 1)
                throw new InvalidOperationException(
                    "Error! Expected one Management Pack with: " + query);
            
            string outputFileName = mpPath + mpCollection[0].Name + ".xml";
            using (Stream mpStream = new FileStream(outputFileName, FileMode.Create))
            {
                XmlWriter xmlwriter = XmlWriter.Create(mpStream);
                ManagementPackXmlWriter mpwriter = new ManagementPackXmlWriter(xmlwriter);
                mpwriter.WriteManagementPack(mpCollection[0]);
                mpStream.Close();
            }
        }
    }
}
' <summary> 
' Exports a sample Management Pack whose display name is
' "Test Management Pack 1".
' </summary>
Imports System
Imports System.Collections.ObjectModel
Imports System.IO
Imports System.Text
Imports System.Xml
Imports Microsoft.EnterpriseManagement
Imports Microsoft.EnterpriseManagement.Configuration
Imports Microsoft.EnterpriseManagement.Configuration.IO


Namespace SDKSamples
    Class Program
        Public Overloads Shared Function Main(ByVal args() As String) As Integer

            Dim mg As ManagementGroup = New ManagementGroup("localhost")

            ' Define the path to the directory that will store
            ' the exported Management Pack.
            Dim mpPath As String = "C:\Program Files\System Center Management Packs\"

            Dim query As String = "DisplayName = 'Test Management Pack 1'"
            Dim mpCriteria As ManagementPackCriteria = New ManagementPackCriteria(query)
            Dim mpCollection As ReadOnlyCollection(Of ManagementPack) = _
                mg.GetManagementPacks(mpCriteria)
            If (mpCollection.Count <> 1) Then
                Throw New InvalidOperationException( _
                    "Error! Expected one Management Pack with: " & query)
            End If

            Dim outputFileName As String = mpPath & mpCollection(0).Name & ".xml"
            Using mpStream As Stream = New FileStream(outputFileName, FileMode.Create)

                Dim writer As XmlWriter = XmlWriter.Create(mpStream)
                Dim mpwriter As ManagementPackXmlWriter = New ManagementPackXmlWriter(writer)
                mpwriter.WriteManagementPack(mpCollection(0))
                mpStream.Close()
            End Using
        End Function
    End Class
End Namespace

See Also

Tasks

How to Import a New Management Pack
How to Display Management Pack Contents
How to Remove an Existing Management Pack

Other Resources

Automating Management Pack Development