使用 Azure PowerShell 列出 Azure 角色分配

Azure 基于角色的访问控制(Azure RBAC) 是用于管理对 Azure 资源的访问的授权系统。 若要确定用户、组、服务主体或托管标识有权访问的资源,请列出其角色分配。 本文介绍如何使用 Azure PowerShell 列出角色分配。

注释

建议使用 Azure Az PowerShell 模块与 Azure 交互。 若要开始,请参阅安装 Azure PowerShell。 若要了解如何迁移到 Az PowerShell 模块,请参阅 将 Azure PowerShell 从 AzureRM 迁移到 Az

注释

如果你的组织已将管理功能外包给使用 Azure Lighthouse 的服务提供商,则此处将不会显示该服务提供商授权的角色分配。 同样,无论已分配有什么角色,服务提供商租户中的用户都不会看到用户在客户租户中的角色分配。

先决条件

列出当前订阅的角色分配

获取当前订阅中所有角色分配的列表(包括根和管理组中继承的角色分配)的最简单方法是使用 Get-AzRoleAssignment 而不使用任何参数。

Get-AzRoleAssignment
PS C:\> Get-AzRoleAssignment

RoleAssignmentId   : /subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Authorization/roleAssignments/11111111-1111-1111-1111-111111111111
Scope              : /subscriptions/00000000-0000-0000-0000-000000000000
DisplayName        : Alain
SignInName         : alain@example.com
RoleDefinitionName : Storage Blob Data Reader
RoleDefinitionId   : 2a2b9908-6ea1-4ae2-8e65-a410df84e7d1
ObjectId           : 44444444-4444-4444-4444-444444444444
ObjectType         : User
CanDelegate        : False

RoleAssignmentId   : /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/pharma-sales/providers/Microsoft.Authorization/roleAssignments/33333333-3333-3333-3333-333333333333
Scope              : /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/pharma-sales
DisplayName        : Marketing
SignInName         :
RoleDefinitionName : Contributor
RoleDefinitionId   : b24988ac-6180-42a0-ab88-20f7382dd24c
ObjectId           : 22222222-2222-2222-2222-222222222222
ObjectType         : Group
CanDelegate        : False

...

列出订阅的角色分配

若要列出订阅范围内的所有角色分配,请使用 Get-AzRoleAssignment。 若要获取订阅 ID,可以在 Azure 门户中的 “订阅 ”边栏选项卡上找到它,也可以使用 Get-AzSubscription

Get-AzRoleAssignment -Scope /subscriptions/<subscription_id>
PS C:\> Get-AzRoleAssignment -Scope /subscriptions/00000000-0000-0000-0000-000000000000

列出用户的角色分配

若要列出分配给指定用户的所有角色,请使用 Get-AzRoleAssignment

Get-AzRoleAssignment -SignInName <email_or_userprincipalname>
PS C:\> Get-AzRoleAssignment -SignInName isabella@example.com | FL DisplayName, RoleDefinitionName, Scope

DisplayName        : Isabella Simonsen
RoleDefinitionName : BizTalk Contributor
Scope              : /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/pharma-sales

若要列出分配给指定用户的所有角色以及分配给用户所属组的角色,请使用 Get-AzRoleAssignment

Get-AzRoleAssignment -SignInName <email_or_userprincipalname> -ExpandPrincipalGroups
Get-AzRoleAssignment -SignInName isabella@example.com -ExpandPrincipalGroups | FL DisplayName, RoleDefinitionName, Scope

列出资源组的角色分配

若要列出资源组范围内的所有角色分配,请使用 Get-AzRoleAssignment

Get-AzRoleAssignment -ResourceGroupName <resource_group_name>
PS C:\> Get-AzRoleAssignment -ResourceGroupName pharma-sales | FL DisplayName, RoleDefinitionName, Scope

DisplayName        : Alain Charon
RoleDefinitionName : Backup Operator
Scope              : /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/pharma-sales

DisplayName        : Isabella Simonsen
RoleDefinitionName : BizTalk Contributor
Scope              : /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/pharma-sales

DisplayName        : Alain Charon
RoleDefinitionName : Virtual Machine Contributor
Scope              : /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/pharma-sales

列出管理组的角色分配

若要列出管理组范围内的所有角色分配,请使用 Get-AzRoleAssignment。 若要获取管理组 ID,可以在 Azure 门户的 “管理组 ”边栏选项卡上找到它,也可以使用 Get-AzManagementGroup

Get-AzRoleAssignment -Scope /providers/Microsoft.Management/managementGroups/<group_id>
PS C:\> Get-AzRoleAssignment -Scope /providers/Microsoft.Management/managementGroups/marketing-group

列出资源的角色分配情况

若要列出特定资源的角色分配,请使用 Get-AzRoleAssignment-Scope 参数。 范围将有所不同,具体取决于资源。 若要获取范围,可以在没有任何参数的情况下运行 Get-AzRoleAssignment ,以列出所有角色分配,然后找到要列出的范围。

Get-AzRoleAssignment -Scope "/subscriptions/<subscription_id>/resourcegroups/<resource_group_name>/providers/<provider_name>/<resource_type>/<resource>

以下示例演示如何列出存储帐户的角色分配。 请注意,此命令还会列出适用于此存储帐户的更高范围的角色分配,例如资源组和订阅。

PS C:\> Get-AzRoleAssignment -Scope "/subscriptions/00000000-0000-0000-0000-000000000000/resourcegroups/storage-test-rg/providers/Microsoft.Storage/storageAccounts/storagetest0122"

如果只想列出直接在资源上分配的角色分配,可以使用 Where-Object 命令筛选列表。

PS C:\> Get-AzRoleAssignment | Where-Object {$_.Scope -eq "/subscriptions/00000000-0000-0000-0000-000000000000/resourcegroups/storage-test-rg/providers/Microsoft.Storage/storageAccounts/storagetest0122"}

列出经典服务管理员和共同管理员的角色分配

若要列出经典订阅管理员和共同管理员的角色分配,请使用 Get-AzRoleAssignment

Get-AzRoleAssignment -IncludeClassicAdministrators

列出托管标识的角色分配

  1. 获取系统分配的或用户分配的托管身份的对象 ID。

    若要获取用户分配的托管标识的对象 ID,可以使用 Get-AzADServicePrincipal

    Get-AzADServicePrincipal -DisplayNameBeginsWith "<name> or <vmname>"
    
  2. 若要列出角色分配,请使用 Get-AzRoleAssignment

    Get-AzRoleAssignment -ObjectId <objectid>
    

后续步骤

使用 Azure PowerShell 分配 Azure 角色