From c86794ace227ab623da46b0400fbf7922f05df85 Mon Sep 17 00:00:00 2001 From: Jordan Borean Date: Wed, 29 Aug 2018 10:12:29 +1000 Subject: [PATCH] win_psexec: fix arg handling when command contains multiple args (#44797) (cherry picked from commit 9d916077546e953bfdd8654ee5502a9f325b78eb) --- changelogs/fragments/win_psexec_command_arg.yaml | 2 ++ lib/ansible/modules/windows/win_psexec.ps1 | 5 ++++- test/integration/targets/win_psexec/tasks/main.yml | 12 ++++++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 changelogs/fragments/win_psexec_command_arg.yaml diff --git a/changelogs/fragments/win_psexec_command_arg.yaml b/changelogs/fragments/win_psexec_command_arg.yaml new file mode 100644 index 00000000000..cb4cc43eadf --- /dev/null +++ b/changelogs/fragments/win_psexec_command_arg.yaml @@ -0,0 +1,2 @@ +bugfixes: +- win_psexec - changed code to not escape the command option when building the args - https://github.com/ansible/ansible/issues/43839 diff --git a/lib/ansible/modules/windows/win_psexec.ps1 b/lib/ansible/modules/windows/win_psexec.ps1 index 1dc43c4a433..9d1aa1ab9be 100644 --- a/lib/ansible/modules/windows/win_psexec.ps1 +++ b/lib/ansible/modules/windows/win_psexec.ps1 @@ -106,10 +106,13 @@ If ($extra_opts) { } $arguments += "-accepteula" -$arguments += $command $argument_string = Argv-ToString -arguments $arguments +# add the command at the end of the argument string, we don't want to escape +# that as psexec doesn't expect it to be one arg +$argument_string += " $command" + $start_datetime = [DateTime]::UtcNow $result.psexec_command = "$executable $argument_string" diff --git a/test/integration/targets/win_psexec/tasks/main.yml b/test/integration/targets/win_psexec/tasks/main.yml index 224cbb3c0fa..a1245cdc316 100644 --- a/test/integration/targets/win_psexec/tasks/main.yml +++ b/test/integration/targets/win_psexec/tasks/main.yml @@ -67,6 +67,18 @@ # - whoami_as_elevated.rc == 0 # - whoami_as_elevated.stdout|lower == '{{ ansible_hostname|lower }}\{{ ansible_user_id|lower }}' +- name: Run command with multiple arguments + win_psexec: + command: powershell.exe -NonInteractive "exit 1" + ignore_errors: yes + register: whoami_multiple_args + +- name: Test command with multiple argumetns + assert: + that: + - whoami_multiple_args.rc == 1 + - whoami_multiple_args.psexec_command == "psexec.exe -accepteula powershell.exe -NonInteractive \"exit 1\"" + - name: Remove sysinternals from PATH win_path: name: PATH