Several months ago, there was a request in the Microsoft OpsMgr PowerShell newsgroup where someone was asking if PowerShell provided functionality for working with OpsMgr groups.
NOTE: watch the line wrap - I'll post the script separate in the Downloads section as well.
I definitely wanted to help out. I searched around a bit, and finally this post by Jakub Olesky here http://blogs.msdn.com/jakuboleksy/archive/2006/11/15/creating-and-updating-groups.aspx. That posting was a little bit intimidating to say the least, at the time.
I asked around for some help and Stefan Stranger (http://blogs.technet.com/stefan_stranger/) came to the rescue. He modified Jakub’s code and created a working console application (the code was blogged, but the post is no longer available).
Recently, I finally got back on track with trying to port Jakub’s code to pure PowerShell (but still using the OpsMgr SDK).
I have pretty much taken Jakub’s code line for line and translated it info PowerShell below. Now, this will start a few related blog posts. Now, I’ll mention right off here is that I copied Jakub’s usage of XML objects in this case. I’m going to blog more about this in the next week or so, but will present scripts using string objects (vs XML objects) to greatly simply (I think) any modifications to the code.
Enough talking, here’s Jakub’s code translated into PowerShell (watch the line wrap - I'll post the script separate in the Downloads section as well).
#---------Begin Sample Script-------------
$mg = New-Object Microsoft.EnterpriseManagement.ManagementGroup("localhost")
$document=new-object System.Xml.XMLDocument
$rootElement = $document.CreateElement("Configuration")
$document.AppendChild($rootElement)
$membershipRuleElement = $document.CreateElement("MembershipRule")
$membershipClassElement = $document.CreateElement("MonitoringClass")
$membershipRelationshipElement = $document.CreateElement("RelationshipClass")
$membershipClassElement.set_InnerText("`$MPElement[Name=`"Windows!Microsoft.Windows.Computer`"]`$")
$membershipRelationshipElement.set_InnerText("`$MPElement[Name=`"InstanceGroup!Microsoft.SystemCenter.InstanceGroupContainsEntities`"]`$")
$rootElement.AppendChild($membershipRuleElement)
$membershipRuleElement.AppendChild($membershipClassElement);
$membershipRuleElement.AppendChild($membershipRelationshipElement);
$formula=$rootElement.get_InnerXml()
$allComputersGroup = new-object Microsoft.EnterpriseManagement.Monitoring.CustomMonitoringObjectGroup("Jakub.At.Work.Namespace","AllComputerGroup","Jakub@Work Sample All Computers Group",$formula)
$defaultManagementPack = $mg.GetManagementPacks("Microsoft.SystemCenter.OperationsManager.DefaultUser")[0]
$windowsManagementPack = $mg.GetManagementPack([Microsoft.EnterpriseManagement.Configuration.SystemManagementPack]::Windows)
$instanceGroupManagementPack = $mg.GetManagementPack([Microsoft.EnterpriseManagement.Configuration.SystemManagementPack]::Group)
$newReferences = new-object Microsoft.EnterpriseManagement.Configuration.ManagementPackReferenceCollection
$newReferences.Add("Windows",$windowsManagementPack)
$newReferences.Add("InstanceGroup",$instanceGroupManagementPack)
$defaultManagementPack.InsertCustomMonitoringObjectGroup($allComputersGroup,$newReferences)
$myNewGroup = $mg.GetMonitoringClasses("Jakub.At.Work.Namespace.AllComputerGroup")[0]
$groupPopulateDiscovery = $myNewGroup.GetMonitoringDiscoveries()[0]
Write-Host "The discovery configuration: $groupPopulateDiscovery.DataSource.Configuration"
$document=new-object System.Xml.XMLDocument
$rootElement=$document.CreateElement("Configuration")
$document.AppendChild($rootElement)
$ruleIdElement=$document.CreateElement("RuleId")
$groupInstanceElement = $document.CreateElement("GroupInstanceId")
$membershipRulesElement = $document.CreateElement("MembershipRules")
$membershipRuleElement = $document.CreateElement("MembershipRule")
$membershipClassElement = $document.CreateElement("MonitoringClass")
$membershipRelationshipElement = $document.CreateElement("RelationshipClass")
$excludeListElement = $document.CreateElement("ExcludeList")
$monitoringObjectIdElement = $document.CreateElement("MonitoringObjectId")
$ruleIdElement.set_InnerText("`$MPElement`$")
$groupInstanceElement.set_InnerText("`$MPElement[Name=`"Jakub.At.Work.Namespace.AllComputerGroup`"]`$")
$membershipClassElement.set_InnerText("`$MPElement[Name=`"Windows!Microsoft.Windows.Computer`"]`$")
$membershipRelationshipElement.set_InnerText("`$MPElement[Name=`"InstanceGroup!Microsoft.SystemCenter.InstanceGroupContainsEntities`"]`$")
$monitoringObjectIdElement.set_InnerText("f5E5F15E-3D7D-1839-F4C6-13E36BCD982a")
$rootElement.AppendChild($ruleIdElement)
$rootElement.AppendChild($groupInstanceElement)
$excludeListElement.AppendChild($monitoringObjectIdElement)
$membershipRuleElement.AppendChild($membershipClassElement)
$membershipRuleElement.AppendChild($membershipRelationshipElement)
$membershipRuleElement.AppendChild($excludeListElement)
$membershipRulesElement.AppendChild($membershipRuleElement)
$rootElement.AppendChild($membershipRulesElement)
$newconfiguration=$rootElement.get_InnerXml()
$groupPopulateDiscovery.Status = [microsoft.enterprisemanagement.configuration.ManagementPackElementStatus]::PendingUpdate
$groupPopulateDiscovery.DataSource.Configuration = $newConfiguration
$groupPopulateDiscovery.GetManagementPack().AcceptChanges()
#---------End Sample Script-------------