$ManagementServer = ServerNameHere! $SourceGroupID = GetComputerGroupIDFromDisplayName(DisplayName of SourceGroup) $DestinationGroupID = GetComputerGroupIDFromDisplayName(WebAppName watcher computers group) if (CompareComputerGroupMembership($SourceGroupID, $DestinationGroupID) -eq 0) { SyncComputerGroupMembership($SourceGroupID, $DestinationGroupID, $ManagementServer) } function SyncComputerGroupMembership([string] $SourceGroupID, [string] $DestinationGroupID, [string] $ManagmentServer) { ## load SDK assemblies [System.Reflection.Assembly]LoadWithPartialName(Microsoft.EnterpriseManagement) [System.Reflection.Assembly]LoadWithPartialName(Microsoft.EnterpriseManagement.Configuration) [System.Reflection.Assembly]LoadWithPartialName(Microsoft.EnterpriseManagement.ConnectorFramework) [System.Reflection.Assembly]LoadWithPartialName(Microsoft.EnterpriseManagement.Monitoring) ## connect to management group $ManagementGroup = New-Object Microsoft.EnterpriseManagement.ManagementGroup($ManagementServer) ## build include list xml items from source group childs $IncludeListXML = $SourceGroupChilds = Get-ChildItem (Get-MonitoringClass where {$_.Name -eq $SourceGroupID}) Sort DisplayName foreach ($SourceGroupChild in $SourceGroupChilds) { $IncludeListXML = $IncludeListXML + ( + $SourceGroupChild.Id.ToString() + )} ## get destination group's discovery configuration $DestinationGroup = $ManagementGroup.GetMonitoringClasses($DestinationGroupID)[0] $Discovery = $DestinationGroup.GetMonitoringDiscoveries()[0] $DiscoveryConfiguration = $DestinationGroup.GetMonitoringDiscoveries()[0].DataSource.Configuration ## create new discovery configuration $NewConfiguration = if ($DiscoveryConfiguration.IndexOf() -lt 0) { ## include list present $Start = $DiscoveryConfiguration.IndexOf() + 17 $NewConfiguration = '$MPElement[Name=Windows!Microsoft.Windows.Computer]$$MPElement[Name=SC!Microsoft.SystemCenter.ComputerGroupContainsComputer]$' $NewConfiguration = $NewConfiguration + $IncludeListXML + $NewConfiguration = $DiscoveryConfiguration.Substring(0, $Start) + $NewConfiguration + $DiscoveryConfiguration.Substring($Start) } else { ## include list not present $Start = $DiscoveryConfiguration.IndexOf() + 13 $End = $DiscoveryConfiguration.IndexOf() $NewConfiguration = $DiscoveryConfiguration.Substring(0, $Start) + $IncludeListXML + $DiscoveryConfiguration.Substring($End) } ## set new discovery configuration $Discovery.Status = [Microsoft.EnterpriseManagement.Configuration.ManagementPackElementStatus]PendingUpdate $Discovery.DataSource.Configuration = $NewConfiguration $Discovery.GetManagementPack().AcceptChanges() } function GetComputerGroupIDFromDisplayName([string] $GroupDisplayName) { ## returns the ID (name property) of the computer group specified by display name return (Get-MonitoringClass where {$_.DisplayName -eq $GroupDisplayName}).Name } function CompareComputerGroupMembership([string] $GroupID1, [string] $GroupID2) { ## use this function to compare the membership of 2 computer groups ## 2 parameters, IDs of the groups to compare ## returns 1 if the membership of the groups is identical, otherwise 0 $GroupID1Members = Get-ChildItem (Get-MonitoringClass where {$_.Name -eq $GroupID1}) Sort Id $GroupID2Members = Get-ChildItem (Get-MonitoringClass where {$_.Name -eq $GroupID2}) Sort Id $GroupID1MembersString = foreach ($Member1 in $GroupID1Members) { $GroupID1MembersString = $GroupID1MembersString + + $Member1.Id.ToString() } $GroupID2MembersString = foreach ($Member2 in $GroupID2Members) { $GroupID2MembersString = $GroupID2MembersString + + $Member2.Id.ToString() } if ($GroupID1MembersString -eq $GroupID2MembersString) { return 1 } else { return 0 } }