fix(zdeploy): -Scan must not block on a redirected stdin

Read-Host throws under -NonInteractive - already caught - but with stdin merely
redirected (a pipe, a scheduled task, powershell.exe launched from another
shell) it blocks, waiting on a pipe that never answers. The first scan run that
way hung for ten minutes with its table already printed. The test is
[Console]::IsInputRedirected, checked before the prompt.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
KellyMichels 2026-09-02 16:40:30 -05:00
parent ee1a94e5fa
commit bd45848dea

View File

@ -303,11 +303,19 @@ if ($Scan) {
Write-Host "" Write-Host ""
if (-not $Yes) { if (-not $Yes) {
# Read-Host throws in a -NonInteractive host, and the raw exception # Two different ways a prompt can have nobody to answer it, and they
# reads as "the scan crashed" rather than "nobody could answer the # fail differently:
# question". Caught rather than predicted: [Environment]::UserInteractive # - a -NonInteractive host: Read-Host THROWS. Caught below.
# is still $true under -NonInteractive, so asking first does not work - # - redirected stdin (a pipe, a scheduled task, powershell.exe launched
# only attempting it tells the truth. Scheduled tasks land here. # from another shell): Read-Host does NOT throw - it BLOCKS, waiting
# on a pipe that never answers. The first scan run this way sat for
# ten minutes with its table already printed but withheld behind the
# blocked pipeline. [Environment]::UserInteractive is $true in both
# cases, so it cannot be the test; IsInputRedirected can.
if ([Console]::IsInputRedirected) {
Write-Host " stdin is not a terminal - cannot prompt. Re-run with -Yes to deploy these $($pending.Count).`n" -ForegroundColor Yellow
Stop-ZTracking; exit 0
}
$answer = $null $answer = $null
try { $answer = Read-Host " Deploy these $($pending.Count)? [y/N]" } try { $answer = Read-Host " Deploy these $($pending.Count)? [y/N]" }
catch { catch {