Anshul Jain
Anshul Jain

Reputation: 123

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

Upvotes: 0

Views: 802

Answers (2)

Anatoli Beliaev
Anatoli Beliaev

Reputation: 1664

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'

Upvotes: 1

Doris Lv
Doris Lv

Reputation: 3398

Yes, "the context has not been properly initialized" is different with "the session has not been properly initialized". Maybe the module had been lost after run several days.

After checking the documents about using PowerShell modules, my suggestion is use Save-Module before publishing your Function. Consider deploying your function app to an App Service plan set to always on or to a Premium plan.

A more complex solution may also work is configure the context manually.

Upvotes: 0

Related Questions