Skip to content

Azure PowerShell How to run background Jobs

Vincent A (vinca) edited this page Feb 12, 2019 · 3 revisions

Azure PowerShell task sets the context in Process scope instead of user scope. This is to ensure that the context is not leaked outside the current task.

Now, the context need to be explicitly passed for all background jobs. Follow the below step to pass context to background job:

Save the context to a file and then import the context in each background job.

Save-AzureRmContext -Path "$(Agent.TempDirectory)/myContext.json"
Start-Job -Job {
    Import-AzureRmContext -Path "$(Agent.TempDirectory)/myContext.json"
    Set-AzureRmContext -SubscriptionName "<sub_name>"
    # --- your powershell commands here ---
}

[Reference]