From cc800e5a7708848fcbfa2e98a47032aa0369d3be Mon Sep 17 00:00:00 2001 From: Andreas Jordan Date: Sat, 29 Aug 2026 16:21:50 +0200 Subject: [PATCH] Test-DbaLastBackup - Stop eating the caller loop on inaccessible directories The -Path branch validates DataDirectory and LogDirectory with Stop-Function -Continue, but unlike its twin checks in the per-instance loop further down, no loop encloses these two calls. Without -EnableException the continue escaped the command and consumed an iteration of whatever loop the caller was running in: the new regression test loops three times over an inaccessible DataDirectory and completed zero iterations on the unfixed command. Part of the #10638 inventory, same fix shape as #10636 and #10637: stop and return. The two correct patterns three lines above (the missing -Destination check and the connection catch) already did exactly that. Verified via the lab harness on SQL03\SQL2019: 17 tests, 0 failed; the unfixed command fails the new test with "Expected 3, but got 0". References #10638 (do Test-DbaLastBackup) Co-Authored-By: Claude Fable 5 --- public/Test-DbaLastBackup.ps1 | 10 ++++++++-- tests/Test-DbaLastBackup.Tests.ps1 | 21 +++++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) 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