More with management packs   Leave a comment

Last post, I wrote about retrieving management packs from Service Manager and I don’t really have a lot more to say about retrieving the management pack information, except for provide a way where we don’t have to specify formatting.   Generally, I want to see whether the management pack is sealed, the version number and then name, which translates into using format-table like this:

format-table Sealed,Version,Name -autosize

I’ll create a ServiceManager.Format.ps1xml file which will format the default view of management packs.

    <autosize />

In order to add this to my environment, all I need to do is use the Update-FormatData cmdlet using the filename as an argument.  After that, retrieving management packs will be be in the format that I want.

PS> $MGroup.ManagementPacks.GetManagementPacks()

Sealed Version    Name
------ -------    ----
True   7.0.3683.0 ServiceManager.ServiceMaps.Library
True   7.0.3683.0 Microsoft.SystemCenter.InstanceGroup.Library
False  7.0.3683.0 ServiceManager.OpsMgrConnector.Configuration

So less typing for me, yay! 

For even less typing, I’ll create a script called Get-ManagementPack which creates a connection to Service Manager and retrieves the management packs.

here’s the script:

PS> get-content Get-ManagementPack.ps1
param ( $computerName = "localhost" )
$SMDIR = "C:\Program Files\Microsoft System Center\Service Manager 2010"
$COREDLL = "${SMDIR}/SDK Binaries/Microsoft.EnterpriseManagement.Core.dll"
[reflection.assembly]::LoadFile($COREDLL) | out-null
$MGroup = new-object Microsoft.EnterpriseManagement.EnterpriseManagementGroup $computerName

and to run:

PS> Get-ManagementPack

Sealed Version    Name
------ -------    ----
True   7.0.3683.0 ServiceManager.ServiceMaps.Library
True   7.0.3683.0 Microsoft.SystemCenter.InstanceGroup.Library
False  7.0.3683.0 ServiceManager.OpsMgrConnector.Configuration

Now we have a simple script and default formatting.  But we’re not done with management packs – if I want to see the contents of a management pack, I can do that via a process called "exporting".  Exporting a management pack lets me create an XML file of the management pack which I can then inspect the various elements of the management pack so I can see what it does (and how it does it).  An object exists for just the purpose of exporting management packs – Microsoft.EnterpriseManagement.Configuration.IO.ManagementPackXmlWriter and using the WriteManagementPack method, I can easily create the XML files.

This is perfect for a foreach pipeline, so for each management pack that I retrieve, I’ll create an XML file of the management pack contents.

PS> Get-ManagementPack | %{
>> $xmlWriter = new-object Microsoft.EnterpriseManagement.Configuration.IO.ManagementPackXmlWriter C:\temp 
>> } { 
>> $xmlWriter.WriteManagementPack($_) 
>> }

The first script block in the foreach command creates me an xmlWriter which will used for all the management pack objects that are passed from Get-ManagementPack.   The ManagementPackXmlWriter object has two constructors.  The constructor that I’m using takes a string which points to a directory which will contain the exported xml files.  When invoked, the method returns a string which is the fullname of the exported XML file, so when I execute it, I see the following (ellipses used to save space):


I can easily incorporate this into a script as well:

PS> Get-Content Export-ManagementPack.ps1
param ( $targetDirectory = $( throw "Need a target directory"), [switch]$verbose )
begin {
  if ( $verbose )
    $verbosePreference = "Continue"
  $xmlWriter = new-object Microsoft.EnterpriseManagement.Configuration.IO.ManagementPackXmlWriter $targetDirectory
process {
  if ( $_ -is "Microsoft.EnterpriseManagement.Configuration.ManagementPack" )
    $path = $xmlWriter.WriteManagementPack($_)
    if ( $verbose )
      Write-Verbose "Exporting: $path"
    Write-Error "$_ is not a management pack"

I’ve also added support for -verbose so I can see what’s being exported if I want, along with just a little checking to be sure that I’ve actually got a management pack.

Now I can run the following:

PS> Get-ManagementPack | Export-ManagementPack C:\temp


PS> Get-ManagementPack | Export-ManagementPack C:\temp -verbose


and export all my management packs in one simple step.   Next time I’ll discuss importing management packs.


Posted March 10, 2009 by jtruher3 in ServiceManager

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: