2
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.

G42
  • 9,791
  • 2
  • 19
  • 34
  • `Set-RemoteMailbox kpv@gmail.com -EmailAddresses @{Remove="kiran.kumar@gmail.com","kiran_kumar@gmail.com"}` - do you get the same error for this? – G42 Feb 25 '18 at 10:03
  • @gms0ulman It works; I could use a looping technique to remove the smtps I want but I wanna know why I'm unable to execute the variable in the cmdlet. – LittleBuddha Feb 25 '18 at 21:33
  • Any luck if you use double quotes and delimit using instead of ? `"'$mem'"` -> `"""$mem"""` and `$c= $b -join ', '` -> `$c= $b -join ','`. No access to Exchange server to test. – G42 Feb 26 '18 at 19:12

0 Answers0