使用 Azure CLI 列出 Azure 角色指派

Azure 角色型存取控制 (Azure RBAC) 是您用來管理 Azure 資源的存取權的授權系統。 若要判斷哪些資源使用者、群組、服務主體或受控識別可以存取,請列出其角色指派。 本文說明如何使用 Azure CLI 列出角色指派。

注意

如果您的組織已將管理功能外包給使用 Azure Lighthouse 的服務提供者,則此處不會顯示該服務提供者授權的角色指派。 同樣地,服務提供者租使用者中的使用者不會看到客戶租用戶中使用者的角色指派,無論他們獲指派的角色為何。

必要條件

列出使用者的角色指派

若要列出特定使用者的角色指派,請使用 az role assignment list

az role assignment list --assignee {assignee}

By default, only role assignments for the current subscription will be displayed. To view role assignments for the current subscription and below, add the --all parameter. To include role assignments at parent scopes, add the --include-inherited parameter. To include role assignments for groups of which the user is a member transitively, add the --include-groups parameter.

The following example lists the role assignments that are assigned directly to the patlong@contoso.com user:

az role assignment list --all --assignee patlong@contoso.com --output json --query '[].{principalName:principalName, roleDefinitionName:roleDefinitionName, scope:scope}'
[
  {
    "principalName": "patlong@contoso.com",
    "roleDefinitionName": "Backup Operator",
    "scope": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/pharma-sales"
  },
  {
    "principalName": "patlong@contoso.com",
    "roleDefinitionName": "Virtual Machine Contributor",
    "scope": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/pharma-sales"
  }
]

列出資源群組的角色指派

若要列出存在於資源群組範圍的角色指派,請使用 az role assignment list

az role assignment list --resource-group {resourceGroup}

The following example lists the role assignments for the pharma-sales resource group:

az role assignment list --resource-group pharma-sales --output json --query '[].{principalName:principalName, roleDefinitionName:roleDefinitionName, scope:scope}'
[
  {
    "principalName": "patlong@contoso.com",
    "roleDefinitionName": "Backup Operator",
    "scope": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/pharma-sales"
  },
  {
    "principalName": "patlong@contoso.com",
    "roleDefinitionName": "Virtual Machine Contributor",
    "scope": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/pharma-sales"
  },
  
  ...

]

列出訂用帳戶的角色指派

若要列出訂用帳戶範圍的所有角色指派,請使用 az role assignment list。 若要取得訂用帳戶標識碼,您可以在 Azure 入口網站 的 [訂用帳戶] 刀鋒視窗中找到它,也可以使用 az account list

az role assignment list --scope "/subscriptions/{subscriptionId}"

Example:

az role assignment list --scope "/subscriptions/00000000-0000-0000-0000-000000000000" --output json --query '[].{principalName:principalName, roleDefinitionName:roleDefinitionName, scope:scope}'
[
  {
    "principalName": "admin@contoso.com",
    "roleDefinitionName": "Owner",
    "scope": "/subscriptions/00000000-0000-0000-0000-000000000000"
  },
  {
    "principalName": "Subscription Admins",
    "roleDefinitionName": "Owner",
    "scope": "/subscriptions/00000000-0000-0000-0000-000000000000"
  },
  {
    "principalName": "alain@contoso.com",
    "roleDefinitionName": "Reader",
    "scope": "/subscriptions/00000000-0000-0000-0000-000000000000"
  },

  ...

]

列出管理群組的角色指派

若要列出管理群組範圍中的所有角色指派,請使用 az role assignment list。 若要取得管理群組標識符,您可以在 Azure 入口網站 的 [管理群組] 刀鋒視窗中找到它,也可以使用 az account management-group list

az role assignment list --scope /providers/Microsoft.Management/managementGroups/{groupId}

Example:

az role assignment list --scope /providers/Microsoft.Management/managementGroups/sales-group --output json --query '[].{principalName:principalName, roleDefinitionName:roleDefinitionName, scope:scope}'
[
  {
    "principalName": "admin@contoso.com",
    "roleDefinitionName": "Owner",
    "scope": "/providers/Microsoft.Management/managementGroups/sales-group"
  },
  {
    "principalName": "alain@contoso.com",
    "roleDefinitionName": "Reader",
    "scope": "/providers/Microsoft.Management/managementGroups/sales-group"
  }
]

列出受控識別的角色指派

執行下列步驟:

  1. 取得系統指派或使用者指派受控識別的主體標識碼。

    若要取得使用者指派受控識別的主體標識符,您可以使用 az ad sp listaz identity list

    az ad sp list --display-name "{name}" --query [].id --output tsv
    

    若要取得系統指派受控識別的主體標識符,您可以使用 az ad sp list

    az ad sp list --display-name "{vmname}" --query [].id --output tsv
    
  2. 若要列出角色指派,請使用 az role assignment list

    根據預設,只會顯示目前訂用帳戶的角色指派。 若要檢視目前訂用帳戶和以下的角色指派,請新增 --all 參數。 若要檢視繼承的角色指派,請新增 --include-inherited 參數。

    az role assignment list --assignee {objectId}
    

後續步驟