February 7, 2019

Multithreading in PowerShell & Exchange Online (Office 365)

The details of a few (around 300) users' mailbox in Exchange Online were to be fetched, and the approach is via PowerShell. Now, the total users were over 3800, and to filter from those, consumed a lot of time. As a result, going the conventional way of getting those one-by-one took us 39 seconds per user which was around 3.5 hours for the whole script execution.  

We had to find another way to get this done. So, we chose the approach of getting multiple records simultaneously, using background processes. 

And, multi-threading came to the rescue!!

We created the connection to Exchange Online via PowerShell:
$Credentials = Get-Credential

$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri  https://outlook.office365.com/powershell-liveid/ -Credential $Credentials -Authentication  Basic -AllowRedirection

Import-PSSession $Session 

Then, we read the user display names from the file, and stored them in an array. 
$accounts = Import-CSV C:\Temp\File.csv

Now, instead of specifying the number of jobs, we would want the jobs to be automatically created.

So, we would be specifying, the batch size of each job. In our case, we chose 50 users per batch.

$accountsperBatch = 50

Then, we iterated the loop for all the accounts to be split into batches as jobs, and then started the job using Start-Job for creating the job. 

This Start-Job  needs:

1. ScriptBlock in order to job to know, what it has to do.
2. ArgumentList in order to job to know, the items using which the job is to be performed.    

On specifying this, PowerShell would begin the creation of jobs, and as the job is created, the code within the ScriptBlock is executed.  
$accountsperBatch = 50
$i = 0
$j = $accountsperBatch - 1
$batch = 1
$csvContents = @() 
while ($i -lt $accounts.Count)
 $accountBatch = $accounts[$i..$j]
 $jobName = "Batch$batch"
 $fileName = "c:\Temp\Details$jobName"
 Start-Job  -Name $jobName `
       -ScriptBlock {
         param([string[]] $accounts)
  foreach($account in $accounts)
   $mailBoxDetail = Get-Mailbox  -Identity  $account | Select  DisplayName, UserPrincipalName 
   $row =  New-Object System.Object
   $row | Add-Member -MemberType NoteProperty -Name "DisplayName" -Value  $mailBoxDetail.DisplayName
   $row | Add-Member -MemberType NoteProperty -Name "UserPrincipalName" -Value  $mailBoxDetail.UserPrincipalName

   $csvContents += $row  
                $csvContents | Export-CSV $fileName
      -ArgumentList (,$accountBatch)
 $batch += 1
 $i = $j + 1
 $j += $accountsperBatch
 if($i -gt $accounts.Count) {$i = $accounts.Count}
 if($j -gt $accounts.Count) {$j = $accounts.Count}

Then, as per the best practices, we would remove the jobs completed by using Remove-Job  and the PS Session using Remove-PSSession $Session. 

This script helped us to generate the details of the user accounts simultaneously and the results improved drastically and the results were available under 1.5 hours.

Note: If we open too many connections with Office 365, it would suspend the connections, as the security mechanism at Office 365 would consider this as a DDoS attack, thus, giving us an error like remote host not found or the connection was closed. 

If you have any questions you can reach out our SharePoint Consulting team here.

1 comment:

  1. Hi, Thanks for the script. Are the sure that the $session is being passed to the start-job? I ran through the code and the error I receive when I execute the Receive-Job is "The term 'Get-Mailbox' is not recognized as the name of a cmdlet, function, script file, or operable program". Is the code still working.