SCOM management pack importer

After a couple of SCOM installations and upgrades where I need to import management packs I thought that a script would be nice to do this so I don’t have to check if the management pack is imported or not and if the version I try to import is newer than the version in SCOM.

So after some testing and scripting here is the result, a function:

function Import-MP($MPLocation) {
###################################################################
#    Written by Mattias Lehmus, TrueSec
#    Twitter: @onelehmus
#    Blog: onelehmus.com
#
#    THIS CODE IS PROVIDED *AS IS*
###################################################################

$MPFiles = (Get-ChildItem -Path “$MPLocation\*” -Include *.mp*,*.xml,*.mpb -Recurse)
[System.Collections.ArrayList]$InstallList = @()
$InstalledMPs = (Get-SCOMManagementPack | Select Name)

    #Functions
function Create-MPInstallationList($MPs){
foreach ($MP in $MPs) {
$MPName = [System.IO.Path]::GetFileNameWithoutExtension($MP)

            #Create InstallationList
If ($(Get-SCOMManagementPack | Where-Object { $_.name -contains $MPName }).count -eq 0) {
[Array]$MPInstallationList += $MP.FullName
Write-Verbose “$($MP.Name) is missing, adding to installation queue.” -Verbose
}
Else {
#Check if MP version is Equal
$MPFile = $MP.Name

                IF($MPFile -like “*.mp”){
$MPtoImport = Get-SCOMManagementPack -ManagementPackFile $MP.FullName
}
IF($MPFile -like “*.xml”){
$MPtoImport = Get-SCOMManagementPack -ManagementPackFile $MP.FullName
}
IF($MPFile -like “*.mpb”){
$MPtoImport = Get-SCOMManagementPack -BundleFile $MP.FullName
}
$MPinSCOM = Get-SCOMManagementPack | Where-Object { $_.name -contains $MPName }

                IF($MPinSCOM.Version.Major -lt $MPtoImport.Version.Major){
[Array]$MPInstallationList += $MP.FullName
Write-Verbose “$($MP.Name) is in newer version, adding to installation queue.” -Verbose
}
IF(($MPinSCOM.Version.Major -eq $MPtoImport.Version.Major) -and ($MPinSCOM.Version.Minor -lt $MPtoImport.Version.Minor)){
[Array]$MPInstallationList += $MP.FullName
Write-Verbose “$($MP.Name) is in newer version, adding to installation queue.” -Verbose
}
IF(($MPinSCOM.Version.Major -eq $MPtoImport.Version.Major) -and ($MPinSCOM.Version.Minor -eq $MPtoImport.Version.Minor) -and ($MPinSCOM.Version.Build -lt $MPtoImport.version.Build)){
[Array]$MPInstallationList += $MP.FullName
Write-Verbose “$($MP.Name) is in newer version, adding to installation queue.” -Verbose
}
IF(($MPinSCOM.Version.Major -eq $MPtoImport.Version.Major) -and ($MPinSCOM.Version.Minor -eq $MPtoImport.Version.Minor) -and ($MPinSCOM.Version.Build -eq $MPtoImport.version.Build) -and ($MPinSCOM.Version.Revision -lt $MPtoImport.version.Revision)){
[Array]$MPInstallationList += $MP.FullName
Write-Verbose “$($MP.Name) is in newer version, adding to installation queue.” -Verbose
}
}
}
Return $MPInstallationList
}

    function Install-MP($MP){
#Import SCOM Module
Import-Module OperationsManager
$VerbosePreference = “Continue”

        IF($MP -like “*.mp”){
Import-SCOMManagementPack -Fullname $MP -ErrorAction SilentlyContinue
}
IF($MP -like “*.xml”){
Import-SCOMManagementPack -Fullname $MP -ErrorAction SilentlyContinue
}
IF($MP -like “*.mpb”){
Import-SCOMManagementPack $MP -ErrorAction SilentlyContinue
}
}

    #Create MP Installation List
$InstallationList = Create-MPInstallationList -MPs $MPFiles

    #Exit if Installation list is empty
IF($InstallationList.Count -eq 0){
Write-Output “No Management Packs to import”
Break
}

    While($InstallationList.count -gt 0){
$InstallationListCount = $InstallationList.count
ForEach($MPInstall in $InstallationList){
Install-MP -MP $MPInstall
}

#Clear InstallationList
$InstallationList = @()

        #Create New InstallationList
$InstallationList = Create-MPInstallationList -MPs $MPFiles

IF($InstallListCount -eq $InstallationList.count){
Write-Output “Can’t import MP’s, do manual import: $InstallationList”
Break
}
}

    #Import Completed
Write-Output “Import of MPs completed”
}

The function can be downloaded here: https://1drv.ms/u/s!AsVfLv6C271rhqQH_Y3-zAwYrK8esQ

The function checks the specified folder and it’s subfolders for management packs in the format of MP, MPB and xml. Then it checks management packs that are imported in SCOM and compares the two lists. If a management pack is missing or is in a newer version it will import it, otherwise it will skip the management pack. The script will try to import all the management packs, if it can’t import the management packs it will send out a list of the management packs that can’t be imported so you know which MPs that needs to be checked.

To run the function you load it and then run the following command:

import-mp -MPLocation ‘C:\MPs’

I hope this can help some SCOM administrators with the management of management packs.

//Mattias

This entry was posted in Operations Manager, Powershell and tagged . Bookmark the permalink.

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s