共用方式為


教學課程:使用 Azure PowerShell 授與群組 Azure 資源的存取權

Azure 角色型存取控制 (Azure RBAC) 是您管理 Azure 資源存取權的方式。 在本教學課程中,您會授與群組存取權,以檢視訂用帳戶中的所有內容,並使用 Azure PowerShell 管理資源群組中的所有內容。

在本教學課程中,您將瞭解如何:

  • 在不同範圍內授權群組的存取權限
  • 清單存取
  • 移除存取權

如尚未擁有 Azure 訂用帳戶,請在開始之前先建立免費帳戶

備註

建議您使用 Azure Az PowerShell 模組來與 Azure 互動。 開始使用前,請參閱安裝 Azure PowerShell。 若要了解如何移轉至 Az PowerShell 模組,請參閱將 Azure PowerShell 從 AzureRM 移轉至 Az

先決條件

若要完成本教學課程,您將需要:

角色指派

在 Azure RBAC 中,若要授與存取權,您可以建立角色指派。 角色指派由三項元素所組成:安全性主體、角色定義和範圍。 以下是您將在本教學課程中執行的兩個角色指派:

安全性主體 角色定義 Scope
群體
(RBAC 教學小組)
Reader Subscription
群體
(RBAC 教學小組)
Contributor 資源群組
(rbac-tutorial-resource-group)

群組的角色指派

建立群組

