使用 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>
    

后续步骤