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

CreateGroup.txt

Comments (22)

  1. Phil says:

    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?

  2. Russ Slaten says:

    Hey Phil,

    Unfortunately it would be by doing the same thing I did with the modify group membership code (see blogs.msdn.com/…/modifying-explicit-group-membership-in-scom-2012-with-powershell.aspx), which means creating your own XML manipulation functions. The explicit members are the most time consuming ones so that is why I wrote that blog. The dynamic part is harder to script because to design one easily you really need some sort of UI or just build the XML yourself which defeats the purpose of automation. The new SCCM 2012 SP1 cmdlets are great, they actually have the same functionality built for collections, but unfortunately we don't have this for SCOM group management.

    Russ

  3. Sean says:

    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.

  4. Scripter says:

    Hi, Is there a way to add a Dynamic Inclusion Rule into the script?

    Thanks

  5. Scripter says:

    Hi, i need to create lots of groups based on Computernames.

    I can create the groups with powershell with your script

    But i would like how to add a Dynamic inclusion rule to each group like:

    ( Object is Windows Computer AND ( DNS Name Contains TestComputer ) AND True )

    Is there a way to do it?

    Alternatively can i modify each group after creation and include this rule?

    Setup is System Center 2012

    Thanks

  6. Russ Slaten says:

    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).

  7. Karan Bhalla says:

    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.

  8. Russ Slaten says:

    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.

  9. Karan Bhalla says:

    Thank you for that Russ.  Is there a way to pipe some text to the 'Description' field of the group with this script?

  10. Russ Slaten says:

    This is a guess, but I think it will work. Please let us know. Add this above the "Script Completed" line at the bottom of the script.

    $Group.Description = (XMLEncode -s "Test Group Description")

    $MP.AcceptChanges()

  11. Karan Bhalla says:

    Sadly that didn't work.  Script completed with no errors.

  12. Karan Bhalla says:

    I figured it out.  I added your two lines above the "Adding Group" line.  It worked.

    Cheers!

  13. Russ Slaten says:

    Thanks for the update!

  14. Benny says:

    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!

  15. Benny says:

    I am getting this error at the end of the script

    Exception calling "InsertCustomMonitoringObjectGroup" with "1" argument(s): "Processing the template failed.

    Any ideas?

  16. Russ Slaten says:

    Benny, this is very basic code that won't create your entire MP for you, so you have to add the missing references for it to work. Try this script: blogs.msdn.com/…/modifying-explicit-group-membership-in-scom-2012-with-powershell.aspx. It's a more complex script that does a bit more including add missing references.

  17. Benny V says:

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

  18. Russ Slaten says:

    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.

  19. Benny V says:

    Russ,  Thanks brother I thought I was going crazy.  I could have sworn that in my QA Environment (during my test of these scripts) that the folder view under Monitoring was also created..  I will figure out how to do the folders and post my results..  Thanks again.

  20. Carsten says:

    Hi Russ,

    thanks for this posting, I have to migrate 50 Groups to another SCOM Server.

    Here is my question

    I create the Group, everything is fine, but if I want to fill in the "dynamic members" Groups (principal Name) and the SCOM console will Crash with the following error message. Do I have forgot something?

    ——————————————————————————-

    See the end of this message for details on invoking

    just-in-time (JIT) debugging instead of this dialog box.

    ************** Exception Text **************

    : Cannot process $MPElement..$ string. Null string has been passed.

    ———————————————————————————————-

  21. Carsten says:

    Hi Russ,

    thanks for this posting, I have to migrate 50 Groups to another SCOM Server.

    Here is my question

    I create the Group, everything is fine, but if I want to fill in the "dynamic members" Groups (principal Name) and the SCOM console will Crash with the following error message. Do I have forgot something?

    ——————————————————————————-

    See the end of this message for details on invoking

    just-in-time (JIT) debugging instead of this dialog box.

    ************** Exception Text **************

    : Cannot process $MPElement..$ string. Null string has been passed.

    ———————————————————————————————-

  22. CarstenJuli says:

    Hi Russ,

    thanks for this posting, I have to migrate 50 Groups to another SCOM Server.

    Here is my question

    I create the Group, everything is fine, but if I want to fill in the "dynamic members" Groups (principal Name) and the SCOM console will Crash with the following error message. Do I have forgot something?

    ——————————————————————————-

    See the end of this message for details on invoking

    just-in-time (JIT) debugging instead of this dialog box.

    ************** Exception Text **************

    : Cannot process $MPElement..$ string. Null string has been passed.

    ———————————————————————————————-

Skip to main content