简体   繁体   中英

Powershell script with Get-Mailbox and Get-MailboxStatistics missing output

I'm using a powershell script to determine the size of the mailboxes of users in my office 365 tenant. This works almost fine but only for the first returned item the e-mailaddress of the user is not shown. In this cae it should be: username1@contoso.com But it stays blank

Why?

Get-Mailbox -ResultSize Unlimited | Where {$_.EmailAddresses -like 
"*@contoso.com"} | Get-MailboxStatistics |
Select-Object -Property @{label=”User”;expression={$_.DisplayName}},
@{label="Emailaddress";e={(get-mailbox $_.legacyDN).PrimarySMTPAddress}},
@{label=”Total Messages”;expression= {$_.ItemCount}},
@{label=”Total Size (MB)”;expression={[math]::Round(`
($_.TotalItemSize.ToString().Split("(")[1].Split(" ")[0].Replace(",","")/1MB),2)}}

The result is the following output. Missing the e-mailaddress on the first line.

User        Emailaddress             Total Messages Total Size (MB)
----        ------------             -------------- ---------------
Username 1                                    15977         2844,87
Username 2  username2@contoso.com               546            3,41

You could use a Foreach-Object loop and a custom object to achive what you need like this:

 Get-Mailbox -Filter "EmailAddresses -like '*@contoso.com'" -ResultSize Unlimited | 
    ForEach-Object{
        $Statistics = Get-MailboxStatistics -Identity $_.sAMAccountName
        [PSCustomObject]@{
            User = $_.DisplayName
            Emailaddress = $_.PrimarySMTPAddress
            'Total Messages' = $Statistics.ItemCount
            'Total Size (MB)' = [math]::Round(($Statistics.TotalItemSize.ToString().Split("(")[1].Split(" ")[0].Replace(",","")/1MB),2)
        }
    }

It could be even a little bit faster because you don't need to run your Get-Mailbox cmdlet twice.

To stop receiving warning or error, please use these steps

$WarningAction ='Ignore'
$WarningAction = 'SilentlyContinue'
$ErrorActionPreference = 'SilentlyContinue'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM