SQL Server CMS Group Duplication

A CMS is a wonderful thing, shame that the connection properties (i.e. Encrypt connection/Trust server certificate options) can't be stored, but that's another story (You'll start to reaslise when you look at msdb.dbo.sysmanagement_shared_registered_servers).

I recently had a scenario where I had multiple hierarchies of groups that I wanted to store, and have the same content under different group paths.

A bit of dirty PowerShell (i.e. I could have written it better) with #DBATools, meant that I could recreate my "Flintstones\Fred" CMS folder structure into "Flintstones\Wilma":

$CMSServer = "sqlinstance.bedrock.com"

$Source = "Fred"
$SourceServerGroupPath = "Flintstones\$Source"
$SourceServerGroupParent = Get-DbaRegServerGroup -SqlInstance $CMSServer -Group $SourceServerGroupPath 

$Destination = "Wilma"
$DestinationGroupPath = "Flintstones\$Destination"

function DBA-CMSGroupWalkthrough {
    param (
        [string]$ParentPath,
        [object[]]$ParentGroup
    )
    
    $Group = $ParentGroup | Select-Object -ExpandProperty ServerGroups

    if ($Group) {
        foreach ($ChildGroup in $Group) {
            "$ParentPath\$($ChildGroup.Name)"
            DBA-CMSGroupWalkthrough "$ParentPath\$($ChildGroup.Name)" $ChildGroup
        }
    }
}

$SourceGroups = DBA-CMSGroupWalkthrough $SourceServerGroupPath $SourceServerGroupParent

foreach ($SourceGroup in $SourceGroups) {
    $DestinationGroup = $SourceGroup.Replace($SourceServerGroupPath, $DestinationGroupPath )
    
    if (!(Get-DbaRegServerGroup -SqlInstance $CMSServer -Group $DestinationGroup)) {
        Add-DbaRegServerGroup -SqlInstance $CMSServer -Name $DestinationGroup
    }
}        

To view or add a comment, sign in

More articles by Kevin Lewis

Explore content categories