From 6fd01d34be09d847baa7a73694dc633ab1791285 Mon Sep 17 00:00:00 2001 From: Jordan Borean Date: Wed, 11 Dec 2024 16:18:08 +1000 Subject: [PATCH] win_powershell - Add defensive GetType() check (#710) Some unique types may fail when calling the GetType() method. This commit adds a check to ensure the GetType() method exists before calling it to avoid an error when trying to process the output object. --- changelogs/fragments/win_powershell-type.yml | 4 ++++ plugins/modules/win_powershell.ps1 | 4 +++- 2 files changed, 7 insertions(+), 1 deletion(-) create mode 100644 changelogs/fragments/win_powershell-type.yml diff --git a/changelogs/fragments/win_powershell-type.yml b/changelogs/fragments/win_powershell-type.yml new file mode 100644 index 00000000..bb507fa4 --- /dev/null +++ b/changelogs/fragments/win_powershell-type.yml @@ -0,0 +1,4 @@ +bugfixes: + - >- + ansible.windows.win_powershell - Add extra checks to avoid ``GetType`` error when converting the output object + - ttps://github.com/ansible-collections/ansible.windows/issues/708 diff --git a/plugins/modules/win_powershell.ps1 b/plugins/modules/win_powershell.ps1 index a4d3028b..80f854f8 100644 --- a/plugins/modules/win_powershell.ps1 +++ b/plugins/modules/win_powershell.ps1 @@ -363,7 +363,9 @@ Function Convert-OutputObject { elseif (&$isType -InputObject $InputObject -Type ([switch])) { $InputObject.IsPresent } - elseif ($InputObject.GetType().IsValueType) { + # Have a defensive check to see if GetType() exists as a method on the object. + # https://github.com/ansible-collections/ansible.windows/issues/708 + elseif ('GetType' -in $InputObject.PSObject.Methods.Name -and $InputObject.GetType().IsValueType) { # We want to display just this value and not any properties it has (if any). $InputObject.PSObject.BaseObject }