From fa81621879cd8e79a6debb4b08250abfb9702218 Mon Sep 17 00:00:00 2001 From: Andreas Jordan Date: Sat, 29 Aug 2026 16:01:42 +0200 Subject: [PATCH] Get-DbaDatabase - Stop eating the caller loop on invalid parameter combination The begin block validation for -ExcludeUser plus -ExcludeSystem called Stop-Function -Continue, but no loop encloses that call, so without -EnableException the continue escaped the command and consumed an iteration of whatever loop the caller was running in. A script looping over instances with that parameter mistake lost every single iteration: the new regression test measures exactly that - a foreach over three elements completed zero of them on the unfixed command. Same defect class as the two sites fixed in Restore-DbaDatabase (#10636). The call now stops and returns; Test-FunctionInterrupt in the process block already handles the rest. Found by an AST sweep over all 1591 Stop-Function -Continue sites for calls without an enclosing loop or switch; this is the first fix of that inventory, chosen because Get-DbaDatabase is the most used command in the module. Verified via the lab harness on SQL03\SQL2019: 17 tests, 0 failed; the unfixed command fails the new test with "Expected 3, but got 0". (do Get-DbaDatabase) Co-Authored-By: Claude Fable 5 --- public/Get-DbaDatabase.ps1 | 5 ++++- tests/Get-DbaDatabase.Tests.ps1 | 15 +++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/public/Get-DbaDatabase.ps1 b/public/Get-DbaDatabase.ps1 index fe741b6209c0..0f682bd763d0 100644 --- a/public/Get-DbaDatabase.ps1 +++ b/public/Get-DbaDatabase.ps1 @@ -244,7 +244,10 @@ function Get-DbaDatabase { begin { if ($ExcludeUser -and $ExcludeSystem) { - Stop-Function -Message "You cannot specify both ExcludeUser and ExcludeSystem." -Continue -EnableException $EnableException + # No -Continue here: the begin block has no enclosing loop, so the continue would escape + # the command and eat an iteration of whatever loop the caller runs in. + Stop-Function -Message "You cannot specify both ExcludeUser and ExcludeSystem." -EnableException $EnableException + return } } diff --git a/tests/Get-DbaDatabase.Tests.ps1 b/tests/Get-DbaDatabase.Tests.ps1 index ab2889c331c9..a953ea826478 100644 --- a/tests/Get-DbaDatabase.Tests.ps1 +++ b/tests/Get-DbaDatabase.Tests.ps1 @@ -58,6 +58,21 @@ Describe $CommandName -Tag IntegrationTests { } } + Context "When ExcludeUser and ExcludeSystem are combined" { + It "Warns without eating an iteration of the caller's loop" { + # The invalid combination used to run Stop-Function -Continue in the begin block, where no + # loop encloses it - the continue escaped the command and consumed an iteration of this + # very loop, so the counter stayed at zero. + $loopCount = 0 + foreach ($i in 1..3) { + $null = Get-DbaDatabase -SqlInstance $TestConfig.InstanceSingle -ExcludeUser -ExcludeSystem -WarningAction SilentlyContinue + $loopCount++ + } + $loopCount | Should -Be 3 + $WarnVar | Should -BeLike "*You cannot specify both ExcludeUser and ExcludeSystem*" + } + } + } Describe $CommandName -Tag IntegrationTests {