使用 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。 若要取得訂用帳戶識別碼,可以在 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

To list all the roles that are assigned to a specified user and the roles that are assigned to the groups to which the user belongs, use 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。 若要取得管理群組標識符,您可以在 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>

This following example shows how to list the role assignments for a storage account. Note that this command also lists role assignments at higher scopes, such as resource groups and subscriptions, that apply to this storage account.

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

If you want to just list role assignments that are assigned directly on a resource, you can use the Where-Object command to filter the list.

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. 取得系統指派或使用者指派受控識別的物件標識碼。

    若要取得使用者指派受控識別的物件標識符,您可以使用 Get-AzADServicePrincipal

    Get-AzADServicePrincipal -DisplayNameBeginsWith "<name> or <vmname>"
    
  2. 若要列出角色指派,請使用 Get-AzRoleAssignment

    Get-AzRoleAssignment -ObjectId <objectid>
    

後續步驟