OpsMgr 2012 – how to dump management pack bundles (small “improvement”)

Last week Daniele Grandini posted a great PowerShell script to dump the contents of Management Pack Bundles (.mpb) files and the old MP contents.

The script takes just two parameters an input directory where you have to copy MP and MPB files and an output directory where the dumps will be saved. The OpsMgr 2012 admin console needs to be installed for the script to work properly.

Today I finally had some time to play with OM12 and PowerShell for a session I’m preparing and I also tried to Daniele’s dump management pack bundles PowerShell script. It worked as expected, but the results of the dumped mpb files where stored in the same directory together with all other dumped mpb file contents. And this is not what I wanted.

image

 

I would rather have the contents of each dumped mpb file stored in it’s own folder. That way it’s easier to explore the contents of the mpb file IMO.

So I made some small changes to the PowerShell script Daniele created to have each mpb file dump it’s content in it’s own folder.

image

Now it’s easier to see what has been dumped for each mpb file.

I hope Daniele won’t mind me changing his script slightly to fit my personal preferences Smile 

Here is my changed version of Daniele’s script:

param($inDir, $outDir)

[Reflection.Assembly]::LoadWithPartialName("Microsoft.EnterpriseManagement.Core") [Reflection.Assembly]::LoadWithPartialName("Microsoft.EnterpriseManagement.Packaging")

$mpStore = new-Object Microsoft.EnterpriseManagement.Configuration.IO.ManagementPackFileStore($inDir)

$mps = get-childitem $inDir *.mp #$mpWriter = new-object Microsoft.EnterpriseManagement.Configuration.IO.ManagementPackXmlWriter($outDir) if ($mps -ne $null) {     foreach ($file in $mps)     {         md ($outDir + "\"+ $file) #ADDED              $mpWriter = new-object Microsoft.EnterpriseManagement.Configuration.IO.ManagementPackXmlWriter($outDir + "\"+ $file) #ADDED         $MPFilePath = $file.FullName         $mp = new-object Microsoft.EnterpriseManagement.Configuration.ManagementPack($MPFilePath)         Write-Host $file         $mpWriter.WriteManagementPack($mp)     } } #now dump MPB files $mps = get-childitem $inDir *.mpb $mpbReader = [Microsoft.EnterpriseManagement.Packaging.ManagementPackBundleFactory]::CreateBundleReader() if ($mps -ne $null) {     foreach ($file in $mps)     {         md ($outDir + "\"+ $file) #ADDED              $mpWriter = new-object Microsoft.EnterpriseManagement.Configuration.IO.ManagementPackXmlWriter($outDir + "\"+ $file) #ADDED            $mpb = $mpbReader.Read($file.FullName, $mpStore)         foreach($mp in $mpb.ManagementPacks)         {             #write the xml             $mpWriter.WriteManagementPack($mp)             $streams = $mpb.GetStreams($mp)             foreach($stream in $streams.Keys)             {                 $mpElement = $mp.FindManagementPackElementByName($stream)                 $fileName = $mpElement.FileName                 if ($fileName -eq $null)                 {                     $fileName = $outDir +'\' + ($mp.Name)+ '.' + $stream+ '.bin'                 }                 else                 {                     If ($fileName.IndexOf('\') -gt 0)                     {                         #only on dir level supported                         $dir = $outDir + '\' + $fileName.SubString(0, $fileName.IndexOf('\'))                         if ([System.Io.Directory]::Exists($dir) -ne $true)                         {                             [System.Io.Directory]::CreateDirectory($dir)                         }                     }                     #$fileName = "${outdir}\${fileName}"                     $fileName = $outdir+"\"+$file+"\$filename" #ADDED                 }                 Write-Host "`t$fileName"                 $fs = [system.io.file]::Create($fileName)                 $streams[$stream].WriteTo($fs)                 $fs.Close()             }         }     } }

 

See the comment # for what I changed.

 

This posting is provided "AS IS" with no warranties, and confers no rights.

Tweet