【问题标题】:Error: The Azure PowerShell context has not been properly initialized. Please import the module and try again错误:尚未正确初始化 Azure PowerShell 上下文。请导入模块并重试
【发布时间】:2021-02-12 14:05:54
【问题描述】:

我有一个 azure powershell 函数应用程序。此函数应用的工作是生成 Azure 存储帐户 SAS 令牌

我正在使用以下命令生成令牌

$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 令牌。但是由于一段时间以来它的抛出错误

Azure PowerShell 上下文未正确初始化。请导入模块并重试

我看到一些问题提到“'session 没有被正确初始化'”并且没有提到上下文。

我需要帮助解决这个问题

【问题讨论】:

  • 安装后确定导入模块了吗?此外,有时在安装模块后简单地重新启动机器应该会有所帮助。

标签: azure azure-functions azure-storage azure-powershell


【解决方案1】:

这很可能是由https://github.com/Azure/azure-functions-powershell-worker/issues/554 引起的。最近发布的 Az.Accounts 2.1.* 引入了这种回归。在模块中解决此问题之前,临时解决方法是使用以下说明回滚到 Az.Accounts 1.9.5:https://github.com/Azure/azure-functions-powershell-worker/issues/552#issue-732797653。请注意,这一步非常重要:

Import-Module Az.Accounts -RequiredVersion '1.9.5'

【讨论】:

    【解决方案2】:

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

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

    configure the context manually 也可以使用更复杂的解决方案。

    【讨论】:

    • 我尝试使用 Save-Module 并将模块 Az.Account 和 Az.Storage 保存到模块中,并将其导入到运行文件中。这似乎在本地工作得很好。在部署时,我使用路径 D:\home\site\wwwroot 作为前缀,因为那是部署 azure 函数的位置。但这并没有解决"the context has not been properly initialized"的错误。
    • 调试时我尝试使用Get-AzContext 命令获取上下文。这也在本地唤醒,而在部署版本中它说命令无法识别
    猜你喜欢
    • 2020-09-29
    • 2020-04-06
    • 1970-01-01
    • 2017-06-06
    • 2021-08-17
    • 2016-05-31
    • 1970-01-01
    • 2021-01-10
    • 1970-01-01
    相关资源
    最近更新 更多