问题描述
我的代码是:
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,[email protected],$true
如何进行必要的转换?
谢谢.
推荐答案
使用-as
时要小心.仅当字符串为0
或1
时有效.
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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!