Select-Object PrimarySmtpAddress

时间:2013-10-02 18:12:49

标签: powershell csv exchange-server

为什么这段代码不显示PrimarySmtpAddress?

Get-Mailbox -ResultSize Unlimited | Get-MailboxStatistics | where{$_.TotalItemSize -ge 1000MB} | Sort-Object TotalItemSize -Descending | Select-Object PrimarySmtpAddress,TotalItemSize | Export-CSV mailboxsize.csv

1 个答案:

答案 0 :(得分:1)

因为您将Get-Mailbox的结果传送到Get-MailboxStatistics。

Get-Mailbox返回包含PrimarySmtpAddress属性的Microsoft.Exchange.Data.Directory.Management.Mailbox对象。但是,一旦将这些对象传递到另一个cmdlet,在这种情况下为Get-MailboxStatistics,管道的其余部分将包含该cmdlet的结果。因此,在运行Get-MailboxStatistics之后,管道包含Microsoft.Exchange.Data.Mapi.MailboxStatistics对象 - 这些对象不具有PrimarySmtpAddress属性。在将对象传递到管道之前,您需要将PrimarySmtpAddress存储在变量中;像这样的东西:

foreach ($mailbox in (Get-Mailbox -ResultSize Unlimited)) {
    $properties = @{
        PrimarySmtpAddress = $mailbox.PrimarySmtpAddress
        TotalItemSize = $mailbox | Get-MailboxStatistics | Select-Object -ExpandProperty TotalItemSize
    }
    New-Object PSObject -Property $properties
} | where{$_.TotalItemSize -ge 1000MB} | Sort-Object TotalItemSize -Descending