如何在此Powershell代码中将字符串转换为布尔值以进行Exchange Online? [英] How to convert string to boolean in this Powershell code for Exchange Online?

查看:119
本文介绍了如何在此Powershell代码中将字符串转换为布尔值以进行Exchange Online?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码是:

Import-Csv "$env:userprofile\Desktop\ExternalContacts.csv"| foreach {New-MailContact -Name $_.Name -DisplayName $_.Name -ExternalEmailAddress $_.ExternalEmailAddress -FirstName $_.FirstName -LastName $_.LastName | Set-MailContact -Identity $_.Name -HiddenFromAddressListsEnabled $_.HiddenFromAddressListsEnabled}

我得到了:

无法在参数'HiddenFromAddressListsEnabled'上处理参数转换.无法将值"System.String"转换为类型"System.Boolean",此类型的参数仅接受布尔值或数字,而应使用$ true,$ false,1或0.

Cannot process argument transformation on parameter 'HiddenFromAddressListsEnabled'. Cannot convert value "System.String" to type "System.Boolean", parameters of this type only accept booleans or numbers, use $true, $false, 1 or 0 instead.

这是在记事本中查看的CSV文件的前两行:

Here are the first two rows of my CSV file as viewed in Notepad:

Name,FirstName,LastName,ExternalEmailAddress,HiddenFromAddressListsEnabled
Ted Testington,Ted,Testington,ted.testington@blah.com,$true

如何进行必要的转换?

谢谢.

推荐答案

使用-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.

这篇关于如何在此Powershell代码中将字符串转换为布尔值以进行Exchange Online?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