[BUG] cmd.script when run with shell=pwsh or shell=powershell does not return expected exit codes #60884
Labels
Bug
broken, incorrect, or confusing behavior
Execution-Module
P1
Priority 1
severity-high
2nd top severity, seen by most users, causes major problems
Windows
Milestone
Description
Hi,
Exit codes returned from PowerShell scripts will never be anything other than 0 or 1 if said scripts are called via cmd.script with shell=powershell or shell=pwsh.
This seems to be happening because cmdmod.py uses "-Command" with the script as argument, when it (arguably) should use "-File" instead.
The behaviour of the "-Command" switch is by design.
The documentation for "-Command" also points this out:
"The process exit code is determined by status of the last (executed) command within the script block. The exit code is 0 when $? is $true or 1 when $? is $false. If the last command is an external program or a PowerShell script that explicitly sets an exit code other than 0 or 1, that exit code is converted to 1 for process exit code. To preserve the specific exit code, add exit $LASTEXITCODE to your command string or script block."
Replacing "-Command" with "-File" yields the expected behaviour.
Setup
Steps to Reproduce the behavior
Expected behavior
Salt should return exit code 12345
Versions Report
The text was updated successfully, but these errors were encountered: