共用方式為


使用 Azure PowerShell 指派 Azure 角色

Azure 角色型存取控制 (Azure RBAC) 是您用來管理 Azure 資源存取權的授權系統。 若要授與存取權,您可以將角色指派給特定範圍的使用者、群組、服務主體或受控識別。 本文說明如何使用 Azure PowerShell 指派角色。

備註

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

先決條件

若要指派角色,您必須具備:

指派 Azure 角色的步驟

指派角色包含三個元素:安全性主體、角色定義和範圍。

步驟 1:判斷誰需要存取權

您可以將角色指派給使用者、群組、服務主體或受控識別。 若要指派角色,您可能需要指定物件的唯一 ID。 ID 的格式為: 11111111-1111-1111-1111-111111111111。 您可以使用 Azure 入口網站 或 Azure PowerShell 取得識別碼。

User

針對 Microsoft Entra 使用者,請取得使用者主體名稱,例如 patlong@contoso.com 或使用者物件識別碼。 若要取得物件識別碼,您可以使用 Get-AzADUser

Get-AzADUser -StartsWith <userName>
(Get-AzADUser -DisplayName <userName>).id

Group

針對 Microsoft Entra 群組,您需要群組物件識別碼。 若要取得物件標識碼,您可以使用 Get-AzADGroup

Get-AzADGroup -SearchString <groupName>
(Get-AzADGroup -DisplayName <groupName>).id

服務主體

針對 Microsoft Entra 服務主體 (應用程式使用的身分識別),您需要服務主體物件識別碼。 若要取得物件識別碼,您可以使用 Get-AzADServicePrincipal。 針對服務主體,請使用物件識別碼, 而不是 應用程式識別碼。

Get-AzADServicePrincipal -SearchString <principalName>
(Get-AzADServicePrincipal -DisplayName <principalName>).id

受控識別

針對系統指派或使用者指派的受控識別,您需要物件識別碼。 若要取得物件識別碼,您可以使用 Get-AzADServicePrincipal

Get-AzADServicePrincipal -SearchString <principalName>
(Get-AzADServicePrincipal -DisplayName <principalName>).id

步驟 2:選擇合適的角色

權限會被組合成角色。 您可以從數個 Azure 內建角色 的清單中選取,也可以使用自己的自訂角色。 最佳做法是授與所需最低權限的存取權,因此請避免指派更廣泛的角色。

若要列出角色並取得唯一角色識別碼,您可以使用 Get-AzRoleDefinition

Get-AzRoleDefinition | Format-Table -Property Name, IsCustom, Id

以下是列出特定角色詳細資料的方法。

Get-AzRoleDefinition -Name <roleName>

如需詳細資訊,請參閱 列出 Azure 角色定義

步驟 3:確定所需的範圍

Azure 提供四個層級的範圍:資源、 資源群組、訂用帳戶和管理 群組。 最佳做法是以所需的最低權限授與存取權,因此請避免在更廣泛的範圍內指派角色。 如需範圍的詳細資訊,請參閱 了解範圍

資源範圍

對於資源範圍,您需要該資源的 ID。 您可以在 Azure 入口網站中查看資源的屬性,以尋找資源識別碼。 資源識別碼具有下列格式。

/subscriptions/<subscriptionId>/resourcegroups/<resourceGroupName>/providers/<providerName>/<resourceType>/<resourceSubType>/<resourceName>

資源群組範圍

對於資源群組範圍,您需要資源群組的名稱。 您可以在 Azure 入口網站的 [資源群組] 頁面上找到名稱,也可以使用 Get-AzResourceGroup

Get-AzResourceGroup

訂閱範圍

針對訂用帳戶範圍,您需要訂用帳戶識別碼。 您可以在 Azure 入口網站的 [訂用帳戶 ] 頁面上找到識別碼,也可以使用 Get-AzSubscription

Get-AzSubscription

管理群組範圍

