From c09f6375385d33c2b4b39fed82cda4d693e5325a Mon Sep 17 00:00:00 2001 From: Dan Tsekhanskiy Date: Sun, 12 Apr 2026 14:50:17 +0000 Subject: [PATCH] fix: Propagate native exe exit codes from PowerShell Append '; if ($LASTEXITCODE) { exit $LASTEXITCODE }' to every PowerShell -Command invocation. Without this, powershell.exe exits 0 when a native executable (msiexec, reg.exe, net.exe) fails, causing converge to treat a failed Apply as successful. Safe for pure-cmdlet commands: $LASTEXITCODE is $null when no native exe has run, so the suffix is a no-op. Ref #21 --- extensions/exec/exec.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/extensions/exec/exec.go b/extensions/exec/exec.go index a89c0ed..32d4ca4 100644 --- a/extensions/exec/exec.go +++ b/extensions/exec/exec.go @@ -35,6 +35,13 @@ var defaultPSParams = []string{"-NoProfile", "-NonInteractive", "-ExecutionPolic // corrupts stdout parsing for OnlyIfMatch and adds latency. const psPrefix = "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue'; " +// psSuffix is appended to every PowerShell -Command invocation. +// Propagates $LASTEXITCODE from native executables (msiexec, reg.exe, etc.) +// to the process exit code. Without this, powershell.exe exits 0 even when +// the native exe fails. Only fires when $LASTEXITCODE is non-null (pure +// cmdlet commands never set it). +const psSuffix = "; if ($LASTEXITCODE) { exit $LASTEXITCODE }" + // Opts configures an Exec resource. type Opts struct { Command string @@ -127,7 +134,7 @@ func (e *Exec) shellCmd(ctx context.Context, command string) *exec.Cmd { args = append(args, "/C", command) default: // PowerShell variants: wrap with error/progress preferences - args = append(args, "-Command", psPrefix+command) + args = append(args, "-Command", psPrefix+command+psSuffix) } return exec.CommandContext(ctx, binary, args...)