From b6172c389bcb387b09e39f8016f6984147c2d24b Mon Sep 17 00:00:00 2001 From: Jordan Borean Date: Tue, 10 Dec 2024 05:08:45 +1000 Subject: [PATCH] win_powershell - fix empty array result value (#711) Fix to ensure `$Ansible.Result = @()` is returned as an empty array and not null. This was a regression on a previous fix and this change goes back to the original behaviour. --- .../fragments/win_powershell-result-array.yml | 4 ++++ plugins/modules/win_powershell.ps1 | 2 +- .../targets/win_powershell/tasks/tests.yml | 13 +++++++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 changelogs/fragments/win_powershell-result-array.yml diff --git a/changelogs/fragments/win_powershell-result-array.yml b/changelogs/fragments/win_powershell-result-array.yml new file mode 100644 index 00000000..1cf52286 --- /dev/null +++ b/changelogs/fragments/win_powershell-result-array.yml @@ -0,0 +1,4 @@ +bugfixes: + - >- + win_powershell - Ensure ``$Ansible.Result = @()`` as an empty array is returned as an empty list and not null + - https://github.com/ansible-collections/ansible.windows/issues/686 diff --git a/plugins/modules/win_powershell.ps1 b/plugins/modules/win_powershell.ps1 index 9ce46908..a4d3028b 100644 --- a/plugins/modules/win_powershell.ps1 +++ b/plugins/modules/win_powershell.ps1 @@ -801,7 +801,7 @@ $OutputEncoding = [Console]::InputEncoding = [Console]::OutputEncoding = $utf8No # https://github.com/ansible-collections/ansible.windows/issues/642 $resultPipeline = [PowerShell]::Create() $resultPipeline.Runspace = $runspace - $result = $resultPipeline.AddScript('$Ansible').Invoke() + $result = $resultPipeline.AddScript('$Ansible').Invoke()[0] } finally { if (-not $processId) { diff --git a/tests/integration/targets/win_powershell/tasks/tests.yml b/tests/integration/targets/win_powershell/tasks/tests.yml index fa68f9c5..81af6f69 100644 --- a/tests/integration/targets/win_powershell/tasks/tests.yml +++ b/tests/integration/targets/win_powershell/tasks/tests.yml @@ -268,6 +268,19 @@ - result_ansible.output == [] - result_ansible.result == ['1970-01-01T00:00:00.0000000Z', 'string'] +- name: set empty array as Ansible.Result + win_powershell: + executable: '{{ pwsh_executable | default(omit) }}' + script: $Ansible.Result = @() + register: result_ansible + +- name: assert set empty array as Ansible.Result + assert: + that: + - result_ansible is changed + - result_ansible.output == [] + - result_ansible.result == [] + - name: get temporary directory win_powershell: executable: '{{ pwsh_executable | default(omit) }}'