powershellexchange-serverpowershell-2.0exchange-server-2010

Is it possible just to extract the SMTP addresses from a public folder


I'm trying to extract a list of SMTP addresses associated with some of our Exchange 2010 public folders using the get-mailpublicfolder command

While I can extract the WindowsEmailAddress and EmailAddresses values, the latter contains X400 addresses that I'd like to exclude.

The closest I've been able to find in relation to this is the code listed in the following link

https://unlockpowershell.wordpress.com/2010/01/27/powershell-get-mailbox-display-smtp-addresses/

But this appears to only work for Get-Mailbox as the following code returns the following errors

$data=Get-MailPublicFolder -Identity $id |select-object WindowsEmailAddress,@{Name="EmailAddresses";Expression={$_.EmailAddresses}|Where-Object {$_.PrefixString -ceq "smtp:"}}| ForEach-Object {$_.SmtpAddress}

 

Select-Object : Key "Expression" has illegal type System.Management.Automation.PSObject; expected types are {System.Str

ing, System.Management.Automation.ScriptBlock}.

At C:\Temp\FindAllEmailAdresses_v1.ps1:33 char:56

+ $data=Get-MailPublicFolder -Identity $id |select-object <<<<  WindowsEmailAddress,@{Name="EmailAddresses";Expression=

{$_.EmailAddresses}|Where-Object {$_.PrefixString -ceq "smtp:"}}| ForEach-Object {$_.SmtpAddress}

    + CategoryInfo          : InvalidArgument: (:) [Select-Object], NotSupportedException

    + FullyQualifiedErrorId : DictionaryKeyIllegalValue1,Microsoft.PowerShell.Commands.SelectObjectCommand

Select-Object : Key "Expression" has illegal type System.Management.Automation.PSObject; expected types are {System.Str

ing, System.Management.Automation.ScriptBlock}.

OR

$data=Get-MailPublicFolder -Identity $id |select WindowsEmailAddress,@{Name="EmailAddresses";Expression={$_.EmailAddresses}|Where-Object {$_.PrefixString -ceq "smtp:"}}

 

+ $data=Get-MailPublicFolder -Identity $id |select <<<<  WindowsEmailAddress,@{Name="EmailAddresses";Expression={$_.

ilAddresses}|Where-Object {$_.PrefixString -eq "smtp:"}}

    + CategoryInfo          : InvalidArgument: (:) [Select-Object], NotSupportedException

    + FullyQualifiedErrorId : DictionaryKeyIllegalValue1,Microsoft.PowerShell.Commands.SelectObjectCommand

Select-Object : Key "Expression" has illegal type System.Management.Automation.PSObject; expected types are {System.

ing, System.Management.Automation.ScriptBlock}.

At C:\Temp\FindAllEmailAdresses_v1.ps1:31 char:49

Is it possible to just extract the SMTP addresses or am I stuck with extracting the x400 addresses also? Thanks in advance

EDIT

Based on the answer, I've changed the code to the following and it works with one change, which is to remove the colon from the -ceq "smtp:" section, as leaving it in place results in the email addresses not being returned. Note this is my error not Dougs as they simply based the code on what I'd previously provided.

$expression = {$_.EmailAddresses | Where-Object {$_.PrefixString -ceq "smtp"} | ForEach-Object {$_.SmtpAddress}}

$selectprops = 'WindowsEmailAddress',
               @{Name="EmailAddresses"; Expression=$expression}

$data = Get-MailPublicFolder -Identity $id | Select-Object $selectprops

Solution

  • This is where formatting/indentation can save you tons of time and frustration. The error you're getting is saying that your expression (e={} part of your calculated property) can only be a string or scriptblock. Simply put, both commands have a curly brace in the wrong spot. One real easy way to avoid this is to store chunks of code in variables.

    Here are the original lines with the braces moved.

    $data = Get-MailPublicFolder -Identity $id |select-object WindowsEmailAddress,@{Name="EmailAddresses"; Expression={$_.EmailAddresses | Where-Object {$_.PrefixString -ceq "smtp:"}| ForEach-Object {$_.SmtpAddress}}}
    

    and

    $data = Get-MailPublicFolder -Identity $id |select WindowsEmailAddress,@{Name="EmailAddresses";Expression={$_.EmailAddresses|Where-Object {$_.PrefixString -ceq "smtp:"}}}
    

    Here is what I really recommend

    $expression = {$_.EmailAddresses | Where-Object {$_.PrefixString -ceq "smtp:"} | ForEach-Object {$_.SmtpAddress}}
    
    $selectprops = 'WindowsEmailAddress',
                   @{Name="EmailAddresses"; Expression=$expression}
    
    $data = Get-MailPublicFolder -Identity $id | Select-Object $selectprops
    

    or

    $expression = {$_.EmailAddresses | Where-Object {$_.PrefixString -ceq "smtp:"}}
    
    $selectprops = 'WindowsEmailAddress',
                   @{Name="EmailAddresses";Expression=$expression}
    
    $data = Get-MailPublicFolder -Identity $id | Select-Object $selectprops
    

    NOTE

    I left the powershell 2.0 code formatting in case you're stuck in an older version. If it's 5.1 then this could be tidied up a bit more.

    $expression = {$_.EmailAddresses | Where-Object PrefixString -ceq "smtp:" | ForEach-Object SmtpAddress}
    
    $selectprops = 'WindowsEmailAddress',
                   @{Name="EmailAddresses"; Expression=$expression}
    
    $data = Get-MailPublicFolder -Identity $id | Select-Object $selectprops
    

    or

    $expression = {$_.EmailAddresses | Where-Object PrefixString -ceq "smtp:"}
    
    $selectprops = 'WindowsEmailAddress',
                   @{Name="EmailAddresses";Expression=$expression}
    
    $data = Get-MailPublicFolder -Identity $id | Select-Object $selectprops