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

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

在本教學課程中,您會了解如何:

  • 為不同範圍的使用者授與存取權
  • 列出存取權
  • 移除存取權

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

注意

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

必要條件

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

角色指派

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

安全性主體 角色定義 範圍
User
(RBAC 教學課程使用者)
讀取者 訂用帳戶
User
(RBAC 教學課程使用者)
參與者 資源群組
(rbac-tutorial-resource-group)

Role assignments for a user

建立使用者

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

  1. 在 Azure Cloud Shell 中,建立符合密碼複雜度需求的密碼。

    $PasswordProfile = @{ Password = "<Password>" }
    
  2. 使用 New-MgUser 命令為您的網域建立新的使用者。

    New-MgUser -DisplayName "RBAC Tutorial User" -PasswordProfile $PasswordProfile `
       -UserPrincipalName "rbacuser@example.com" -AccountEnabled:$true -MailNickName "rbacuser"
    
    DisplayName        Id                                   Mail UserPrincipalName
    -----------        --                                   ---- -----------------
    RBAC Tutorial User 11111111-1111-1111-1111-111111111111      rbacuser@example.com
    

建立資源群組

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

  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/00000000-0000-0000-0000-000000000000/resourceGroups/rbac-tutorial-resource-group
    

授予存取權

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

  1. 使用 Get-AzSubscription 命令取得訂用帳戶的識別碼。

    Get-AzSubscription
    
    Name     : Pay-As-You-Go
    Id       : 00000000-0000-0000-0000-000000000000
    TenantId : 22222222-2222-2222-2222-222222222222
    State    : Enabled
    
  2. 將訂用帳戶範圍儲存在變數中。

    $subScope = "/subscriptions/00000000-0000-0000-0000-000000000000"
    
  3. 將讀者角色指派給訂用帳戶範圍的使用者。

    New-AzRoleAssignment -SignInName rbacuser@example.com `
      -RoleDefinitionName "Reader" `
      -Scope $subScope
    
    RoleAssignmentId   : /subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Authorization/roleAssignments/44444444-4444-4444-4444-444444444444
    Scope              : /subscriptions/00000000-0000-0000-0000-000000000000
    DisplayName        : RBAC Tutorial User
    SignInName         : rbacuser@example.com
    RoleDefinitionName : Reader
    RoleDefinitionId   : acdd72a7-3385-48ef-bd42-f606fba81ae7
    ObjectId           : 11111111-1111-1111-1111-111111111111
    ObjectType         : User
    CanDelegate        : False
    
  4. 參與者 角色指派給資源群組範圍的使用者。

    New-AzRoleAssignment -SignInName rbacuser@example.com `
      -RoleDefinitionName "Contributor" `
      -ResourceGroupName "rbac-tutorial-resource-group"
    
    RoleAssignmentId   : /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rbac-tutorial-resource-group/providers/Microsoft.Authorization/roleAssignments/33333333-3333-3333-3333-333333333333
    Scope              : /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rbac-tutorial-resource-group
    DisplayName        : RBAC Tutorial User
    SignInName         : rbacuser@example.com
    RoleDefinitionName : Contributor
    RoleDefinitionId   : b24988ac-6180-42a0-ab88-20f7382dd24c
    ObjectId           : 11111111-1111-1111-1111-111111111111
    ObjectType         : User
    CanDelegate        : False
    

列出存取權

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

    Get-AzRoleAssignment -SignInName rbacuser@example.com -Scope $subScope
    
    RoleAssignmentId   : /subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Authorization/roleAssignments/22222222-2222-2222-2222-222222222222
    Scope              : /subscriptions/00000000-0000-0000-0000-000000000000
    DisplayName        : RBAC Tutorial User
    SignInName         : rbacuser@example.com
    RoleDefinitionName : Reader
    RoleDefinitionId   : acdd72a7-3385-48ef-bd42-f606fba81ae7
    ObjectId           : 11111111-1111-1111-1111-111111111111
    ObjectType         : User
    CanDelegate        : False
    

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

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

    Get-AzRoleAssignment -SignInName rbacuser@example.com -ResourceGroupName "rbac-tutorial-resource-group"
    
    RoleAssignmentId   : /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rbac-tutorial-resource-group/providers/Microsoft.Authorization/roleAssignments/33333333-3333-3333-3333-333333333333
    Scope              : /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rbac-tutorial-resource-group
    DisplayName        : RBAC Tutorial User
    SignInName         : rbacuser@example.com
    RoleDefinitionName : Contributor
    RoleDefinitionId   : b24988ac-6180-42a0-ab88-20f7382dd24c
    ObjectId           : 11111111-1111-1111-1111-111111111111
    ObjectType         : User
    CanDelegate        : False
    
    RoleAssignmentId   : /subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Authorization/roleAssignments/22222222-2222-2222-2222-222222222222
    Scope              : /subscriptions/00000000-0000-0000-0000-000000000000
    DisplayName        : RBAC Tutorial User
    SignInName         : rbacuser@example.com
    RoleDefinitionName : Reader
    RoleDefinitionId   : acdd72a7-3385-48ef-bd42-f606fba81ae7
    ObjectId           : 11111111-1111-1111-1111-111111111111
    ObjectType         : User
    CanDelegate        : False
    

    在輸出中,您可以看到參與者和讀者角色都已指派給 RBAC 教學課程使用者。 參與者角色位於 rbac-tutorial-resource-group 範圍,且讀取者角色繼承於訂用帳戶範圍。

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

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

    Role assignments for a user at subscription scope

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

    Role assignments for a user at resource group scope

移除存取權

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

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

    Remove-AzRoleAssignment -SignInName rbacuser@example.com `
      -RoleDefinitionName "Contributor" `
      -ResourceGroupName "rbac-tutorial-resource-group"
    
  2. 使用下列命令,移除訂用帳戶範圍用戶的讀者角色指派。

    Remove-AzRoleAssignment -SignInName rbacuser@example.com `
      -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-MgUser 命令刪除使用者。

    $User = Get-MgUser -Filter "DisplayName eq 'RBAC Tutorial User'"
    Remove-MgUser -UserId $User.Id
    

下一步