From 90c98ada7cccc3f8fddfeb83cfc97409b392184d Mon Sep 17 00:00:00 2001 From: Matt Martz Date: Thu, 19 Jun 2014 10:36:53 -0500 Subject: [PATCH] Add ConvertTo-Bool filter function in powershell common code --- lib/ansible/module_utils/powershell.ps1 | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/lib/ansible/module_utils/powershell.ps1 b/lib/ansible/module_utils/powershell.ps1 index a911ea95dd5..86c3c82e5b1 100644 --- a/lib/ansible/module_utils/powershell.ps1 +++ b/lib/ansible/module_utils/powershell.ps1 @@ -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 +}