Azure Quick Tip: Workaround for Azure Credentials expired errors

When using PowerShell Workflows or even in regular scripts the error Your Azure Credentials have not been set up or have expired, please run Connect-AzAccount to set up your Azure credentials may be displayed.

Azure Credentials

A workaround for this issue is to save the Azure context in a JSON file on the Azure Automation worker node using the following cmdlet.

Save-AzContext -path ((Get-Location).Path + "\file.json") -Force

When required in your code that you need to refresh the authentication, just use this cmdlet:

$tmpContext = Import-AzContext -Path ((Get-Location).path + "\file.json")

If you are using PowerShell Workflows, the following code can be used where you add the credential for every multithread instance. If you use functions that use Azure cmdlets, you may use the Import-AzContext within the function instead.

$RGs = Get-AzResourceGroup
ForEach -Parallel -ThrottleLimit 6 ($rg in $RGs){
$tmpContext = Import-AzContext -Path ((Get-Location).path + "\file.json")
<Code>
}

Leave a Comment

Your email address will not be published.

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Scroll to Top