Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 3 additions & 1 deletion public/Stop-DbaExternalProcess.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ function Stop-DbaExternalProcess {
}
}
} catch {
Stop-Function -Message "Error killing $ProcessId on $ComputerName" -ErrorRecord $_ -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 "Error killing $ProcessId on $ComputerName" -ErrorRecord $_
}
}
}
17 changes: 16 additions & 1 deletion tests/Stop-DbaExternalProcess.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,19 @@ Describe $CommandName -Tag IntegrationTests {
$results.Status | Should -Be "Stopped"
}
}
}

Context "When the process does not exist" {
It "Warns without eating an iteration of the caller's loop" {
# The catch used to run Stop-Function -Continue at the end of 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). Stop-Process on a process id that does not exist throws.
$loopCount = 0
foreach ($i in 1..3) {
$null = Stop-DbaExternalProcess -ComputerName localhost -ProcessId 2147483647 -WarningAction SilentlyContinue -Confirm:$false
$loopCount++
}
$loopCount | Should -Be 3
($WarnVar -join " ") | Should -BeLike "*Error killing 2147483647*"
}
}
}