Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions public/Import-DbaSpConfigure.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,11 @@ function Import-DbaSpConfigure {
}

if (-not (Test-SqlSa -SqlInstance $sourceserver -SqlCredential $SourceSqlCredential)) {
Stop-Function -Message "Not a sysadmin on $sourceserver. Quitting." -Category PermissionDenied -Target $sourceserver -Continue
# No -Continue on these guards: the begin block has no enclosing loop, so the continue
# would escape the command, eat an iteration of whatever loop the caller runs in, and
# skip the connection cleanup in the end block.
Stop-Function -Message "Not a sysadmin on $sourceserver. Quitting." -Category PermissionDenied -Target $sourceserver
return
}

try {
Expand All @@ -151,7 +155,8 @@ function Import-DbaSpConfigure {
}

if (-not (Test-SqlSa -SqlInstance $destserver -SqlCredential $DestinationSqlCredential)) {
Stop-Function -Message "Not a sysadmin on $destserver. Quitting." -Category PermissionDenied -Target $destserver -Continue
Stop-Function -Message "Not a sysadmin on $destserver. Quitting." -Category PermissionDenied -Target $destserver
return
}

$source = $sourceserver.DomainInstanceName
Expand All @@ -170,11 +175,13 @@ function Import-DbaSpConfigure {
}

if (!(Test-SqlSa -SqlInstance $server -SqlCredential $SqlCredential)) {
Stop-Function -Message "Not a sysadmin on $server. Quitting." -Category PermissionDenied -Target $server -Continue
Stop-Function -Message "Not a sysadmin on $server. Quitting." -Category PermissionDenied -Target $server
return
}

if (-not (Test-Path $Path)) {
Stop-Function -Message "File $Path Not Found" -Category InvalidArgument -Target $Path -Continue
Stop-Function -Message "File $Path Not Found" -Category InvalidArgument -Target $Path
return
}
}

Expand Down
15 changes: 15 additions & 0 deletions tests/Import-DbaSpConfigure.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,21 @@ Describe $CommandName -Tag IntegrationTests {
$PSDefaultParameterValues.Remove("*-Dba*:EnableException")
}

Context "A missing file does not eat an iteration of the caller's loop" {
It "Warns and completes every iteration" {
# The begin block guards used to run Stop-Function -Continue without an enclosing loop -
# the continue escaped the command and consumed an iteration of this very loop, so the
# counter fell short (#10638).
$loopCount = 0
foreach ($i in 1..3) {
$null = Import-DbaSpConfigure -SqlInstance $TestConfig.InstanceSingle -Path "$exportPath\does-not-exist.sql" -WarningAction SilentlyContinue
$loopCount++
}
$loopCount | Should -Be 3
$WarnVar | Should -BeLike "*Not Found*"
}
}

Context "The connection of the caller is left alone when importing from a file (#10554)" {
BeforeAll {
$PSDefaultParameterValues["*-Dba*:EnableException"] = $true
Expand Down