Exchange Online Powershell v2- Not so fast?

So I’m really pumped this module exists. There’s finally a Exchange Online module that supports MFA that isn’t locked behind some non-standard installer buried in ECP. Nor is this module locked behind an Azure subscription. It’s published to the PowerShell Gallery like every other modern module. Even more exciting there’s new cmdlets available to speed up data collection. But do they live up the to hype?

According to Microsoft the primary goal of the new cmdlets is to speed up execution time. To reach that goal Microsoft slimmed down the returned objects for common cmdlets (get-mailbox, get-mailboxstatistics, etc). So now instead of 100s of properties get-mailbox returns 15. Get-MailboxStatistics now only returns 6 properties. You can get more, but now you have to explicitly ask for which ones you need using the -property parameter.

On paper this sounds awesome, and with some light editing you can take advantage of the new cmdlets. But first lets test it out on a simpler command.

PS C:\> Get-EXOMailBox -ResultSize Unlimited | select UserPrincipalName, RecipientTypeDetails

Here’s a relatively simple command. It’ll output the UPN and mailbox type (UserMailbox, SharedMailbox, RoomMailbox, etc.) for all mailboxes in the tenant. In my case I’m running this against a tenant with 155 mailboxes.

PS C:\> (get-mailbox -resultsize unlimited | select UserPrincipalName,RecipientTypeDetails).count
155

Not exactly the 1000s or 10,000s of accounts these new cmdlets are designed for but it will suit our needs today.

PS C:\> measure-command {get-mailbox -resultsize unlimited | select UserPrincipalName,RecipientTypeDetails}


Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 4
Milliseconds      : 908
Ticks             : 49086056
TotalDays         : 5.68125648148148E-05
TotalHours        : 0.00136350155555556
TotalMinutes      : 0.0818100933333333
TotalSeconds      : 4.9086056
TotalMilliseconds : 4908.6056

So running everything the old way it takes us just shy of 5 seconds to collect data for 155 mailboxes. Next up, lets try the new way

PS C:\> measure-command {get-exomailbox -resultsize unlimited | select UserPrincipalName,RecipientTypeDetails}


Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 7
Milliseconds      : 396
Ticks             : 73966512
TotalDays         : 8.56093888888889E-05
TotalHours        : 0.00205462533333333
TotalMinutes      : 0.12327752
TotalSeconds      : 7.3966512
TotalMilliseconds : 7396.6512

7.4 seconds. Hmmm…. That can’t be right. Can’t it? All we did is swap Get-Mailbox for the newer “faster” Get-EXOMailbox, yet it’s running ~2.5 seconds slower. Maybe we’ll see the true power of the EXO cmdlets in something more complex like a script?

#requires -module ExchangeOnlineManagement
$mailboxes = get-EXOmailbox -Resultsize unlimited
 
foreach ($mailbox in $mailboxes) {
     
    $MxboxStats = $mailbox | get-EXOmailboxstatistics
 
    if ($mailbox.ArchiveStatus -eq "Active" ) {
        $ArchiveMxboxStats = $mailbox | get-EXOmailboxstatistics -Archive
    }
    else {
        $ArchiveMxboxStats = $null
    }
    $properties = [ordered]@{
        DisplayName          = $mailbox.DisplayName
        PrimarySMTPAddress   = $mailbox.PrimarySMTPAddress
        UserPrincipalName    = $mailbox.UserPrincipalName
        Alias                = $mailbox.EmailAddresses
        RecipientTypeDetails = $mailbox.RecipientTypeDetails
        ItemCount            = $MxboxStats.ItemCount
        totalitemsize        = $MxboxStats.totalitemsize
        ArchiveItemCount     = $ArchiveMxboxStats.ItemCount
        ArchiveTotalItemSize = $ArchiveMxboxStats.totalitemsize
    }  
    $out = new-Object -Type PSCustomObject -Property $properties
    Write-Output $out
}

