0
votes

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

2
Did you make sure to import the module after installing it? Additionally, sometimes after installing a module a simple reboot of your machine should help.hsingh

2 Answers

1
votes

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'
0
votes

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.