針對管理群組的範圍,您需要管理群組名稱。 您可以在 Azure 入口網站的 [ 管理群組 ] 頁面上找到名稱,也可以使用 Get-AzManagementGroup

Get-AzManagementGroup

步驟 4:指派角色

若要指派角色,請使用 New-AzRoleAssignment 命令。 視範圍而定,命令通常具有下列其中一種格式。

資源範圍

New-AzRoleAssignment -ObjectId <objectId> `
-RoleDefinitionName <roleName> `
-Scope /subscriptions/<subscriptionId>/resourcegroups/<resourceGroupName>/providers/<providerName>/<resourceType>/<resourceSubType>/<resourceName>
New-AzRoleAssignment -ObjectId <objectId> `
-RoleDefinitionId <roleId> `
-ResourceName <resourceName> `
-ResourceType <resourceType> `
-ResourceGroupName <resourceGroupName>

資源群組範圍

New-AzRoleAssignment -SignInName <emailOrUserprincipalname> `
-RoleDefinitionName <roleName> `
-ResourceGroupName <resourceGroupName>
New-AzRoleAssignment -ObjectId <objectId> `
-RoleDefinitionName <roleName> `
-ResourceGroupName <resourceGroupName>

訂閱範圍

New-AzRoleAssignment -SignInName <emailOrUserprincipalname> `
-RoleDefinitionName <roleName> `
-Scope /subscriptions/<subscriptionId>
New-AzRoleAssignment -ObjectId <objectId> `
-RoleDefinitionName <roleName> `
-Scope /subscriptions/<subscriptionId>

管理群組範圍

New-AzRoleAssignment -SignInName <emailOrUserprincipalname> `
-RoleDefinitionName <roleName> `
-Scope /providers/Microsoft.Management/managementGroups/<groupName>
New-AzRoleAssignment -ObjectId <objectId> `
-RoleDefinitionName <roleName> `
-Scope /providers/Microsoft.Management/managementGroups/<groupName>

指派角色範例

為儲存帳戶資源範圍內的所有 Blob 容器配置角色

在名為 storage12345 的儲存體帳戶的資源範圍中,將儲存體 Blob 資料貢獻者角色指派給物件識別碼為 55555555-5555-5555-5555-555555555555 和應用程式識別碼為 66666666-6666-6666-6666-666666666666 的服務主體。

PS C:\> New-AzRoleAssignment -ApplicationId 66666666-6666-6666-6666-666666666666 `
-RoleDefinitionName "Storage Blob Data Contributor" `
-Scope "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/Example-Storage-rg/providers/Microsoft.Storage/storageAccounts/storage12345"

RoleAssignmentId   : /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/Example-Storage-rg/providers/Microsoft.Storage/storageAccounts/storage12345/providers/Microsoft.Authorization/roleAssignments/cccccccc-cccc-cccc-cccc-cccccccccccc
Scope              : /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/Example-Storage-rg/providers/Microsoft.Storage/storageAccounts/storage12345
DisplayName        : example-identity
SignInName         :
RoleDefinitionName : Storage Blob Data Contributor
RoleDefinitionId   : ba92f5b4-2d11-453d-a403-e96b0029c9fe
ObjectId           : 55555555-5555-5555-5555-555555555555
ObjectType         : ServicePrincipal
CanDelegate        : False

為特定 Blob 容器資源範圍指派角色

在資源範圍內,將儲存 Blob 資料參與者角色指派給物件識別碼為55555555-5555-5555-5555-555555555555及應用程式識別碼為66666666-6666-6666-6666-666666666666的服務主體,以用於名為blob-container-01的 Blob 容器。

PS C:\> New-AzRoleAssignment -ApplicationId 66666666-6666-6666-6666-666666666666 `
-RoleDefinitionName "Storage Blob Data Contributor" `
-Scope "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/Example-Storage-rg/providers/Microsoft.Storage/storageAccounts/storage12345/blobServices/default/containers/blob-container-01"

