Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Add-DbaRegServerGroup #5752

Merged
merged 2 commits into from
Jun 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion functions/Add-DbaRegServerGroup.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]
}
Expand Down
12 changes: 12 additions & 0 deletions tests/Add-DbaRegServerGroup.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
}
}