Sealing an XML Management Pack with Windows PowerShell and MPSeal.exe

We needed to make life a little easier for some of our new admins that need to seal some management packs. Ordinarily you’d open up a command prompt and run something like this.

cd “M:\SCOM 2012 R2\SupportTools\AMD64”
./MPSeal.exe “M:\Name of Management Pack.xml” /I “M:\Path to Referenced MP’s” /Outdir “M:\Output Directory” /Keyfile “M:\Seal Tools\mykey.snk” /Company “Your Company Name”

This would use the MPSeal.exe file from the Installation Media and seal an XML Management Pack using the appropriate information provided.

Now that’s ok, but let’s wrap it up in a little bit of PowerShell.

 

The cool part about using this method is that you now can select one or more XML files using a standard Windows Open File Dialog Box and then seal them all at once. Much easier.

Download the Script here: SealMP

 

#############################################################################
#
#   SealMP.ps1
#
#   Add the MPSeal.exe file from the Installation Media to the path where
#   you store this script. MPSeal.exe can be found in the SupportTools ->
#   AMD64 or i386 Folders.
#
#   Remember to change the following things:
#   The name of yourkeyfile.snk
#   Your Company Name
#   $Include = @(“M:\All-MPs”) to the path to where YOUR Mp’s are stored
#
##############################################################################

param(
[Parameter(Position = 0)]
[String[]]$Source = “”,

[Parameter(Position = 1)]
[String]$Target = “”,

[Parameter(Position = 2)]
[String]$ExePath = “.\MPSeal.exe”,

[Parameter(Position = 3)]
[String]$KeyFile = “yourkeyfile.snk”,

[Parameter(Position = 4)]
[String]$Company = “Your Company Name”,

[Parameter(Position = 5)]
[String[]]$Include = “”
)

# Function to display the ‘Open File Dialog Box’
# This will allow you to select multiple files if you want to seal more than one

function Select-FilesDialog {
$ScriptBlock = {
[Void][Reflection.Assembly]::LoadWithPartialName(‘System.Windows.Forms’)
$OpenFileDialog = New-Object Windows.Forms.OpenFileDialog
#$OpenFileDialog.InitialDirectory = “Desktop”
$OpenFileDialog.filter = “All XML Files (*.xml)| *.xml”
$OpenFileDialog.Title = “Browse for XML Management Pack/s to Seal”
$OpenFileDialog.Multiselect = $True
$OpenFileDialog.ShowDialog() | out-null
return $OpenFileDialog.FileNames
}
return & powershell -STA $ScriptBlock
}

# Function to display the ‘Save File to Folder Dialog Box’
function Save-toFolderDialog {
$ScriptBlock = {
[Void][Reflection.Assembly]::LoadWithPartialName(‘System.Windows.Forms’)
$FolderBrowserDialog = New-Object Windows.Forms.FolderBrowserDialog
$FolderBrowserDialog.RootFolder = “Desktop”
$FolderBrowserDialog.Description = “Where would you like to save the Sealed MP file/s?”
$FolderBrowserDialog.ShowDialog() | out-null
return $FolderBrowserDialog.SelectedPath
}
return & powershell -STA $ScriptBlock
}

if (!$Source) {
$Source = Select-FilesDialog
}
if (!$Target) {
$Target = Save-toFolderDialog
}
if (!$Include) {

# This is the Path to Management Packs that we might reference.
# Since this is an array you can have the script make use of multiple paths
# eg, $Include = @(“C:\MPs”,”E:\More MPs”,”M:\Some More MPs”)

$Include = @(“M:\All-MPs”)
}
if ($KeyFile -match “\\”) {
$a = New-Object -ComObject Scripting.FileSystemObject
$f = $a.GetFile($KeyFile)
$KeyFile = $f.ShortPath
}

$MPSeal = $ExePath

$Source | foreach {
if ($_) {
$Src = $_
$MPCommand = “`”$Src`” ”
$Include | foreach {
$MPCommand += “/I `”$_`” ”
}
$MPCommand += “/Keyfile `”$KeyFile`” /Company `”$Company`” /OutDir `”$Target`””

$MPCommand

# If we run this from the ISE it will error. Although we can ignore the error I hate looking at it,
# so I am redirecting the output of the command to null on the next line ‘2> $null’
# But when we do this, we will not receive any output telling us if the sealing was successful or not.
# We need output as it will tell us if we are missing references to MP’s.

& $MPSeal $MPCommand 2> $null

# So we now show the last entry written to the $error variable and you’ll get the output.
# if we only run this script from the PowerShell Console then we can remove the Write-Host
# line below and the redirect ‘2> $null’ above.

Write-Host -foregroundcolor White $Error[0]
$Src = $null
}
}

 

Oh and since I’ve been asked the question… “Will this Seal a Schema 2.0 Management Pack?” Yes it will, as long as the MPSeal.exe file is the one from the SCOM 2012 Installation Media.

Enjoy 🙂

 
Comments

No comments yet.

Leave a Reply