本文介绍将托管标识与自动化账户一起使用时可能会遇到的问题的解决方案。 有关将托管标识与自动化帐户一起使用的常规信息,请查看 Azure 自动化帐户身份验证概述。
具有系统分配的托管标识的 runbook 失败,并出现错误 unable to acquire for tenant organizations with error ManagedIdentityCredential authentication failed. Managed System Identity not found! Status 400 (Bad Request)
创建系统分配的托管标识后,尚未分配权限。
请确保为系统分配的托管标识分配适当的权限。 对 Azure 自动化帐户使用系统分配的托管标识
在 Runbook 中使用托管标识时,会收到错误:connect-azaccount : ManagedIdentityCredential authentication failed: Failed to get MSI token for account d94c0db6-5540-438c-9eb3-aa20e02e1226 and resource https://management.core.chinacloudapi.cn/. Status: 500 (Internal Server Error)
在以下情况下可能发生这种问题:
原因 1:使用尚未创建的自动化帐户系统托管标识,且
Code Connect-AzAccount -Environment AzureChinaCloud -Identity
尝试对 Azure 进行身份验证并在 Azure 或混合 Runbook 辅助角色上运行 Runbook。原因 2:自动化帐户分配有用户托管标识,而不是系统托管标识,并且
Code Connect-AzAccount -Environment AzureChinaCloud -Identity
尝试使用 Azure VM 系统托管标识在 Azure 虚拟机混合 Runbook 辅助角色上运行 Runbook。
解决方法 1:必须创建自动化帐户系统托管标识并向其授予对 Azure 资源的访问权限。
解决方法 2:根据需求,可以:
- 创建自动化帐户系统托管标识并使用它进行身份验证。
或 - 删除自动化帐户用户分配的托管标识。
- 创建自动化帐户系统托管标识并使用它进行身份验证。
想要将用户分配的托管标识添加到自动化帐户。 但无法在“自动化”边栏选项卡中找到该帐户。
如果对用户分配的托管标识没有以下权限(用于在“自动化”边栏选项卡中对其进行查看),则会出现此问题。
Microsoft.ManagedIdentity/userAssignedIdentities/*/read
Microsoft.ManagedIdentity/userAssignedIdentities/*/assign/action
注意
默认情况下,将上述权限授予托管标识操作员和托管标识参与者。
确保拥有标识操作员角色权限,以便将用户分配的托管标识添加到自动化帐户。
你的 Runbook 使用托管标识 Connect-AzAccount -Environment AzureChinaCloud -Identity,该标识尝试管理 Azure 对象,无法成功工作,并且记录以下错误 - this.Client.SubscriptionId cannot be null.
get-azvm : 'this.Client.SubscriptionId' cannot be null. At line:5 char:1 + get-azvm + ~~~~~~~~ + CategoryInfo : CloseError: (:) [Get-AzVM], ValidationException + FullyQualifiedErrorId : Microsoft.Azure.Commands.Compute.GetAzureVMCommand
在托管标识(或 Runbook 中使用的其他帐户)尚未被授予任何访问订阅的权限时,可能会发生这种情况。
请在订阅中向该托管标识(或 Runbook 中使用的其他帐户)授予相应的角色成员身份。 了解详细信息
在自动化帐户中使用用户分配的托管标识时,你会收到类似以下内容的错误:Failed to get MSI token for account a123456b-1234-12a3-123a-aa123456aa0b
。
在为自动化帐户启用系统分配的托管标识之前使用用户分配的托管标识。
为自动化帐户启用系统分配的托管标识。 然后使用用户分配的托管标识。
尝试在自动化帐户中使用托管标识时,遇到如下错误:
Connect-AzureRMAccount : An error occurred while sending the request. At line:2 char:1 + Connect-AzureRMAccount -Identity +
CategoryInfo : CloseError: (:) [Connect-AzureRmAccount], HttpRequestException + FullyQualifiedErrorId : Microsoft.Azure.Commands.Profile.ConnectAzureRmAccountCommand
导致这种情况的最常见原因是,在尝试使用标识之前未启用该标识。 若要验证这一点,请在受影响的自动化帐户中运行以下 PowerShell Runbook。
$resource= "?resource=https://management.chinacloudapi.cn/"
$url = $env:IDENTITY_ENDPOINT + $resource
$Headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$Headers.Add("X-IDENTITY-HEADER", $env:IDENTITY_HEADER)
$Headers.Add("Metadata", "True")
try
{
$Response = Invoke-RestMethod -Uri $url -Method 'GET' -Headers $Headers
}
catch
{
$StatusCode = $_.Exception.Response.StatusCode.value__
$stream = $_.Exception.Response.GetResponseStream()
$reader = New-Object System.IO.StreamReader($stream)
$responseBody = $reader.ReadToEnd()
Write-Output "Request Failed with Status: $StatusCode, Message: $responseBody"
}
如果问题是你在尝试使用标识之前未启用该标识,则会看到如下所示的结果:
Request Failed with Status: 400, Message: {"Message":"No managed identity was found for Automation account xxxxxxxxxxxx"}
必须为自动化帐户启用标识,然后才能使用托管标识服务。 请查看为 Azure 自动化帐户启用托管标识
如果本文未解决你的问题,请尝试通过以下渠道之一获取更多支持:
- 通过 Azure 论坛获取 Azure 专家的解答。
- 提出 Azure 支持事件。 转到 Azure 支持站点。