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 five 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

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. Also, this script simply creates a blank group.

Syntax:

.\CreateGroup.ps1 –ManagementServer ‘om01.contoso.com’ –ManagementPackID ‘custom.example.test’ –GroupID group1 –GroupName ‘My Test Group 1’

Parameters:

Name Description
ManagementServer Name of MS to connect to
ManagementPackID ID of the MP you want to modify. This MP must contain a reference to the Microsoft.SystemCenter.InstanceGroup.Library.
GroupID ID of the group. This shouldn’t include the entire namespace, so in the example above my group ID will be custom.example.test.group1 but I should only pass group1 as the parameter.
GroupName Friendly name of the group
1 Param( 2 [parameter(Mandatory=$true)] 3 $ManagementServer, 4 [parameter(Mandatory=$true)] 5 $ManagementPackID, 6 [parameter(Mandatory=$true)] 7 $GroupID, 8 [parameter(Mandatory=$true)] 9 $GroupName 10 ) 11 12 Write-Host "ManagementServer: "$ManagementServer 13 Write-Host "ManagementPackID: "$ManagementPackID 14 Write-Host "GroupID: "$GroupID 15 Write-Host "GroupName: "$GroupName 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 function XMLEncode 26 { 27 param([string]$s) 28 $s = $s.Replace("&", "&amp;") 29 $s = $s.Replace("<", "&lt;") 30 $s = $s.Replace(">", "&gt;") 31 $s = $s.Replace('"', "&quot;") 32 $s = $s.Replace("'", "&apos;") 33 return $s.ToString() 34 } 35 36 Write-Host "Adding SCOM Snap-in" 37 Add-PSSnapin Microsoft.EnterpriseManagement.OperationsManager.Client 38 39 Write-Host "Connecting to SCOM Management Group" 40 $ManagementServer = New-Object Microsoft.EnterpriseManagement.ManagementGroup($ManagementServer) 41 42 Write-Host "Getting MP Information and Incrementing Version" 43 try 44 { 45 $MP = $ManagementServer.GetManagementPacks($ManagementPackID)[0] 46 $VIncrement = $MP.Version.ToString().Split('.') 47 $VIncrement[$VIncrement.Length - 1] = ([system.int32]::Parse($VIncrement[$VIncrement.Length - 1]) + 1).ToString() 48 $MP.Version = ([string]::Join(".", $VIncrement)) 49 } 50 catch 51 { 52 Write-Host "MP Not Found, Creating New MP" 53 CreateManagementPack $ManagementServer $ManagementPackID 54 $MP = $ManagementServer.GetManagementPacks($ManagementPackID)[0] 55 } 56 57 $Formula = '<MembershipRule Comment="Empty Membership Rule">' + ` 58 '<MonitoringClass>$MPElement[Name="SCIG!Microsoft.SystemCenter.InstanceGroup"]$</MonitoringClass>' + ` 59 '<RelationshipClass>$MPElement[Name="SCIG!Microsoft.SystemCenter.InstanceGroupContainsEntities"]$</RelationshipClass>' + ` 60 '<Expression>' + ` 61 '<SimpleExpression>' + ` 62 '<ValueExpression>' + ` 63 '<Value>True</Value>' + ` 64 '</ValueExpression>' + ` 65 '<Operator>Equal</Operator>' + ` 66 '<ValueExpression>' + ` 67 '<Value>False</Value>' + ` 68 '</ValueExpression>' + ` 69 '</SimpleExpression>' + ` 70 '</Expression>' + ` 71 '</MembershipRule>' 72 73 Write-Host "Getting Alias for the Microsoft.SystemCenter.InstanceGroup.Library Management Pack Reference" 74 $Alias = ($MP.References | where {$_.Value -like '*Microsoft.SystemCenter.InstanceGroup.Library*'}).key 75 If (!($Alias)) 76 { 77 Write-Host "Management Pack Reference Not Found, Exiting" 78 exit 79 } 80 ElseIf ($Alias -ne 'SCIG') 81 { 82 Write-Host "Management Pack Reference Found but Alias Not Equal to SCIG. Modifying Formula" 83 $Formula = $Formula.Replace("SCIG", $Alias) 84 } 85 86 Write-Host "Creating Group" 87 $Group = New-Object Microsoft.EnterpriseManagement.Monitoring.CustomMonitoringObjectGroup($ManagementPackID, $GroupID, (XMLEncode -s $GroupName), $Formula) 88 89 Write-Host "Adding Group" 90 $MP.InsertCustomMonitoringObjectGroup($Group) 91 92 Write-Host "Script Completed"