diff --git a/lib/ansible/module_utils/powershell.ps1 b/lib/ansible/module_utils/powershell.ps1 index 310536c784c..ae8b10d221e 100644 --- a/lib/ansible/module_utils/powershell.ps1 +++ b/lib/ansible/module_utils/powershell.ps1 @@ -130,9 +130,15 @@ Function Add-DeprecationWarning($obj, $message, $version = $null) } } +# Helper function to expand environment variables in values. By default +# it turns any type to a string, but we ensure $null remains $null. Function Expand-Environment($value) { - [System.Environment]::ExpandEnvironmentVariables($value) + if ($value -ne $null) { + [System.Environment]::ExpandEnvironmentVariables($value) + } else { + $value + } } # Helper function to get an "attribute" from a psobject instance in powershell. @@ -192,12 +198,22 @@ Function Get-AnsibleParam($obj, $name, $default = $null, $resultobj = @{}, $fail } + # If $value -eq $null, the parameter was unspecified if ($value -ne $null -and $type -eq "path") { - # Expand environment variables on path-type (Beware: turns $null into "") + # Expand environment variables on path-type $value = Expand-Environment($value) - } elseif ($type -eq "bool") { + } elseif ($value -ne $null -and $type -eq "str") { + # Convert str types to real Powershell strings + $value = $value.ToString() + } elseif ($value -ne $null -and $type -eq "bool") { # Convert boolean types to real Powershell booleans $value = $value | ConvertTo-Bool + } elseif ($value -ne $null -and $type -eq "int") { + # Convert int types to real Powershell integers + $value = $value -as [int] + } elseif ($value -ne $null -and $type -eq "float") { + # Convert float types to real Powershell floats + $value = $value -as [float] } return $value @@ -261,7 +277,7 @@ Function Parse-Args($arguments, $supports_check_mode = $false) # and above can handle: Function Get-FileChecksum($path, $algorithm = 'sha1') { - If (Test-Path -PathType Leaf $path) + If (Test-Path -Path $path -PathType Leaf) { switch ($algorithm) { @@ -270,7 +286,7 @@ Function Get-FileChecksum($path, $algorithm = 'sha1') 'sha256' { $sp = New-Object -TypeName System.Security.Cryptography.SHA256CryptoServiceProvider } 'sha384' { $sp = New-Object -TypeName System.Security.Cryptography.SHA384CryptoServiceProvider } 'sha512' { $sp = New-Object -TypeName System.Security.Cryptography.SHA512CryptoServiceProvider } - default { Fail-Json (New-Object PSObject) "Unsupported hash algorithm supplied '$algorithm'" } + default { Fail-Json @{} "Unsupported hash algorithm supplied '$algorithm'" } } If ($PSVersionTable.PSVersion.Major -ge 4) { @@ -282,7 +298,7 @@ Function Get-FileChecksum($path, $algorithm = 'sha1') $fp.Dispose(); } } - ElseIf (Test-Path -PathType Container $path) + ElseIf (Test-Path -Path $path -PathType Container) { $hash = "3"; }