diff --git a/public/Export-DbaDacPackage.ps1 b/public/Export-DbaDacPackage.ps1 index 07ca956c224..9ee551b16b2 100644 --- a/public/Export-DbaDacPackage.ps1 +++ b/public/Export-DbaDacPackage.ps1 @@ -165,7 +165,10 @@ function Export-DbaDacPackage { #check that at least one of the DB selection parameters was specified if (!$AllUserDatabases -and !$Database) { - Stop-Function -Message "Either -Database or -AllUserDatabases should be specified" -Continue + # No -Continue here: this block has no enclosing loop, so the continue would escape the command + # and eat an iteration of whatever loop the caller runs in (#10638). + Stop-Function -Message "Either -Database or -AllUserDatabases should be specified" + return } #Check Option object types - should have a specific type if ($Type -eq 'Dacpac') { diff --git a/tests/Export-DbaDacPackage.Tests.ps1 b/tests/Export-DbaDacPackage.Tests.ps1 index 2a70454ed0f..2efc52d0977 100644 --- a/tests/Export-DbaDacPackage.Tests.ps1 +++ b/tests/Export-DbaDacPackage.Tests.ps1 @@ -240,4 +240,19 @@ Describe $CommandName -Tag IntegrationTests { } } } -} \ No newline at end of file + + Context "When only ExcludeDatabase is given" { + It "Warns without eating an iteration of the caller's loop" { + # The database selection check used to run Stop-Function -Continue in the process 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 (#10638). The check fires before any connection is made. + $loopCount = 0 + foreach ($i in 1..3) { + $null = Export-DbaDacPackage -SqlInstance $TestConfig.InstanceSingle -ExcludeDatabase dbatoolsci_none -WarningAction SilentlyContinue + $loopCount++ + } + $loopCount | Should -Be 3 + ($WarnVar -join " ") | Should -BeLike "*Either -Database or -AllUserDatabases should be specified*" + } + } +}