diff --git a/public/Test-DbaLastBackup.ps1 b/public/Test-DbaLastBackup.ps1 index 624bd0379fc..c1b85526d77 100644 --- a/public/Test-DbaLastBackup.ps1 +++ b/public/Test-DbaLastBackup.ps1 @@ -369,7 +369,11 @@ function Test-DbaLastBackup { if ($DataDirectory) { if (-not (Test-DbaPath -SqlInstance $destserver -Path $DataDirectory)) { $serviceAccount = $destserver.ServiceAccount - Stop-Function -Message "Can't access $DataDirectory Please check if $serviceAccount has permissions." -Continue + # No -Continue here: unlike the twin check in the per-instance loop below, no loop + # encloses this call, so the continue would escape the command and eat an iteration + # of whatever loop the caller runs in. + Stop-Function -Message "Can't access $DataDirectory Please check if $serviceAccount has permissions." + return } $effectiveDataDirectory = $DataDirectory } else { @@ -379,7 +383,9 @@ function Test-DbaLastBackup { if ($LogDirectory) { if (-not (Test-DbaPath -SqlInstance $destserver -Path $LogDirectory)) { $serviceAccount = $destserver.ServiceAccount - Stop-Function -Message "$Destination can't access its local directory $LogDirectory. Please check if $serviceAccount has permissions." -Continue + # No -Continue here either, see above. + Stop-Function -Message "$Destination can't access its local directory $LogDirectory. Please check if $serviceAccount has permissions." + return } $effectiveLogDirectory = $LogDirectory } else { diff --git a/tests/Test-DbaLastBackup.Tests.ps1 b/tests/Test-DbaLastBackup.Tests.ps1 index 68ae9780de6..d86c5ea7b11 100644 --- a/tests/Test-DbaLastBackup.Tests.ps1 +++ b/tests/Test-DbaLastBackup.Tests.ps1 @@ -383,6 +383,27 @@ Describe $CommandName -Tag IntegrationTests { } } + Context "Test -Path with an inaccessible DataDirectory" { + It "Warns without eating an iteration of the caller's loop" { + # The inaccessible-directory checks 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). + $splatInaccessible = @{ + Path = $backupPath + Destination = $TestConfig.InstanceSingle + DataDirectory = "Q:\dbatoolsci\does\not\exist" + WarningAction = "SilentlyContinue" + } + $loopCount = 0 + foreach ($i in 1..3) { + $null = Test-DbaLastBackup @splatInaccessible + $loopCount++ + } + $loopCount | Should -Be 3 + $WarnVar | Should -BeLike "*Can't access*" + } + } + Context "Test a single database" { BeforeAll { $singleDbResults = Test-DbaLastBackup -SqlInstance $TestConfig.InstanceSingle -Database $testlastbackup