0

我有一大群公用文件夹。每个文件夹至少有 3 个最多 20 个电子邮件地址,这些地址将指向该公用文件夹。

我尝试编写一个 ExchangeManagementShell 脚本来将电子邮件地址提取到 CSV 文件中。提取它们的命令执行我希望它在 powershell 窗口中执行的操作,但是当它进入 CSV 时,它只显示一行没有任何意义的文本。

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

我得到的只是

 "27c87ef9bbda4f709f6b4002fa4af63c",,,,,

重复 49 次。

任何帮助都将不胜感激。

4

1 回答 1

0

我发现这取决于你在哪里运行脚本。如果从本地 Exchange 服务器运行,则Emailaddresses属性为Microsoft.Exchange.Data.SmtpProxyAddressCollection,而远程运行时,您将收到System.Collections.ArrayList.

试试下面的代码:

    $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
于 2020-02-04T14:36:29.340 回答