Function Get-GmailAlias ($ID) {
$a = (Get-RemoteMailbox $ID |
Select-Object DisplayName,PrimarySmtpAddress, @{ Name="EmailAddresses"; Expression={$_.EmailAddresses | Where-Object { $_.PrefixString -ceq "smtp" } | ForEach-Object { $_.SmtpAddress }}}).EmailAddresses |
Where-Object { $_ -like '*gmail*' }
$b=@()
foreach ($mem in $a) {
$b += "'$mem'"
}
$c= $b -join ', '
return $c
}
When you call the function with a UPN the result would be as follows:
'kiran.kumar@gmail.com', 'kiran_kumar@gmail.com'
I'm trying to call the function in below cmdlet.
Set-RemoteMailbox kpv@gmail.com -EmailAddresses @{Remove=Get-GmailAlias('kpv@gmail.com')}
I get the following output:
WARNING: The command completed successfully but no settings of 'XXX/XXX/XXX/XXX/XXX/XXX/Kiran Kumar' have been modified.
I tried below methods:
>@{Remove=$(Get-GmailAlias('kpv@gmail.com'))} # Alternative-1
>$c = Get-GmailAlias('kpv@gmail.com')
@{Remove="$c"} # Alternative-2
>$c = "@{Remove = $(Get-GmailAlias('kpv@gmail.com'))}"
Set-RemoteMailbox kpv@gmail.com -EmailAddresses $c # Alternative-3
None of the above worked and also alternative-3 gave me below error:
Cannot process argument transformation on parameter 'EmailAddresses'. Cannot convert value "@{Remove = 'kiran.kumar@gmail.com', 'kiran_kumar@gmail.com'}" to type "Microsoft.Exchange.Data.ProxyAddressCollection".
Kindly assist me find out why I'm unable to execute the variable/function in -EmailAddresses.