SMPLGRP
SMPLGRP

Reputation: 261

Select-Object PrimarySmtpAddress

Why is this piece of code not displaying the PrimarySmtpAddress?

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

Upvotes: 0

Views: 4472

Answers (1)

jbsmith
jbsmith

Reputation: 1666

Because you piped the results of Get-Mailbox into Get-MailboxStatistics.

Get-Mailbox returns Microsoft.Exchange.Data.Directory.Management.Mailbox objects, which contain the PrimarySmtpAddress property. But once you pipe those objects into another cmdlet, Get-MailboxStatistics in this case, the rest of the pipeline contains the results of that cmdlet. So after Get-MailboxStatistics runs, the pipeline contains Microsoft.Exchange.Data.Mapi.MailboxStatistics objects instead -- these objects do NOT have the PrimarySmtpAddress property. You'd need to store the PrimarySmtpAddress in a variable before passing the objects into the pipeline; something like this:

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 

Upvotes: 1

Related Questions