且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在 PowerShell 中安全地将字符串转换为布尔值

更新时间:2021-12-04 23:15:44

你可以使用 try/catch 块:

You could use a try / catch block:

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

给予:

> $result
False