0

I have a large group of public folders. Each folder has a minimum of 3 up to a max of 20 e-mails address that will direct to that public folder.

I have tried writing a ExchangeManagementShell script to extract the e-mail addresses to a CSV file. The command to extract them does what I want it to do in the powershell window but when it goes in to a CSV it just shows a line of text that doesn't mean anything.

Get-MailPublicFolder |Select Displayname, Emailaddresses | export-csv -Path $env:userprofile\Desktop\mail-enabled-public-folders.csv

all I get is

 "27c87ef9bbda4f709f6b4002fa4af63c",,,,,

repeated 49 times.

Any help would be appriciated.

Matt Bartlett
  • 348
  • 1
  • 3
  • 21

1 Answers1

0

I found it depends on where you run the script. If run from an on-premise Exchange server, the Emailaddresses property is a Microsoft.Exchange.Data.SmtpProxyAddressCollection, whereas running this remotely you will receive a System.Collections.ArrayList.

Try the code below:

    $result = Get-MailPublicFolder -ResultSize Unlimited -ErrorAction SilentlyContinue | 
        ForEach-Object {
            $primary = $_.PrimarySmtpAddress
            if ($_.EmailAddresses -is [System.Collections.ArrayList]) {
                # using a remote connection, this is a System.Collections.ArrayList
                # containing address strings with 'smtp:' of 'SMTP:' prefix
                $aliases = ($_.EmailAddresses | Where-Object { $_ -cmatch '^smtp:' }) | 
                           ForEach-Object { $_ -replace '^smtp:'}
            }
            else {
                # when run on an on-premise Exchange server, this is a 
                # Microsoft.Exchange.Data.SmtpProxyAddressCollection
                # where every object has these properties:
                    #   SmtpAddress        : address@company.com
                    #   AddressString      : address@company.com
                    #   ProxyAddressString : smtp:address@company.com
                    #   Prefix             : SMTP
                    #   IsPrimaryAddress   : False
                    #   PrefixString       : smtp
                $aliases = $_.EmailAddresses | 
                           Where-Object { !$_.IsPrimaryAddress -and $_.PrefixString -eq 'smtp' } | 
                           Select-Object -ExpandProperty AddressString
            }

            # output an object to be collected in variable $result
            [PsCustomObject]@{            
                DisplayName           = $_.DisplayName
                'PrimaryEmailAddress' = $primary
                'EmailAlias'          = $aliases -join '; '
            }
        }

# output on screen
$result | Format-Table -AutoSize  # or use Format-List if you like that output better

# output to CSV file
$fileOut = Join-Path -Path $env:USERPROFILE -ChildPath 'Desktop\mail-enabled-public-folders.csv'
$result | Export-Csv -Path $fileOut -Encoding UTF8 -NoTypeInformation -Force
Theo
  • 57,719
  • 8
  • 24
  • 41