RoleAssignmentId   : /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/Example-Storage-rg/providers/Microsoft.Storage/storageAccounts/storage12345/blobServices/default/containers/blob-container-01/providers/Microsoft.Authorization/roleAssignm
                     ents/dddddddd-dddd-dddd-dddd-dddddddddddd
Scope              : /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/Example-Storage-rg/providers/Microsoft.Storage/storageAccounts/storage12345/blobServices/default/containers/blob-container-01
DisplayName        : example-identity
SignInName         :
RoleDefinitionName : Storage Blob Data Contributor
RoleDefinitionId   : ba92f5b4-2d11-453d-a403-e96b0029c9fe
ObjectId           : 55555555-5555-5555-5555-555555555555
ObjectType         : ServicePrincipal
CanDelegate        : False

為特定虛擬網路資源範圍中的群組指派角色

在虛擬網路名稱為 pharma-sales-project-network 的資源範圍中,將虛擬機器貢獻者角色指派給識別碼為 aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa 的 Pharma Sales Admins 群組。

PS C:\> New-AzRoleAssignment -ObjectId aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa `
-RoleDefinitionName "Virtual Machine Contributor" `
-ResourceName pharma-sales-project-network `
-ResourceType Microsoft.Network/virtualNetworks `
-ResourceGroupName MyVirtualNetworkResourceGroup

RoleAssignmentId   : /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/MyVirtualNetworkResourceGroup
                     /providers/Microsoft.Network/virtualNetworks/pharma-sales-project-network/providers/Microsoft.Authorizat
                     ion/roleAssignments/bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb
Scope              : /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/MyVirtualNetworkResourceGroup
                     /providers/Microsoft.Network/virtualNetworks/pharma-sales-project-network
DisplayName        : Pharma Sales Admins
SignInName         :
RoleDefinitionName : Virtual Machine Contributor
RoleDefinitionId   : 9980e02c-c2be-4d73-94e8-173b1dc7cf3c
ObjectId           : aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa
ObjectType         : Group
CanDelegate        : False

在資源群組範圍內指派使用者的角色

虛擬機器貢獻者 角色指派給在 patlong@contoso.com 資源群組範圍內的 使用者。

PS C:\> New-AzRoleAssignment -SignInName patlong@contoso.com `
-RoleDefinitionName "Virtual Machine Contributor" `
-ResourceGroupName pharma-sales

RoleAssignmentId   : /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/pharma-sales/pr
                     oviders/Microsoft.Authorization/roleAssignments/55555555-5555-5555-5555-555555555555
Scope              : /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/pharma-sales
DisplayName        : Pat Long
SignInName         : patlong@contoso.com
RoleDefinitionName : Virtual Machine Contributor
RoleDefinitionId   : 9980e02c-c2be-4d73-94e8-173b1dc7cf3c
ObjectId           : 44444444-4444-4444-4444-444444444444
ObjectType         : User
CanDelegate        : False

或者,您可以使用參數 -Scope 指定完整資源群組:

PS C:\> New-AzRoleAssignment -SignInName patlong@contoso.com `
-RoleDefinitionName "Virtual Machine Contributor" `
-Scope "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/pharma-sales"

RoleAssignmentId   : /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/pharma-sales/providers/Microsoft.Authorization/roleAssignments/55555555-5555-5555-5555-555555555555
Scope              : /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/pharma-sales
DisplayName        : Pat Long
SignInName         : patlong@contoso.com
RoleDefinitionName : Virtual Machine Contributor
RoleDefinitionId   : 9980e02c-c2be-4d73-94e8-173b1dc7cf3c
ObjectId           : 44444444-4444-4444-4444-444444444444
ObjectType         : User
CanDelegate        : False

在資源群組範圍使用唯一角色識別碼為使用者指派角色

有幾次角色名稱可能會變更,例如:

  • 您正在使用自己的自訂角色,並決定要變更其名稱。
  • 您正在使用名稱中有 (預覽版) 的預覽角色。 釋放角色時,會重新命名角色。

