Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] cmd.script when run with shell=pwsh or shell=powershell does not return expected exit codes #60884

Open
lubyou opened this issue Sep 16, 2021 · 0 comments
Assignees
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

Comments

@lubyou
Copy link
Contributor

lubyou commented Sep 16, 2021

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

# ps_test.sls
test ps exit code:
  cmd.script:
    - shell: powershell
    - source: salt://ps_exit.ps1
    - name: ps_exit.ps1
# ps_exit.ps1
$exitCode = 12345
Write-Host "Expected exit code: $exitCode"
Exit 12345

Steps to Reproduce the behavior

REM The command that salt generates is
REM C:\Windows\system32\cmd.exe /c ""powershell" -NonInteractive -NoProfile -ExecutionPolicy Bypass -Command C:\temp\2\__salt.tmp.4ygj5aqm.ps1"

C:\Users\Administrator>salt-call --local state.apply ps_test
[ERROR   ] Command '"powershell"' failed with return code: 1
[ERROR   ] stdout: Expected exit code: 12345
[ERROR   ] retcode: 1
[ERROR   ] {'pid': 3056, 'retcode': 1, 'stdout': 'Expected exit code: 12345', 'stderr': ''}
local:
----------
         ID: test ps exit code
   Function: cmd.script
       Name: ps_exit.ps1
     Result: False
    Comment: Command 'ps_exit.ps1' run
    Started: 09:20:09.926308
   Duration: 861.742 ms
    Changes:
             ----------
             pid:
                 3056
             retcode:
                 1
             stderr:
             stdout:
                 Expected exit code: 12345

Summary for local
------------
Succeeded: 0 (changed=1)
Failed:    1
------------
Total states run:     1
Total run time: 861.742 ms

Expected behavior
Salt should return exit code 12345

Versions Report

Salt Version:
          Salt: 3003.3

Dependency Versions:
          cffi: 1.14.5
      cherrypy: 18.6.0
      dateutil: 2.8.1
     docker-py: Not Installed
         gitdb: 4.0.5
     gitpython: 3.1.13
        Jinja2: 2.11.3
       libgit2: Not Installed
      M2Crypto: Not Installed
          Mako: 1.1.4
       msgpack: 1.0.2
  msgpack-pure: Not Installed
  mysql-python: Not Installed
     pycparser: 2.20
      pycrypto: Not Installed
  pycryptodome: 3.9.8
        pygit2: Not Installed
        Python: 3.7.4 (tags/v3.7.4:e09359112e, Jul  8 2019, 20:34:20) [MSC v.1916 64 bit (AMD64)]
  python-gnupg: 0.4.6
        PyYAML: 5.4.1
         PyZMQ: 18.0.1
         smmap: 3.0.4
       timelib: 0.2.4
       Tornado: 4.5.3
           ZMQ: 4.3.1

System Versions:
          dist:
        locale: cp1252
       machine: AMD64
       release: 2012ServerR2
        system: Windows
       version: 2012ServerR2 6.3.9600 SP0
@lubyou lubyou added Bug broken, incorrect, or confusing behavior needs-triage labels Sep 16, 2021
@twangboy twangboy added Execution-Module P1 Priority 1 severity-high 2nd top severity, seen by most users, causes major problems Windows and removed needs-triage labels Mar 1, 2022
@twangboy twangboy added this to the Sulphur v3006.0 milestone Mar 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
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
Projects
None yet
Development

No branches or pull requests

4 participants