Secure string conversion to bool in Powershell

I am trying to convert the argument of my powershell script to a boolean value. This line

[System.Convert]::ToBoolean($a) 

It works fine as long as I use valid values ​​such as true or false, but when an invalid value such as bla or "is passed, an error is returned. I need something similar to TryParse, which would simply set to false if the input value is invalid and returns a boolean value indicating whether the conversion was successful or failed. For the record, I tried [boolean] :: TryParse and [bool] :: TryParse, PowerShell does not seem to recognize it.

Now I need to clumsyly handle this with two additional if statements.

What surprised me is that none of the posts about how I and the blog that I have found so far deal with invalid values. Am I missing something or are the PowerShell kids just too cool to test input?

+3
source share
5 answers

You can use the try / catch block:

 $a = "bla" try { $result = [System.Convert]::ToBoolean($a) } catch [FormatException] { $result = $false } 

gives:

 > $result False 
+9
source

TryParse should work as long as you use ref and declare the variable first:

 $out = $null if ([bool]::TryParse($a, [ref]$out)) { # parsed to a boolean Write-Host "Value: $out" } else { Write-Host "Input is not boolean: $a" } 
+9
source
 $a = 'bla' $a = ($a -eq [bool]::TrueString).tostring() $a False 
+3
source

Another possibility is to use stat switch and evaluate only True , 1 and default :

 $a = "Bla" $ret = switch ($a) { {$_ -eq 1 -or $_ -eq "True"}{$True} default{$false}} 

In this case, a string equal to True $true is returned. In all other cases, $false returned.

And another way to do this:

 @{$true="True";$false="False"}[$a -eq "True" -or $a -eq 1] 

Source Ternary Operator in PowerShell by Jon Friesen

+1
source

just searched this again and found my own answer, but as a comment, adding as a response a few corrections / other input values, as well as a test to check to see if it works as expected:

 Function ParseBool{ [CmdletBinding()] param( [Parameter(Position=0)] [System.String]$inputVal ) switch -regex ($inputVal.Trim()) { "^(1|true|yes|on|enabled)$" { $true } default { $false } } } Describe "ParseBool Testing" { $testcases = @( @{ TestValue = '1'; Expected = $true }, @{ TestValue = ' true'; Expected = $true }, @{ TestValue = 'true '; Expected = $true }, @{ TestValue = 'true'; Expected = $true }, @{ TestValue = 'True'; Expected = $true }, @{ TestValue = 'yes'; Expected = $true }, @{ TestValue = 'Yes'; Expected = $true }, @{ TestValue = 'on'; Expected = $true }, @{ TestValue = 'On'; Expected = $true }, @{ TestValue = 'enabled'; Expected = $true }, @{ TestValue = 'Enabled'; Expected = $true }, @{ TestValue = $null; Expected = $false }, @{ TestValue = ''; Expected = $false }, @{ TestValue = '0'; Expected = $false }, @{ TestValue = ' false'; Expected = $false }, @{ TestValue = 'false '; Expected = $false }, @{ TestValue = 'false'; Expected = $false }, @{ TestValue = 'False'; Expected = $false }, @{ TestValue = 'no'; Expected = $false }, @{ TestValue = 'No'; Expected = $false }, @{ TestValue = 'off'; Expected = $false }, @{ TestValue = 'Off'; Expected = $false }, @{ TestValue = 'disabled'; Expected = $false }, @{ TestValue = 'Disabled'; Expected = $false } ) It 'input <TestValue> parses as <Expected>' -TestCases $testCases { param ($TestValue, $Expected) ParseBool $TestValue | Should Be $Expected } } 
+1
source

Source: https://habr.com/ru/post/1245952/


All Articles