即使重新命名角色,角色 ID 也不會變更。 如果您使用指令碼或自動化來建立角色指派,最佳實務是使用唯一角色識別碼,而不是角色名稱。 因此,如果重新命名角色,您的腳本更有可能正常運作。

下列範例會將 虛擬機器參與者 角色指派給 patlong@contoso.compharma-sales 資源群組範圍的使用者。

PS C:\> New-AzRoleAssignment -ObjectId 44444444-4444-4444-4444-444444444444 `
-RoleDefinitionId 9980e02c-c2be-4d73-94e8-173b1dc7cf3c `
-Scope "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/pharma-sales"

RoleAssignmentId   : /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/pharma-sales/providers/Microsoft.Authorization/roleAssignments/55555555-5555-5555-5555-555555555555
Scope              : /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/pharma-sales
DisplayName        : Pat Long
SignInName         : patlong@contoso.com
RoleDefinitionName : Virtual Machine Contributor
RoleDefinitionId   : 9980e02c-c2be-4d73-94e8-173b1dc7cf3c
ObjectId           : 44444444-4444-4444-4444-444444444444
ObjectType         : User
CanDelegate        : False

在資源群組範圍指派應用程式的角色

pharma-sales 資源群組範圍中,將虛擬機器參與者角色指派給服務主體物件識別碼為 77777777-7777-7777-7777-77777777777777 的應用程式。

PS C:\> New-AzRoleAssignment -ObjectId 77777777-7777-7777-7777-777777777777 `
-RoleDefinitionName "Virtual Machine Contributor" `
-ResourceGroupName pharma-sales

RoleAssignmentId   : /subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Authorization/roleAssignments/66666666-6666-6666-6666-666666666666
Scope              : /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/pharma-sales
DisplayName        : MyApp1
SignInName         :
RoleDefinitionName : Virtual Machine Contributor
RoleDefinitionId   : 9980e02c-c2be-4d73-94e8-173b1dc7cf3c
ObjectId           : 77777777-7777-7777-7777-777777777777
ObjectType         : ServicePrincipal
CanDelegate        : False

在訂閱範圍為使用者分配角色

讀取者 角色指派給在訂用帳戶範圍的 annm@example.com 使用者。

PS C:\> New-AzRoleAssignment -SignInName annm@example.com `
-RoleDefinitionName "Reader" `
-Scope "/subscriptions/00000000-0000-0000-0000-000000000000"

RoleAssignmentId   : /subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Authorization/roleAssignments/66666666-6666-6666-6666-666666666666
Scope              : /subscriptions/00000000-0000-0000-0000-000000000000
DisplayName        : Ann M
SignInName         : annm@example.com
RoleDefinitionName : Reader
RoleDefinitionId   : acdd72a7-3385-48ef-bd42-f606fba81ae7
ObjectId           : 77777777-7777-7777-7777-777777777777
ObjectType         : ServicePrincipal
CanDelegate        : False

在管理群組範圍指派使用者的角色

帳單讀取者 角色指派給 alain@example.com 管理群組範圍的使用者。

PS C:\> New-AzRoleAssignment -SignInName alain@example.com `
-RoleDefinitionName "Billing Reader" `
-Scope "/providers/Microsoft.Management/managementGroups/marketing-group"

RoleAssignmentId   : /providers/Microsoft.Management/managementGroups/marketing-group/providers/Microsoft.Authorization/roleAssignments/22222222-2222-2222-2222-222222222222
Scope              : /providers/Microsoft.Management/managementGroups/marketing-group
DisplayName        : Alain Charon
SignInName         : alain@example.com
RoleDefinitionName : Billing Reader
RoleDefinitionId   : fa23ad8b-c56e-40d8-ac0c-ce449e1d2c64
ObjectId           : 44444444-4444-4444-4444-444444444444
ObjectType         : User
CanDelegate        : False

後續步驟