From 182d70625878953d781c39ab55796779458867f8 Mon Sep 17 00:00:00 2001 From: Andreas Jordan Date: Sat, 12 Sep 2026 15:25:38 +0200 Subject: [PATCH] Export-DbaDacPackage - Stop eating the caller loop when only ExcludeDatabase is given Stop-Function -Continue runs PowerShell's continue. No loop encloses this call site inside the command, so the continue unwound out of the command and consumed an iteration of whatever loop the caller runs in: a user's foreach silently skipped an element, and Pester's runner corrupted. The escape only bites the non-EnableException path; with EnableException Stop-Function throws before it gets there. Part of #10638 (do Export-DbaDacPackage) Co-Authored-By: Claude Fable 5.1 --- public/Export-DbaDacPackage.ps1 | 5 ++++- tests/Export-DbaDacPackage.Tests.ps1 | 17 ++++++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) 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*" + } + } +}