1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

powershell.ps1: Add str, int, float types and ignore $null values (#21377)

* powershell.ps1: Add str type and ignore $null values

Changes include:
- New "str" type support using -type "str"
- Ensure that $null values are retained (for bool and str types)
  (Note: $null means unspecified value for parameters)
- Some minor cosmetic changes

* Also add -type "int" and -type "float"

No modules require this at the moment though.
This commit is contained in:
Dag Wieers 2017-02-24 07:41:03 +01:00 committed by Matt Davis
parent 371f9fa90d
commit f47901c3ad

View file

@ -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";
}