From a0e9fb79b4848ea0a4c944c5211868b508c5ce01 Mon Sep 17 00:00:00 2001 From: Andreas Jordan Date: Thu, 27 Aug 2026 09:23:38 +0200 Subject: [PATCH 1/2] Remove-DbaAgDatabase - Pass the AvailabilityGroup filter to Get-DbaAgDatabase The parameter was declared and documented but never used, so a removal scoped to one availability group removed the database from whatever availability group it was in. The regression test scopes a removal to a second availability group and asserts the database survives. Found via static analysis by @greenmtnsun in #10607. (do Remove-DbaAgDatabase) --- public/Remove-DbaAgDatabase.ps1 | 8 +++++++- tests/Remove-DbaAgDatabase.Tests.ps1 | 25 ++++++++++++++++++++++++- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/public/Remove-DbaAgDatabase.ps1 b/public/Remove-DbaAgDatabase.ps1 index 6e336a902eac..565a94b8bc8c 100644 --- a/public/Remove-DbaAgDatabase.ps1 +++ b/public/Remove-DbaAgDatabase.ps1 @@ -110,7 +110,13 @@ function Remove-DbaAgDatabase { } if ($SqlInstance) { - $InputObject += Get-DbaAgDatabase -SqlInstance $SqlInstance -SqlCredential $SqlCredential -Database $Database + $splatGetAgDatabase = @{ + SqlInstance = $SqlInstance + SqlCredential = $SqlCredential + Database = $Database + AvailabilityGroup = $AvailabilityGroup + } + $InputObject += Get-DbaAgDatabase @splatGetAgDatabase } foreach ($db in $InputObject) { diff --git a/tests/Remove-DbaAgDatabase.Tests.ps1 b/tests/Remove-DbaAgDatabase.Tests.ps1 index 3589f131311e..74aeb8b2eb63 100644 --- a/tests/Remove-DbaAgDatabase.Tests.ps1 +++ b/tests/Remove-DbaAgDatabase.Tests.ps1 @@ -47,6 +47,18 @@ Describe $CommandName -Tag IntegrationTests { } $ag = New-DbaAvailabilityGroup @splatAvailabilityGroup + # A second availability group without any databases, to prove that a removal scoped + # to this group does not touch databases in other availability groups. + $agname2 = "dbatoolsci_removeagdb_agroup2" + $splatAvailabilityGroup2 = @{ + Primary = $TestConfig.InstanceHadr + Name = $agname2 + ClusterType = "None" + FailoverMode = "Manual" + Certificate = "dbatoolsci_AGCert" + } + $null = New-DbaAvailabilityGroup @splatAvailabilityGroup2 + # We want to run all commands outside of the BeforeAll block without EnableException to be able to test for specific warnings. $PSDefaultParameterValues.Remove("*-Dba*:EnableException") } @@ -55,7 +67,7 @@ Describe $CommandName -Tag IntegrationTests { # We want to run all commands in the AfterAll block with EnableException to ensure that the test fails if the cleanup fails. $PSDefaultParameterValues["*-Dba*:EnableException"] = $true - $null = Remove-DbaAvailabilityGroup -SqlInstance $server -AvailabilityGroup $agname + $null = Remove-DbaAvailabilityGroup -SqlInstance $server -AvailabilityGroup $agname, $agname2 $null = Get-DbaEndpoint -SqlInstance $TestConfig.InstanceHadr -Type DatabaseMirroring | Remove-DbaEndpoint $null = Remove-DbaDatabase -SqlInstance $server -Database $dbname Remove-Item -Path "$($TestConfig.Temp)\$dbname.bak", "$($TestConfig.Temp)\$dbname.trn" -ErrorAction SilentlyContinue @@ -63,6 +75,17 @@ Describe $CommandName -Tag IntegrationTests { $PSDefaultParameterValues.Remove("*-Dba*:EnableException") } + Context "honors the AvailabilityGroup filter" { + It "does not remove the database when the removal is scoped to another availability group" { + $results = Remove-DbaAgDatabase -SqlInstance $TestConfig.InstanceHadr -Database $dbname -AvailabilityGroup $agname2 + $results | Should -BeNullOrEmpty + $WarnVar | Should -BeNullOrEmpty + + $agDatabase = Get-DbaAgDatabase -SqlInstance $TestConfig.InstanceHadr -Database $dbname + $agDatabase.AvailabilityGroup | Should -Be $agname + } + } + Context "removes ag db" { It "returns removed results" { $results = Remove-DbaAgDatabase -SqlInstance $TestConfig.InstanceHadr -Database $dbname From 236bbcadbbff7466bfbf8eac00b4e4b88c451759 Mon Sep 17 00:00:00 2001 From: Andreas Jordan Date: Thu, 27 Aug 2026 10:02:13 +0200 Subject: [PATCH 2/2] Remove-DbaAgDatabase - Stop demanding availability groups the check does not require The error text told the user to specify one or more availability groups, but the condition only ever required -Database. The AvailabilityGroup parameter is an optional filter. (do Remove-DbaAgDatabase) --- public/Remove-DbaAgDatabase.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/public/Remove-DbaAgDatabase.ps1 b/public/Remove-DbaAgDatabase.ps1 index 565a94b8bc8c..079ac8c00390 100644 --- a/public/Remove-DbaAgDatabase.ps1 +++ b/public/Remove-DbaAgDatabase.ps1 @@ -98,7 +98,7 @@ function Remove-DbaAgDatabase { if ((Test-Bound -ParameterName SqlInstance)) { if ((Test-Bound -Not -ParameterName Database)) { - Stop-Function -Message "You must specify one or more databases and one or more Availability Groups when using the SqlInstance parameter." + Stop-Function -Message "You must specify one or more databases when using the SqlInstance parameter." return } }