Skip to content

Commit

Permalink
(puppetlabsGH-1846) Wrap all powershell commands in a script
Browse files Browse the repository at this point in the history
Powershell will [exit 1](PowerShell/PowerShell#11461) whenever the
$LASTEXITCODE is greater than 0, regardless of what the code is,
whenever running a bare command. Powershell exits with the correct exit
code when executing files, or when `exit $LASTEXITCODE` is appended to
the command being run (as in the [execute_process
snippet](https://github.com/puppetlabs/bolt/blob/0d44ce739d4dafcb6d27abd894e230894fdd50e0/lib/bolt/shell/powershell/snippets.rb#L19),
meaning this is only an issue when running commands using the local
transport through Bolt. This modifies the local transport to append `;
exit $LASTEXITCODE` to commands run on Windows, which will correctly
return the exit code from the command run instead of changing it to 1.

Closes puppetlabs#1846

!bug

* **Return correct exit code when running commands in powershell** ([1846](puppetlabs#1846))

  Bolt will now display the correct exit code when running commands in
  powershell that exit with code > 1.
  • Loading branch information
lucywyman committed Jun 9, 2020
1 parent 0d44ce7 commit 4ba619e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/bolt/transport/local/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def copy_file(source, dest)

def execute(command)
if Bolt::Util.windows?
command += '; exit $LASTEXITCODE'
# If it's already a powershell command then invoke it normally.
# Otherwise, wrap it in powershell.exe.
unless command.start_with?('powershell.exe')
Expand Down
6 changes: 6 additions & 0 deletions spec/integration/local_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@
expect(result[0]['_error']).to be
end

it 'returns correct exit code' do
result = run_failed_nodes(%W[command run exit(2) --targets #{uri} --format json]).first
expect(result['_error']['msg']).to eq('The command failed with exit code 2')
expect(result['_error']['details']['exit_code']).to eq(2)
end

it 'runs a ruby task using bolt ruby', :reset_puppet_settings do
result = run_one_node(%w[task run sample::bolt_ruby message=somemessage] + config_flags)
expect(result['env']).to match(/somemessage/)
Expand Down

0 comments on commit 4ba619e

Please sign in to comment.