为什么会出现错误“提供的信息未映射到AD对象ID”使用服务主体执行New-AzRoleAssignment时?

问题描述

我在Azure DevOps管道中使用Powershell,我试图将密钥保险库的委托人的角色void*分配给存储帐户。

命令行

将Azure与服务主体连接后,将运行命令行:

[[no_unique_address]]

这是我执行的命令行:

Storage Account Key Operator Service Role

位置:

  • $credentials = New-Object -TypeName System.Management.Automation.PSCredential($servicePrincipalApplicationId,$clientSecret) Connect-AzAccount -ServicePrincipal -Credential $credentials -Tenant $tenantId 是Key Vault的pre-registered principal ID。其值为New-AzRoleAssignment -ApplicationId $keyvaultServicePrincipalId -ResourceGroupName $resourceGroupName -ResourceName $storageAccountName -ResourceType "Microsoft.Storage/storageAccounts" -RoleDeFinitionName "Storage Account Key Operator Service Role"
  • $keyvaultServicePrincipalId是存储所在的资源组的名称。其值为cfa8b339-82a2-471a-a3c9-0fc0be7a4093
  • $resourceGroupName是我的存储帐户的名称。其值为accountsmanager-test-global-rg

服务主体

以下是运行该命令的服务主体的权限:

Service Principal permissions

该命令作为在订阅中具有所有者角色的服务主体运行:

Owner in Subscription

在该订阅中创建的资源组也归该服务主体所有:

Owner in Resource Group

问题

运行命令时,出现以下错误

$storageAccountName

为什么在执行命令accountsmanagertest时出现错误New-AzRoleAssignment: The provided @R_8_4045@ion does not map to an AD object id.

解决方法

我也可以在我的身边重现这一点,有两个问题。

enter image description here

1。在您的命令中,ResourceType应该是Microsoft.Storage/storageAccounts,而不是Microsoft.Storage/storageAccount

2。在与DevOps服务连接中使用的服务主体相关的AD App的API权限中,您需要在Directory.Read.All中添加 Application权限 Azure Active Directory Graph ,而不是Microsoft Graph

enter image description here

enter image description here

enter image description here

一段时间后生效,它将正常工作。

enter image description here