@@ -281,8 +281,21 @@ if ($binaryItem.Attributes -band [System.IO.FileAttributes]::ReparsePoint) {
281281}
282282
283283# Prove the downloaded binary runs before touching an existing installation.
284+ # PS 5.1 wraps redirected native stderr into ErrorRecords, so under the global
285+ # ErrorActionPreference=Stop a HEALTHY binary that prints one warning while
286+ # exiting 0 becomes a terminating error here. Relax to Continue for the probe
287+ # only; failure detection stays on $LASTEXITCODE, and the pre-seed guarantees
288+ # a binary that fails to START (stale $LASTEXITCODE from an earlier native
289+ # call) can never read as success.
284290try {
285- $candidateVersion = & $DownloadedBinary -- version 2>&1
291+ $ProbeEap = $ErrorActionPreference
292+ try {
293+ $ErrorActionPreference = " Continue"
294+ $global :LASTEXITCODE = 1
295+ $candidateVersion = & $DownloadedBinary -- version 2>&1
296+ } finally {
297+ $ErrorActionPreference = $ProbeEap
298+ }
286299 if ($LASTEXITCODE -ne 0 ) { throw " candidate exited with $LASTEXITCODE " }
287300 Write-Host " Verified candidate: $candidateVersion "
288301} catch {
@@ -349,8 +362,18 @@ if (Test-Path -LiteralPath $DownloadedInstaller -PathType Leaf) {
349362}
350363
351364# Verify
365+ # Same PS 5.1 stderr-wrapping guard as the candidate probe above; the pre-seed
366+ # matters MOST here, because prior successful native calls leave a stale
367+ # $LASTEXITCODE=0 that a start-failure would otherwise inherit.
352368try {
353- $ver = & $Dest -- version 2>&1
369+ $ProbeEap = $ErrorActionPreference
370+ try {
371+ $ErrorActionPreference = " Continue"
372+ $global :LASTEXITCODE = 1
373+ $ver = & $Dest -- version 2>&1
374+ } finally {
375+ $ErrorActionPreference = $ProbeEap
376+ }
354377 if ($LASTEXITCODE -ne 0 ) { throw " installed binary exited with $LASTEXITCODE " }
355378 Write-Host " Installed: $ver "
356379} catch {
0 commit comments