Get-Mailbox System.OutOfMemoryException and Runbook restarts

I have a PowerShell runbook that gets all the Exchange Online mailboxes in my firm and does stuff with it.

I thought this would work without much issues, but I was mistaken. Since we have a lot of mailboxes (about 12000+) it looks like the runbook sandbox runs out of memory causing it to restart for up to 3 times and finally get shutdown.

It worked fine on my machine, but in a runbook it kept crashing the runbook and restarting it. Looks like it was hitting the runbook worker limits. I tried it in a Hybrid Runbook Worker with about 4GB RAM and that failed with an System.OutOfMemoryException error.

I tried a lot of tricks like just getting the mailbox results first into an array, and doing the processing separately. Like:

Didn’t help!

There’s no batching switch with Get-Mailbox so I couldn’t throttle it somehow. I figured the issue is the large number of mailboxes and that’s just causing issues (runbook sandboxes have a 400MB limit I think, so I am definitely hitting that causing it to be restarted; ditto with the larger Hybrid Runbook Worker which just gives an error). I could just go with a beefier Hybrid Runbook Worker but that didn’t feel right. Or I could go with a beefier Hybrid Runbook Worker, power it on just when the runbook is doing its stuff, then turn it off… but that felt like too much work and things to keep track of. I am a very lazy person.

Finally I hit upon an ingenious (hopefully!) idea. I can use the -Filter switch to get a selected number of mailboxes. So now I do the following:

I had to go with two levels as just doing with the first level (getting mailboxes whose email addresses start with a to z was still causing issues). So now I do aa, ab, ac, ad,… all the way to zz. Hopefully I don’t jinx it by saying out loud, but so far this strategy seems to be working. :)

Update: Part deux.