Creating Groups 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 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("&", "&")
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"

CreateGroup.txt

Comments

  • Anonymous
    October 27, 2013
    Great article really helped. Created the group however now need to populate it using a dynamic rule. Not practical to amend a group and add a dynamic rule to this manually, any idea how to PowerShell this?

  • Anonymous
    November 14, 2013
    The comment has been removed

  • Anonymous
    February 12, 2014
    Was having difficulty with the Microsoft.SystemCenter.InstanceGroup.Library reference not being present in new management packs.  Found that the simplest way to get the reference into any given MP was to add a test group to it through the console.  Thought I'd pass that along in case anyone else runs into the same issue.

  • Anonymous
    June 05, 2014
    Hi, Is there a way to add a Dynamic Inclusion Rule into the script? Thanks

  • Anonymous
    June 05, 2014
    The comment has been removed

  • Anonymous
    June 05, 2014
    You can do it manually after the fact, modify the script to support it (it's quite an effort), or use snippets in the VSAE to automate the creation of the groups along with the discoveries based on your criteria (creating a custom MP).

  • Anonymous
    June 10, 2014
    Sorry if I'm being dense here, but how do you see the IDs of your existing groups?  I want to create new groups but keep the ID naming convention intact.

  • Anonymous
    June 10, 2014
    Hi Karan, you'll need to export the MP they are in to XML. Groups are classes so they'll be towards the top of the management pack.

  • Anonymous
    June 16, 2014
    The comment has been removed

  • Anonymous
    June 16, 2014
    The comment has been removed

  • Anonymous
    June 16, 2014
    Sadly that didn't work.  Script completed with no errors.

  • Anonymous
    June 16, 2014
    The comment has been removed

  • Anonymous
    June 16, 2014
    Thanks for the update!

  • Anonymous
    June 03, 2015
    Sean I am running into the Microsoft.SystemCenter.InstanceGroup.Library reference not being present,  how exactly do I fix that, I didnt understand your fix.  Thanks!

  • Anonymous
    June 04, 2015
    I am getting this error at the end of the script Exception calling "InsertCustomMonitoringObjectGroup" with "1" argument(s): "Processing the template failed. Any ideas?

  • Anonymous
    June 04, 2015
    The comment has been removed

  • Anonymous
    June 15, 2015
    Question........  When you create a group,  does it not auto create a Folder view under Monitoring as well?  If not what could be the cause?  I created about 80+ groups and I dont see any of the Folders created under monitoring so I am wondering if I did something wrong or if maybe I am to powershell out the creation of folder views tied to MP's..

  • Anonymous
    June 15, 2015
    Benny, the API to create groups simply creates groups. The UI in the SCOM console is what creates that folder under views whenever you create a group. You can create these folders as well programmatically but I don't have any examples on how to do that.

  • Anonymous
    June 16, 2015
    The comment has been removed

  • Anonymous
    October 22, 2015
    The comment has been removed

  • Anonymous
    October 22, 2015
    The comment has been removed

  • Anonymous
    October 22, 2015
    The comment has been removed