若要指派角色,您需要使用者、群組或服務主體。 如果您還沒有群組,可以建立群組。

  • 在 Azure Cloud Shell 中,使用 New-MgGroup 命令建立新的群組。

    New-MgGroup -DisplayName "RBAC Tutorial Group" -MailEnabled:$false `
        -SecurityEnabled:$true -MailNickName "NotSet"
    
    DisplayName         Id                                   MailNickname Description GroupTypes
    -----------         --                                   ------------ ----------- ----------
    RBAC Tutorial Group aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb NotSet                   {}
    

如果您沒有建立群組的許可權,您可以嘗試 教學課程:改用 Azure PowerShell 授與使用者 Azure 資源的存取權

建立資源群組

您可以使用資源群組來示範如何在資源群組範圍指派角色。

  1. 使用 Get-AzLocation 命令取得區域位置清單。

    Get-AzLocation | select Location
    
  2. 選取您附近的位置,並將其指派給變數。

    $location = "westus"
    
  3. 使用 New-AzResourceGroup 命令建立新的資源群組。

    New-AzResourceGroup -Name "rbac-tutorial-resource-group" -Location $location
    
    ResourceGroupName : rbac-tutorial-resource-group
    Location          : westus
    ProvisioningState : Succeeded
    Tags              :
    ResourceId        : /subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/resourceGroups/rbac-tutorial-resource-group
    

授與存取權

若要授與群組的存取權,請使用 New-AzRoleAssignment 命令來指派角色。 您必須指定安全性主體、角色定義和範圍。

  1. 使用 Get-MgGroup 命令取得群組的物件識別碼。

    Get-MgGroup -Filter "DisplayName eq 'RBAC Tutorial Group'"
    
    DisplayName         Id                                   MailNickname Description GroupTypes
    -----------         --                                   ------------ ----------- ----------
    RBAC Tutorial Group aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb NotSet                   {}
    
  2. 將群組物件 ID 儲存在變數中。

    $groupId = "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb"
    
  3. 使用 Get-AzSubscription 命令取得訂用帳戶的識別碼。

    Get-AzSubscription
    
    Name     : Pay-As-You-Go
    Id       : 00000000-0000-0000-0000-000000000000
    TenantId : aaaabbbb-0000-cccc-1111-dddd2222eeee
    State    : Enabled
    
  4. 將訂用帳戶範圍儲存在變數中。

    $subScope = "/subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e"
    
  5. 讀取者 角色指派給訂閱範圍的群組。

    New-AzRoleAssignment -ObjectId $groupId `
      -RoleDefinitionName "Reader" `
      -Scope $subScope
    
    RoleAssignmentId   : /subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/providers/Microsoft.Authorization/roleAssignments/00000000-0000-0000-0000-000000000000
    Scope              : /subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e
    DisplayName        : RBAC Tutorial Group
    SignInName         :
    RoleDefinitionName : Reader
    RoleDefinitionId   : acdd72a7-3385-48ef-bd42-f606fba81ae7
    ObjectId           : aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb
    ObjectType         : Group
    CanDelegate        : False
    
  6. 參與者角色 指派給資源群組的作用域中的群組。

    New-AzRoleAssignment -ObjectId $groupId `
      -RoleDefinitionName "Contributor" `
      -ResourceGroupName "rbac-tutorial-resource-group"
    
    RoleAssignmentId   : /subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/resourceGroups/rbac-tutorial-resource-group/providers/Microsoft.Authorization/roleAssignments/00000000-0000-0000-0000-000000000000
    Scope              : /subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/resourceGroups/rbac-tutorial-resource-group
    DisplayName        : RBAC Tutorial Group
    SignInName         :
    RoleDefinitionName : Contributor
    RoleDefinitionId   : b24988ac-6180-42a0-ab88-20f7382dd24c
    ObjectId           : aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb
    ObjectType         : Group
    CanDelegate        : False
    

清單存取

  1. 若要驗證訂用帳戶的存取權,請使用 Get-AzRoleAssignment 命令來列出角色指派。

    Get-AzRoleAssignment -ObjectId $groupId -Scope $subScope
    
    RoleAssignmentId   : /subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/providers/Microsoft.Authorization/roleAssignments/ffffffff-eeee-dddd-cccc-bbbbbbbbbbb0
    Scope              : /subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e
    DisplayName        : RBAC Tutorial Group
    SignInName         :
    RoleDefinitionName : Reader
    RoleDefinitionId   : acdd72a7-3385-48ef-bd42-f606fba81ae7
    ObjectId           : aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb
    ObjectType         : Group
    CanDelegate        : False
    

    在輸出中,您可以看到「讀者」角色已在訂用帳戶範圍中指派給 RBAC 教學課程群組。

  2. 若要驗證資源群組的存取權,請使用 Get-AzRoleAssignment 命令來列出角色指派。

    Get-AzRoleAssignment -ObjectId $groupId -ResourceGroupName "rbac-tutorial-resource-group"
    
    RoleAssignmentId   : /subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/resourceGroups/rbac-tutorial-resource-group/providers/Microsoft.Authorization/roleAssignments/00000000-0000-0000-0000-000000000000
    Scope              : /subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/resourceGroups/rbac-tutorial-resource-group
    DisplayName        : RBAC Tutorial Group
    SignInName         :
    RoleDefinitionName : Contributor
    RoleDefinitionId   : b24988ac-6180-42a0-ab88-20f7382dd24c
    ObjectId           : aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb
    ObjectType         : Group
    CanDelegate        : False
    
    RoleAssignmentId   : /subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/providers/Microsoft.Authorization/roleAssignments/ffffffff-eeee-dddd-cccc-bbbbbbbbbbb0
    Scope              : /subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e
    DisplayName        : RBAC Tutorial Group
    SignInName         :
    RoleDefinitionName : Reader
    RoleDefinitionId   : acdd72a7-3385-48ef-bd42-f606fba81ae7
    ObjectId           : aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb
    ObjectType         : Group
    CanDelegate        : False
    

    在輸出中,您可以看到貢獻者和讀取者角色都已指派給 RBAC 教學群組。 「參與者」角色會位於 rbac-tutorial-resource-group 範圍中,此外會在訂用帳戶範圍中繼承「讀者」角色。

(選用)使用 Azure 入口網站列出存取權

  1. 若要查看角色指派在 Azure 入口網站中的顯示情形,請檢視訂用帳戶的 [存取控制 (IAM)] 刀鋒視窗。

    在訂用帳戶範圍內為群組進行角色指派

  2. 檢視資源群組的 [存取控制 (IAM)] 窗格。

    資源群組範圍內群組的角色指派

移除存取權

若要移除使用者、群組和應用程式的存取權,請使用 Remove-AzRoleAssignment 來移除角色指派。

  1. 使用下列命令,在資源群組範圍中移除群組的參與者角色指派。

    Remove-AzRoleAssignment -ObjectId $groupId `
      -RoleDefinitionName "Contributor" `
      -ResourceGroupName "rbac-tutorial-resource-group"
    
  2. 使用下列命令,移除群組在訂用帳戶範圍的「讀者」角色指派。

    Remove-AzRoleAssignment -ObjectId $groupId `
      -RoleDefinitionName "Reader" `
      -Scope $subScope
    

清理資源

若要清除本教學課程所建立的資源,請刪除資源群組和群組。

  1. 使用 Remove-AzResourceGroup 命令刪除資源群組。

    Remove-AzResourceGroup -Name "rbac-tutorial-resource-group"
    
    Confirm
    Are you sure you want to remove resource group 'rbac-tutorial-resource-group'
    [Y] Yes  [N] No  [S] Suspend  [?] Help (default is "Y"):
    
  2. 當要求確認時,請輸入 Y。刪除需要幾秒鐘的時間。

  3. 使用 Remove-MgGroup 命令刪除群組。

    Remove-MgGroup -GroupID $groupId
    

    如果您在嘗試刪除群組時收到錯誤,您也可以在入口網站中刪除群組。

後續步驟