From c1721cbbc1c5ec2088277647b76d1db1bc1513e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A1udio=20Silva?= Date: Mon, 17 Jun 2019 10:31:20 +0100 Subject: [PATCH 1/2] Fix error when group already exists & better help --- functions/Add-DbaRegServerGroup.ps1 | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/functions/Add-DbaRegServerGroup.ps1 b/functions/Add-DbaRegServerGroup.ps1 index ac21622710..fb3d77a8f2 100644 --- a/functions/Add-DbaRegServerGroup.ps1 +++ b/functions/Add-DbaRegServerGroup.ps1 @@ -13,13 +13,14 @@ function Add-DbaRegServerGroup { Login to the target instance using alternative credentials. Windows and SQL Authentication supported. Accepts credential objects (Get-Credential) .PARAMETER Name - The name of the registered server group + The name of the registered server group. .PARAMETER Description The description for the registered server group .PARAMETER Group The SQL Server Central Management Server group. If no groups are specified, the new group will be created at the root. + You can pass sub groups using the '\' to split the path. Group\SubGroup will create both folders. Folder 'SubGroup' under 'Group' folder. .PARAMETER InputObject Allows results from Get-DbaRegServerGroup to be piped in @@ -114,6 +115,9 @@ function Add-DbaRegServerGroup { $newGroup = New-Object Microsoft.SqlServer.Management.RegisteredServers.ServerGroup($reggroup, $group) $newGroup.create() $reggroup.refresh() + } else { + Write-Message -Level Verbose -Message "Group $group already exists. Will continue." + $newGroup = $reggroup.ServerGroups[$group] } $reggroup = $reggroup.ServerGroups[$group] } From 5555615573df41b083515a98311c659d3202e7fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A1udio=20Silva?= Date: Mon, 17 Jun 2019 10:31:37 +0100 Subject: [PATCH 2/2] add two new tests (do Add-DbaRegServerGroup) --- tests/Add-DbaRegServerGroup.Tests.ps1 | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/Add-DbaRegServerGroup.Tests.ps1 b/tests/Add-DbaRegServerGroup.Tests.ps1 index 082b87b502..c28d01ef27 100644 --- a/tests/Add-DbaRegServerGroup.Tests.ps1 +++ b/tests/Add-DbaRegServerGroup.Tests.ps1 @@ -19,6 +19,7 @@ Describe "$CommandName Integration Tests" -Tag "IntegrationTests" { $group = "dbatoolsci-group1" $group2 = "dbatoolsci-group2" $description = "group description" + $descriptionUpdated = "group description updated" } AfterAll { Get-DbaRegServerGroup -SqlInstance $script:instance1 | Where-Object Name -match dbatoolsci | Remove-DbaRegServerGroup -Confirm:$false @@ -39,5 +40,16 @@ Describe "$CommandName Integration Tests" -Tag "IntegrationTests" { $results = Get-DbaRegServerGroup -SqlInstance $script:instance1 -Id 1 | Add-DbaRegServerGroup -Name dbatoolsci-first | Add-DbaRegServerGroup -Name dbatoolsci-second | Add-DbaRegServerGroup -Name dbatoolsci-third | Add-DbaRegServer -ServerName dbatoolsci-test -Description ridiculous $results.Group | Should -Be 'dbatoolsci-first\dbatoolsci-second\dbatoolsci-third' } + It "adds a registered server group and sub-group when not exists" { + $results = Add-DbaRegServerGroup -SqlInstance $script:instance1 -Name "$group\$group2" -Description $description + $results.Name | Should -Be $group2 + $results.SqlInstance | Should -Not -Be $null + } + It "updates description of sub-group when it already exists" { + $results = Add-DbaRegServerGroup -SqlInstance $script:instance1 -Name "$group\$group2" -Description $descriptionUpdated + $results.Name | Should -Be $group2 + $results.Description | Should -Be $descriptionUpdated + $results.SqlInstance | Should -Not -Be $null + } } } \ No newline at end of file