mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Add ConvertTo-Bool filter function in powershell common code
This commit is contained in:
parent
04d94ffb8f
commit
90c98ada7c
1 changed files with 23 additions and 0 deletions
|
@ -64,3 +64,26 @@ Function Fail-Json($obj, $message)
|
|||
echo $obj | ConvertTo-Json
|
||||
Exit 1
|
||||
}
|
||||
|
||||
# Helper filter/pipeline function to convert a value to boolean following current
|
||||
# Ansible practices
|
||||
Function ConvertTo-Bool
|
||||
{
|
||||
param(
|
||||
[parameter(valuefrompipeline=$true)]
|
||||
$obj
|
||||
)
|
||||
|
||||
$boolean_strings = "yes", "on", "1", "true", 1
|
||||
$obj_string = [string]$obj
|
||||
|
||||
if (($obj.GetType().Name -eq "Boolean" -and $obj) -or $boolean_strings -contains $obj_string.ToLower())
|
||||
{
|
||||
$true
|
||||
}
|
||||
Else
|
||||
{
|
||||
$false
|
||||
}
|
||||
return
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue