Search code examples
azureazure-functionsazure-storageazure-powershell

Error: The Azure PowerShell context has not been properly initialized. Please import the module and try again


I have a azure powershell function app. The job of this function app is to generate azure storage account SAS token

I am using the following command to generate the token

$StartTime = Get-Date
$EndTime = $StartTime.AddMinutes(50.0)
$token = Get-AzStorageAccount -Name "<storage-account-name>" -ResourceGroupName "<resource-group-name>" | New-AzStorageContainerSASToken  -Container <container-name> -Permission rdwl -StartTime $StartTime -ExpiryTime $EndTime

The code gave the valid sas token for some days. But since some time its throwing error

The Azure PowerShell context has not been properly initialized. Please import the module and try again

I saw a few questions mentioning "'session has not been properly initialized'" and nowhere mention for context.

I need help in resolving this issue


Solution

  • This is most likely caused by https://github.com/Azure/azure-functions-powershell-worker/issues/554. The recently released Az.Accounts 2.1.* introduced this regression. Until this is fixed in the modules, the temporary workaround is to roll back to Az.Accounts 1.9.5 by using these instructions: https://github.com/Azure/azure-functions-powershell-worker/issues/552#issue-732797653. Please note that this step is critically important:

    Import-Module Az.Accounts -RequiredVersion '1.9.5'