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

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

我有一个 azure powershell 函数应用程序。此函数应用程序的工作是生成 azure storage account SAS 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

该代码提供了几天的有效 sas 令牌。但是一段时间以来它的抛出错误

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

我看到几个问题提到“'session 尚未正确初始化'”,但没有提及上下文。

我需要帮助解决这个问题

是的,"the context has not been properly initialized""the session has not been properly initialized" 不同。也许模块在 运行 几天后丢失了。

在检查了关于使用 PowerShell 模块的 documents 之后,我的建议是在发布函数之前使用 Save-Module。考虑将您的函数应用程序部署到设置为 始终开启App Service 计划Premium 计划

更复杂的解决方案也可能有效 configure the context manually

这很可能是由 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 引起的。请注意这一步非常重要:

Import-Module Az.Accounts -RequiredVersion '1.9.5'