Historically, management packs have been comprised of a single XML file (either in XML format in a .xml file or in a binary representation in a signed .mp file). With the new version of the common System Center management pack infrastructure that ships in Service Manager, the definition of a management pack is being extended to include associated “resources” such as images, form assemblies, workflow assemblies, reports, T-SQL scripts, and more. The aggregation of the XML (or even multiple XMLs) plus its associated resources is called a “management pack bundle”.
A “management pack bundle” is really a MSI file with a file extension of .mpb (I should tell you that these .msi’s aren’t installable, we’re just using MSI as a file format). These bundles can be imported into Service Manager as a whole through a new MP import interface on the Data Access Service. You can import .mpb files via either the Management Packs view in the Administration workspace in the Service Manager console or using the Import-SCSMManagementPack PowerShell cmdlet (available in Beta 2). After import, the resources are automatically distributed to the appropriate places.
In this post I’ll explain how to to aggregate your assemblies and images and multiple mps into a single file, using the BundleFactory in the Microsoft.EnterpriseManagement.Packaging assembly. This factory will let you create .MPB files, which can include resources needed by the management pack. I’ve written a PowerShell script to make this easier for you. You can either just use the script attached to this blog post to create management pack bundles or continue on to learn more about how to use the BundleFactory APIs to create management pack bundles.
The script inspects the management pack defined in the ‘Resources” section of the management pack XML and retrieves the resources. Here’s what this section looks like in the MP I’m using as an example:
The script is one of the more complicated scripts that I’ve done in this blog at about 140 lines so we’ll go through it in sections.
Lines 10 through 19 declare some “constants” which I’ll use in the rest of the script.
Lines 22 and 23 load the needed assemblies. Since we install the assemblies into the GAC on the management server or a computer that has the Service Manager console installed on it, I can use the static LoadWithPartialName method on Reflection.Assembly to load the assemblies we need if this script is run where the assemblies are installed. Since the LoadWithPartialName method returns the assembly, this is saved away so I can use it in lines 24 through 27 to retrieve the types I need later. I’ve done this to avoid the requirement of loading the needed assemblies before running the script. This means that the script has fewer preconditions.
Lines 29 through 78 have function declarations. I declare two functions; the first function (Invoke-GenericMethod) allows me to invoke a generic method, which is how the resources from the management pack are retrieved. It’s a pretty tricky function which uses reflection to invoke the methods in Service Manager which use Generics. The second function, “Get-Resources” retrieves the resources and emits a stream of hash tables which contain the stream and the name of the resource. I need this information when I actually associate the resource with the management pack in the .mpb file.
Lines 80 through 103 collect the management packs into an array. This script allows you to create a .mpb file with more than a single management pack. Line 99 has a check to be sure that I actually got some files in my array, if not, the script exits.
001
002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031 032 033 034 035 036 037 038 039 040 041 042 043 044 045 046 047 048 049 050 051 052 053 054 055 056 057 058 059 060 061 062 063 064 065 066 067 068 069 070 071 072 073 074 075 076 077 078 079 080 081 082 083 084 085 086 087 088 089 090 091 092 093 094 095 096 097 098 099 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# New-MPBFile.ps1
# this takes files (.mp or .xml) and creates a .mpb file param ( $mpFile = $( throw "Must have mpfile" ), [string]$mpbname = "testmpb", $computername = "localhost" ) # VARIABLES NEEDED BY SCRIPT $VerbosePreference = "continue" $SMDLL = "Microsoft.EnterpriseManagement.Core" $SMPKG = "Microsoft.EnterpriseManagement.Packaging" $MPTYPE = "Microsoft.EnterpriseManagement.Configuration.ManagementPack" $MRESTYPE = "Microsoft.EnterpriseManagement.Configuration.ManagementPackResource" $SIGTYPE = "Microsoft.EnterpriseManagement.Packaging.ManagementPackBundleStreamSignature" $FACTYPE = "Microsoft.EnterpriseManagement.Packaging.ManagementPackBundleFactory" $EMGTYPE = "Microsoft.EnterpriseManagement.EnterpriseManagementGroup" $OPEN = [System.IO.FileMode]"Open" $READ = [System.IO.FileAccess]"Read" # make sure the appropriate assemblies are loaded and retrieve the needed types. $SMCORE = [reflection.assembly]::LoadWithPartialName($SMDLL) $SMPACKAGING = [reflection.assembly]::LoadWithPartialName($SMPKG) $EMPTY = $SMCORE.GetType($SIGTYPE)::Empty $TYPEOFMP = $SMCORE.GetType($MPTYPE) $TYPEOFMPR = $SMCORE.GetType($MRESTYPE) $BFACTORY = $SMPACKAGING.GetType($FACTYPE) # Functions # Invoke-GenericMethod # allows scripts to call generic methods. # arguments # mytype - the type inspect for the needed method # mymethod - the method name # typearguments - an array of types used by MakeGenericMethod # object - the object against which invoke is called # parameters - any parameters needed by invoke # it returns whatever is returned by invoke function Invoke-GenericMethod { param ( [type]$mytype, [string]$mymethod, $TypeArguments, $object, [object[]]$parameters = $null ) $Method = $mytype.GetMethod($mymethod) $genericMethod = $Method.MakeGenericMethod($TypeArguments) $genericMethod.Invoke($object,$parameters) } # Get-Resources # this function retrieves resources from the MP. Because our GetResources API # uses generics, it's a bit tricky to call # it returns a hash table of the stream, and the name for each resource # it takes a Management Pack object function Get-Resources { param ( $mpObject ) invoke-GenericMethod $TYPEOFMP "GetResources" $TYPEOFMPR $mpObject | %{ # check to see if we could find the file $fullname = (resolve-path $_.FileName -ea SilentlyContinue).path if ( ! $fullname ) { write-host -for red " WARNING: ('Cannot find resource: ' + $_.FileName) Skipping this resource, your MPB will probably not import Make sure that the resources are in the same directory as the MP" } else { $stream = new-object io.filestream $fullname,$OPEN,$READ @{ Stream = $stream; Name = $_.Name } } } } # Start # Collect all the mps to add to the mpb! $mpfileArray = @() foreach ( $file in $mpFile ) { foreach ( $item in resolve-path $file ) { if ( $item.path ) { $mpfileArray += $item.path } else { Write-Host -for red "ERROR: Cannot find file $item, skipping" } } } # Check to see if we have any management packs, if not, exit. if ( $mpFileArray.Count -eq 0 ) { Write-Host -for red "Error: No files to add" exit } # we need a connection to the server when we start creating # the management pack objects $EMG = new-object $EMGTYPE $computername # In order to create .mpb, we need to create one # we'll use the BundleFactory for this $BUNDLE = $BFACTORY::CreateBundle() # we'll keep a collection of all the resources that we open $AllResources = @() foreach($mpfilepath in $mpfileArray) { # This should handle creating mpb from a local file store. # For now, just create the mp object using the EnterpriseManagementGroup $theMP = new-object $MPTYPE $mpfilepath,$EMG Write-Verbose ("Adding MP: " + $theMP.Name) $BUNDLE.AddManagementPack($theMP) # Add the resources if any are associated with the MP $Resources = Get-Resources $theMP # Add the resources for this MP to the collection $AllResources += $Resources if ( $Resources ) { $Resources | %{ Write-Verbose ("Adding stream: " + $_.Name) $BUNDLE.AddResourceStream($theMP,$_.Name,$_.Stream,$EMPTY) } } } # WRITE THE mpb # First we need a BundleWriter $bundleWriter = $BFACTORY::CreateBundleWriter(${PWD}) # then we can write out the .mpb $mpbfullpath = $bundleWriter.Write($BUNDLE,$mpbname) write-verbose "wrote mpb: $mpbfullpath" # Cleanup the resources if ( $AllResources ) { $AllResources | %{ if ( $_.Stream ) { $_.Stream.Close(); $_.Stream.Dispose() } } } |
Line 107 is where we connect to the Service Manager Data Access Service. This used when the management pack objects are created in line 117.
Line 110 is where we finally create our bundle object which we use to aggregate all the file.
Since we’re going to be creating a number of resources, Line 112 declares an array which we’ll use to keep all the resources so we can clean up in the end.
The foreach loop in lines 113 to 131 is where the work really takes place:
We’re not done yet. We’ve created our bundle, but we need to write it, so line 135 creates a BundleWriter object with the BundleFactory and then line 137 writes the .mpb file.
Finally, we have a bit of clean up, so if there were any resources, we will close the stream and then dispose. Strictly speaking, this is probably not needed because when the script exits, the variables go out of scope and are then cleaned up eventually by the garbage collector, but it doesn’t hurt to be tidy.
The following is an example of using the script. It creates a new .mpb file based on an MP (ResourceExample.xml) which has a single resource (an image file) and some instructions to create a new folder with the image. The MP (as an XML file) and the image file are in my sky drive if you want to use them to try it out.
Here’s what it looks like in the Service Manager Console after I import the .mpb (using the Import-SCSMManagementPack cmdlet that is available in Beta2).
awesome!
Now that we can create a .mpb file, it sure would be nice if we could inspect one. The following script does that very thing. It takes as a .mpb file and returns the management packs and resources found in it.
This requires PowerShell V2 because of the way I’m using new-object which takes advantage of new features.
Instead of using a BundleWriter, I create a BundleReader to retrieve the management packs (line 18 through 20) and for each management pack (line 22), get the associated streams (line 26)
001
002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031 032 033 034 035 036 037 038 039 040 041 042 043 |
#requires -version 2.0
# required because of the way we use new-object # Get-MPBInfo.ps1 # Retrieve management pack and resource information from a .MPB file param ( $file, $computername = "localhost" ) $path = (resolve-path $file).path if ( ! $path ) { throw "Could not find '$file'" } $PACKDLL = "Microsoft.EnterpriseManagement.Packaging" $FSTYPE = "Microsoft.EnterpriseManagement.Configuration.IO.ManagementPackFileStore" $BUNDLET = "Microsoft.EnterpriseManagement.Packaging.ManagementPackBundleFactory" $pkgasm = [reflection.assembly]::LoadWithPartialName($PACKDLL) if ( ! $pkgasm ) { throw "Can't load packaging dll" } # get a bundlefactory type $BFACTORYT = $pkgasm.GetType($BUNDLET) # create a bundle reader # create a filestore, which is used by the bundlereader # and then read the bundle $br = $BFACTORYT::CreateBundleReader() $fs = new-object $FSTYPE $mpb = $br.Read($path,$fs) # for each managementpack, get the resources and create a custom object $mpb.ManagementPacks|%{ $ManagementPack = $_.name # keep track of whether the MP is sealed or no if ( $_.Sealed ) { $Sealed = "Sealed" } else { $Sealed = "Not Sealed" } $mpb.GetStreams($_) |%{ $streams = $_ # retrieve the keys and create a custom object which we'll use # in formatting $streams.keys | %{ $ResourceName = $_ $Length = $streams.Item($ResourceName).Length # this emits a custom object which can then be used with # PowerShell formatting # Get-MPBInfo <file>|ft -group ManagementPack Length,ResourceName new-object -type psobject -prop @{ ManagementPack = "$ManagementPack ($sealed)" Length = $Length ResourceName = $ResourceName } } } } |
Here’s what it looks like when we use it. First on the MPB we just created:
Since we ship some .mpb files, I can use the script to inspect our product files:
Thanks to Lee Holmes and his “Set-ClipboardScript” script which provided the formatting of the code samples!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.