diff --git a/public/Get-DbaDatabase.ps1 b/public/Get-DbaDatabase.ps1 index fe741b6209c..0f682bd763d 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 ab2889c331c..a953ea82647 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 {