Create empty Management Pack from SCOM via PowerShell

Found this one in my toolkit recently and thought I’d share. It’s a little old so the code could do with some optimizing..

What it does:

Prompts the script runner for a client number and then asks them to confirm the name for new management pack. If the operator types ‘yes’ (highly sophisticated coding) then a new management pack will be created with the desired naming convention

Our naming convention in this particular environment is to have all 3 client numbers at the beginning of the management pack name. As this is a multi-tenant environment it makes for easier identification.

 

Anyway, enjoy..

Dan

 

$ManagementServer = "SCOM01"
$orgmanagementpackname = "Management Pack Templates"

###
# Find the managementpacks which fit the filter documented in $orgmanagementpackname
###
$managementpackslist = Get-SCOMManagementPack -ComputerName $ManagementServer |where{$_.DisplayName -eq "$orgManagementPackName"}

Write-Host ""
Write-Host "Custom MP's Source"

$managementpacks = $managementpackslist 

Write-Host -foreground Yellow "Create a Custom Monitors and Settings management pack?"
Write-Host ""
$Action = Read-Host "Type 'yes' to continue"

If($Action -eq "yes"){

$client = read-host "Enter client number (3 digits)"

Foreach ($managementpackocc in $managementpacks) 
    {
    $name = $managementpackocc.name  
    $displayname = $managementpackocc.displayname
    $ManagementPackID = "Custom.Management.Pack.Templates" + ".Monitors.and.Settings." + $Client
    $ManagementPackName = $client + " - " + "Custom Management Pack Templates" + " Monitors and Settings"
        IF((Get-SCOMManagementPack -ComputerName $ManagementServer | where {$_.DisplayName -eq "$ManagementPackName"}) -eq $NUL)
        {
        $MessageGood = "MP " + "'" + $ManagementPackName + "'" + " being created..."
        Write-Host -ForegroundColor Green $MessageGood
        
        ###
        # For all managementpacks in array managementpacks create a new override management pack with a correct naming convention
        # and 1 override management pack per environment
        ###

        # fill in the name of the management packs
        Add-PSSnapin Microsoft.EnterpriseManagement.OperationsManager.Client
        $MG = New-Object Microsoft.EnterpriseManagement.ManagementGroup($ManagementServer)
        $MPStore = New-Object Microsoft.EnterpriseManagement.Configuration.IO.ManagementPackFileStore
        $MP = New-Object Microsoft.EnterpriseManagement.Configuration.ManagementPack($ManagementPackID, $ManagementPackName, (New-Object Version(1, 0, 0)), $MPStore)
        $MG.ImportManagementPack($MP)
        $MP = $MG.GetManagementPacks($ManagementPackID)[0]
        $MP.DisplayName = $ManagementPackName
        $MP.Description = "Auto Generated Management Pack based on: " + $orgmanagementpackname
        $MP.AcceptChanges()
        }
    ELSE
    {
    $MessageFalse = "MP " + "'" + $ManagementPackName + "'" + " already exists - Skipping"
    Write-Host -Foreground Red $MessageFalse}
    }
}
ELSE
{write-host -foregroundcolor red "!!! User Aborted Procedure !!!"}