PowerShell Background Jobs and Office 365

I haven’t done a whole lot of work with PowerShell background jobs in the past and I was curious to know how to run them against an Office 365 environment. My current interest in background jobs has to do with the fact that jobs are forked to their own thread and in a GUI environment, using jobs allows your form to run multiple tasks simultaneously without freezing up.

Based on my testing, it seems that even if you have already run the Connect-MsolService cmdlet in your session, this connection to Office 365 is not used by your background job as it runs on its own thread. As such, you need to be able to pass a credential to your job for it to be able to run commands against your Office 365 environment.

The example below shows the correct way to connect to Office 365 using PowerShell jobs.

$o365cred = Get-Credential
$scriptBlock = {
param($cred)
Connect-MsolService -Credential $cred
Get-MsolUser
}

Start-Job -ScriptBlock $scriptBlock -Name 'GetUsers' -ArgumentList $o365cred

First, you need an object to hold the script block you intend to execute. The three main components of the script block are:

  1. The parameters. The parameter block must include a credential parameter to be used for connecting to your Office 365 environment. In this case, $cred.
  2. The Connect-MsolUser cmdlet. This command must be executed before any other Msol cmdlet is used in your script block in order to connect the job to your Office 365 environment. Set the -credential parameter to $cred. Doing so will ensure that the credential that you used in the job’s argument list is used to connect to Msol.
  3. The command or list of commands you intend to run. Here, I’ve simply used the Get-MsolUser cmdlet to return a list of users.

Next, you’ll need a credential for connecting to Office 365. In this case, I used Get-Credential to create a new PSCredential.

Finally, we can create the background job. To do this, we use the Start-Job cmdlet. Set the ScriptBlock Parameter to your script block ($scriptBlock), the Name parameter to useful name of your choosing (‘GetUsers’), and the ArgumentList parameter to the name of your PSCredential object ($o365cred).

Alternatively, you can type this out on a single line if you so desire.

Start-Job -ScriptBlock {param($cred) ;Connect-MsolService -Credential $cred ;Get-MsolUser} -Name 'GetUsers' -ArgumentList (Get-Credential)

In order to check the status of our example job, simply type the following:

Get-Job -Name 'GetUsers'

If the commands you ran in the script block have any output as is the case with our command, this output can be captured using the Receive-Job cmdlet as shown below:

$UserList = Receive-Job -Name 'GetUsers'

Doing this will save the job’s output for future reference. If you use the Receive-Job cmdlet to retrieve the content of a job without saving it to a variable, it will be lost, since the content of a job can only be retrieved once unless the -keep switch is used. However, this should be avoided. The -keep switch is meant to be used to view the output of a job while it is still running without removing it from its place in memory. Once a job has been completed, all of its output should be moved to a new object.

So there you have it, the quick and easy way to run background jobs in PowerShell for Office 365 management.

What Office 365 tasks do you think would make good background jobs?

If you have any questions or comments, leave a message below. Thanks for reading.

Leave a Comment