Adding References to MPs in SCOM 2012 with PowerShell

This is a continuation of a Data Center Automation series of posts that I have been working on with Anders Bengtsson. Here are the first six posts in this series:

Creating Management Packs in SCOM 2012 with PowerShell
Creating Performance Collection Rules in SCOM 2012 with PowerShell
Creating Event Based Alerting Rules in SCOM 2012 with PowerShell
Enabling or Disabling Workflows in SCOM 2012 with PowerShell
Deleting Workflows in SCOM 2012 with PowerShell
Creating Groups in SCOM 2012 with PowerShell

As of this post this script is not included as an activity in the Operations Manager Admin Integration Pack but will be in the next version.

Syntax:

.\AddReference.ps1 –ManagementServer ‘om01.contoso.com’ –ManagementPackID ‘custom.example.test’ –MPPath ‘c:\temp\MyMP.mp’ –Alias ‘MyMP’

Parameters:

Name Description
ManagementServer Name of MS to connect to
ManagementPackID ID of the MP you want to modify
MPPath Path to the MP you would like to add as a reference
Alias The alias that you would like to use for the MP
  1 Param(            
 2     [parameter(Mandatory=$true)]            
 3     $ManagementServer,            
 4     [parameter(Mandatory=$true)]            
 5     $ManagementPackID,            
 6     [parameter(Mandatory=$true)]            
 7     $MPPath,
 8     [parameter(Mandatory=$true)]
 9     $Alias
10     )
11 
12 Write-Host "ManagementServer: "$ManagementServer
13 Write-Host "ManagementPackID: "$ManagementPackID
14 Write-Host "MPPath: "$MPPath
15 Write-Host "Alias: "$Alias
16 
17 function CreateManagementPack
18 {
19   param([object]$MG, [string]$ManagementPackID)
20   $MPStore = New-Object Microsoft.EnterpriseManagement.Configuration.IO.ManagementPackFileStore
21   $MP = New-Object Microsoft.EnterpriseManagement.Configuration.ManagementPack($ManagementPackID, $ManagementPackID, (New-Object Version(1, 0, 0)), $MPStore)
22   $MG.ImportManagementPack($MP)
23 }
24 
25 Write-Host "Adding SCOM Snap-in"
26 Add-PSSnapin Microsoft.EnterpriseManagement.OperationsManager.Client
27 
28 Write-Host "Connecting to SCOM Management Group"
29 $ManagementServer = New-Object Microsoft.EnterpriseManagement.ManagementGroup($ManagementServer)
30 
31 Write-Host "Getting MP Information and Incrementing Version"
32 try
33 {
34   $MP = $ManagementServer.GetManagementPacks($ManagementPackID)[0]
35   $VIncrement = $MP.Version.ToString().Split('.')
36   $VIncrement[$VIncrement.Length - 1] = ([system.int32]::Parse($VIncrement[$VIncrement.Length - 1]) + 1).ToString()
37   $MP.Version = ([string]::Join(".", $VIncrement))
38 }
39 catch
40 {
41   Write-Host "MP Not Found, Creating New MP"
42   CreateManagementPack $ManagementServer $ManagementPackID
43   $MP = $ManagementServer.GetManagementPacks($ManagementPackID)[0]
44 }
45 
46 Write-Host "Creating New MP Object from Referenced MP"
47 $MPToAdd = New-Object Microsoft.EnterpriseManagement.Configuration.ManagementPack($MPPath)
48 
49 Write-Host "Creating New MP Reference Object"
50 $MPReference = New-Object Microsoft.EnterpriseManagement.Configuration.ManagementPackReference($MPToAdd)
51 
52 Write-Host "Adding Reference"
53 $MP.References.Add($Alias, $MPReference)
54 
55 Write-Host "Saving Changes"
56 $MP.AcceptChanges()
57 
58 Write-Host "Script Completed"
59 

AddReference.renametops1