azure - 运行在 PowerShell 中编码的 Azure Functions 时出现 Connect-AzAccount 错误

标签 azure powershell azure-functions

我在尝试运行 Azure 函数(位于 PowerShell 中)时收到此错误。 PowerShell 脚本在本地运行时可以正常工作。

错误:

[Error] ERROR: No subscription found in the context.  Please ensure that the credentials you provided are authorized to access an Azure subscription, then run Connect-AzAccount to login

在requirements.ps1中:

@{
    'Az.Accounts' = '1.9.0'
    'Az.Resources' = '1.1.2'
    'AzTable' = '2.0.3'
    'Az.Storage' = '1.7.0'
    'Az.KeyVault' = '1.5.1'
}

run.ps1中,我添加了Import-Module命令:

Import-Module Az.Resources
Import-Module Az.Accounts
Import-Module Az.KeyVault
Import-Module AzTable

run.ps1”中的前几行:

$activeKey = Get-AzKeyVaultManagedStorageAccount -VaultName $keyVaultName -Name $storageAccountName

# Get Storage Account Key 
$key = (Get-AzStorageAccountKey -ResourceGroupName $rgName -Name $storageAccountName)

profile.ps1中:

if ($env:MSI_SECRET) {
    Disable-AzContextAutosave
    Connect-AzAccount -Identity
}

谢谢!

最佳答案

请确保:

  1. Enable a managed identity 。使用系统分配的身份更容易,使用用户分配的身份需要 one more step .
  2. Assign appropriate roles在身份的目标订阅上。
  3. run.ps1 代码中,调用 Set-AzContext 以选择目标订阅。

通常不需要在 run.ps1 中显式导入模块。

关于azure - 运行在 PowerShell 中编码的 Azure Functions 时出现 Connect-AzAccount 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63888082/

相关文章:

azure - 如何创建字符串来签署 blob 以在 Azure 中生成SharedAccessSignature

javascript - 在 Azure 函数中运行 Playwright

javascript - 如何连接到 Windows 8 Metro 应用程序中的任何数据库?

c# - 如何为我的应用程序创建 powershell 扩展

azure - 错误索引方法 FunctionName 未解析为值

azure - 边缘模块可以向edgeHub发送多少条消息?

powershell - ConvertTo-JSON 具有单个项目的数组

powershell - 执行 Get-AzureKeyVaultSecret 时服务主体未经授权

azure - 如何使用 Terraform 在 Azure API 管理中导入 Azure Function App?

java - 使用 Java AzureResourceManager 进行异步模板部署