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:
<Resources> <Image ID="SmileyImage" Accessibility="Public" FileName="Smiley.png" HasNullStream="false" /> </Resources>
once the script has the retrieved the resources it looks in the current directory for the files, if it finds the file, it adds the resource to the bundle. If the file can’t be found, it reports a warning, but continues to create the .mpb file. Note that this .mpb file won’t be able to be imported, but I decided to do this because I wanted to keep going to find all the issues in creating the .mpb file.
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.
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:
- Line 117 is where a management pack object is created, this is needed by the AddManagementPack method call in line 119
- Line 121 is the where we collect the resources that this management pack uses.
- For each one of the resources, it’s added with the AddResourceStream method in line 128. This method needs some very specific things.
- The management pack with which the resource is associated
- The name of the resource as defined by the management pack (which is why Get-Resources returns a hash table, so we can keep track of the resource name)
- The stream representing the resource (which is the other element in the hash table returned by Get-Resources)
- The last parameter ($EMPTY) is an optional signature (which would allow you to sign the resource) and we don’t need a signature for this example.
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.
PS> new-mpbfile .\ResourceExample.xml resourceexample VERBOSE: Adding MP: ResourceExample VERBOSE: Adding stream: SmileyImage VERBOSE: wrote mpb: C:\Program Files\System Center Management Packs\resourceexample.mpb
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).
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)
Here’s what it looks like when we use it. First on the MPB we just created:
PS> get-mpbinfo resourceexample.mpb|ft -group managementpack length,resourcename -au ManagementPack: ResourceExample (Not Sealed) Length ResourceName ------ ------------ 861 SmileyImage
Since we ship some .mpb files, I can use the script to inspect our product files:
PS> get-mpbinfo ConfigManagementPack.mpb|ft -gro managementpack length,resourcename -au ManagementPack: ServiceManager.ConfigurationManagement.Library (Sealed) Length ResourceName ------ ------------ 100224 ConfigurationManagementFormsAssembly 55152 JA.ConfigurationManagementFormResourcesAssembly 55152 EN.ConfigurationManagementFormResourcesAssembly 55152 DE.ConfigurationManagementFormResourcesAssembly 96112 ServiceManager.ConfigurationManagement.Library.Assembly.Form 46960 EN.ServiceManager.ConfigurationManagement.Library.Assembly.FormResource 42864 JA.ServiceManager.ConfigurationManagement.Library.Assembly.FormResource 42864 DE.ServiceManager.ConfigurationManagement.Library.Assembly.FormResource 38784 ServiceManager.ConfigurationManagement.Library.Assembly.Task 11136 EN.ServiceManager.ConfigurationManagement.Library.Assembly.TaskResource 10608 JA.ServiceManager.ConfigurationManagement.Library.Assembly.TaskResource 10608 DE.ServiceManager.ConfigurationManagement.Library.Assembly.TaskResource 1399 ConfigItemImage32x32 712 ConfigItemImage16x16 492 ServiceManager.ConfigItem.Image.Edit 922 ServiceManager.ConfigurationManagement.Library.Image.User 3320 ServiceManager.ConfigurationManagement.Library.Image.DeletedItem ManagementPack: ServiceManager.ConfigurationManagement.Configuration (Not Sealed) Length ResourceName ------ ------------ 712 ComputerImage16x16 815 SoftwareImage16x16 800 PrinterImage16x16 1073 SoftwareUpdateImage16x16
Thanks to Lee Holmes and his “Set-ClipboardScript” script which provided the formatting of the code samples!