So lets level set real quick. This handy little script walks gets every mailbox, passes it through a foreach loop to then collect quick audit data of all the mailboxes. We grab their name, UPN, Primary email, mailbox type, number of mail items, total mailbox size, and if they have an archive total archive items, and archive size. I’ll get into this in a later post, but this thing isn’t quite ready to be a function. TL;DR I need to parameterize the mailbox input, but again that’s for another time. Lets see how long this takes to run.

PS C:\> measure-command {.\get-mailboxdatanew.ps1}


Days              : 0
Hours             : 0
Minutes           : 13
Seconds           : 41
Milliseconds      : 954
Ticks             : 8219544483
TotalDays         : 0.00951336167013889
TotalHours        : 0.228320680083333
TotalMinutes      : 13.699240805
TotalSeconds      : 821.9544483
TotalMilliseconds : 821954.4483

A little over 13 and a half minutes. Doesn’t seem all that bad, but how long does it take to run with the older cmdlets? Lets quickly edit this to use the old school get-mailbox and get-mailboxstatistics

$mailboxes = get-mailbox -Resultsize unlimited
 
foreach ($mailbox in $mailboxes) {
     
    $MxboxStats = $mailbox | get-mailboxstatistics
 
    if ($mailbox.ArchiveStatus -eq "Active" ) {
        $ArchiveMxboxStats = $mailbox | get-mailboxstatistics -Archive
    }
    else {
        $ArchiveMxboxStats = $null
    }
    $properties = [ordered]@{
        DisplayName          = $mailbox.DisplayName
        PrimarySMTPAddress   = $mailbox.PrimarySMTPAddress
        UserPrincipalName    = $mailbox.UserPrincipalName
        Alias                = $mailbox.EmailAddresses
        RecipientTypeDetails = $mailbox.RecipientTypeDetails
        ItemCount            = $MxboxStats.ItemCount
        totalitemsize        = $MxboxStats.totalitemsize
        ArchiveItemCount     = $ArchiveMxboxStats.ItemCount
        ArchiveTotalItemSize = $ArchiveMxboxStats.totalitemsize
    }  
    $out = new-Object -Type PSCustomObject -Property $properties
    Write-Output $out
}

And what do we get for our run time?

PS C:\> measure-command {.\get-mailboxdataold.ps1}


Days              : 0
Hours             : 0
Minutes           : 5
Seconds           : 9
Milliseconds      : 436
Ticks             : 3094362711
TotalDays         : 0.00358143832291667
TotalHours        : 0.08595451975
TotalMinutes      : 5.157271185
TotalSeconds      : 309.4362711
TotalMilliseconds : 309436.2711

5 Minutes and 9 seconds… well damn. Back to the drawing board then?

So it doesn’t look like we’re getting a true performance boost from running the new cmdlets. At least not in scripting. We do get some benefit in readability when using the cli since Microsoft has reduced the default number of properties in the output object. However that isn’t enough of a reason to re-write all of your scripts just yet. In it’s defense this module is still in Beta, and is very much in development. So it may improve over time.

While the new cmdlets aren’t quite ready for prime-time, IMO the module is. This new module is easier to install and maintain than all of the other options out there right now. I currently only use it because it still has access to the legacy Exchange cmdlets, and I can update it with Update-Module. So while Microsoft works out the kinks in the new commands, just use the module to connect to O365 and run all of your old scripts.

An interesting distraction
Stop Using +=, or How to Add Data to an Array

Comments

  1. Great content! Super high-quality! Keep it up! 🙂

  2. Long time supporter, and thought I’d drop a comment.

    Your wordpress site is very sleek – hope you don’t mind me asking what theme you’re
    using? (and don’t mind if I steal it? :P)

    I just launched my site –also built in wordpress like yours– but the
    theme slows (!) the site down quite a bit.

    In case you have a minute, you can find it by
    searching for “royal cbd” on Google (would appreciate any feedback) – it’s still in the works.

    Keep up the good work– and hope you all take care of yourself during the coronavirus scare!

Leave a Reply

Your email address will not be published / Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.