且构网

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

如何在此Powershell代码中将字符串转换为布尔值以进行Exchange Online?

更新时间:2023-11-07 18:52:28

使用-as时要小心.仅当字符串为01时有效.

Be careful with -as. It only works when a string is 0 or 1.

("FALSE") -as [bool][bool]("FALSE")都将返回True

***使用

[System.Convert]::ToBoolean("FALSE")
[System.Convert]::ToBoolean("False")
[System.Convert]::ToBoolean(0)

或解析

[bool]::Parse("FALSE")
[bool]::TryParse("FALSE", $outputVariable) # Will not raise an exception if the parse fails

Parse仅将字符串作为参数使用.

Parse only works with strings as parameter.