3

My code is:

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}

And I am getting:

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.

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

How can I do the necessary conversion?

Thanks.

finisterre
  • 331
  • 7
  • 19

2 Answers2

8

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

Both ("FALSE") -as [bool] and [bool]("FALSE") will return True!

It's better to use

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

Or Parse

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

Parse only works with strings as parameter.

MonkeyDreamzzz
  • 3,978
  • 1
  • 39
  • 36
1

You can also use the -as operator

-HiddenFromAddressListsEnabled ($_.HiddenFromAddressListsEnabled -as [bool])
JPBlanc
  • 70,406
  • 17
  • 130
  • 175
  • Thanks. That gives: `The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match any of the parameters that take pipeline input. – finisterre Jun 07 '13 at 12:26