共用方式為


使用 Azure CLI 列出 Azure 角色指派

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

備註

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

先決條件

列出使用者的角色指派

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

az role assignment list --assignee {assignee}

根據預設,只會顯示目前訂用帳戶的角色指派。 若要檢視目前訂用帳戶及以下訂用帳戶的角色指派,請新增參數 --all 。 若要在父範圍中包含角色指派,請新增參數 --include-inherited 。 若要以可轉移方式包含使用者所屬群組的角色指派,請新增參數 --include-groups

下列範例列出直接指派給 patlong@contoso.com 使用者的角色指派:

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}

下列範例列出 pharma-sales 資源群組的角色指派:

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}"

範例:

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}

範例:

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}
    

後續步驟

使用 Azure CLI 指派 Azure 角色