convert “Yes” or “No” to boolean

限于喜欢 提交于 2019-12-05 19:50:31

One way is a switch statement:

$bool = switch ($string) {
  'yes' { $true }
  'no'  { $false }
}

Add a clause default if you want to handle values that are neither "yes" nor "no":

$bool = switch ($string) {
  'yes'   { $true }
  'no'    { $false }
  default { 'neither yes nor no' }
}

Another option might be a simple comparison:

$string -eq 'yes'            # matches just "yes"

or

$string -match '^y(es)?$'    # matches "y" or "yes"

These expressions would evaluate to $true if the string is matched, otherwise to $false.

Ah, the magic of powershell functions, and invoke expression.

function Yes { $true }
function No { $false }

$magicBool = & $answer 

Note: This is case insensitive, but will not handle misspellings

If the only possible values are "Yes" and "No" then probably the simplest way is

$result = $value -eq 'Yes'

With misspelled values and the default $false the above will do as well.

With misspelled values and the default $true this will work

$result = $value -ne 'No'

All of these are valid approaches. If you are looking for a one liner, this will validate it is an acceptable value and set to boolean true if in the 'true' value set. This will also give you a default $false value.

$result = @("true","false","yes","no") -contains $value -and @("true","yes") -contains $value

For a default $true value you would need something like so.

$result = $true

if (@("true","false","yes","no") -contains $value) {
    $result = @("true","yes") -contains $value
}

Without a full snippet of your existing code, something like this would probably be an alternative path to take, as opposed to a string of IF statements.

NOTE: This will not handle simple 'Y' or 'N' input, but is case insensitive. So, you should be able to see 'yes' or 'YES' working, as well.

$myVar = Read-Host 'What is your answer?'
switch ($myVar)
{
  Yes {$myVarConverted = $true; break}
  True {$myVarConverted = $true; break}
  No {$myVarConverted = $false; break}
  False {$myVarConverted = $false; break}
  default {"Invalid Input"; break}
}
Write-Host $myVarConverted

Please see my additional comment on your question about the 'misspelling' caveat. That's difficult to code around without specific restrictions or requirements.

Here's the way I do Yes-No answers:

function ask-user
{
   [CmdletBinding()]
   Param(
      [Parameter(Mandatory=$true)]
      [string] $question
   )
   Process
   {  $answer = read-Host $question
      if ("yes" -match $answer) {$true}
      elseif ("no" -match $answer) {$false}
      else {ask-user $question}
   }
}

You can easily substitute true and false for yes and no.
This one is case insensitive, and will match valid abbreviations. (Y or N). In the case of misspellings, it asks again. Yeah, I could have done it without recursion, but I'm lazy.

These are great solutions above, but let me just say that this whole topic just proves the vast shortcomings of Powershell...

  [System.Convert]::ToBoolean("False") -eq $true ? 
  [System.Convert]::ToBoolean("0") -eq $true ?  

Really? Give me a f--kin break.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!