分享方式:


使用 Azure PowerShell 指派 Azure 角色

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

注意

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

必要條件

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

指派 Azure 角色的步驟

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

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

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

使用者

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

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

群組

針對 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 提供四個範圍層級:資源、 資源群組、訂用帳戶和管理 群組。 最佳做法是使用所需的最低許可權來授與存取權,因此請避免在更廣泛的範圍內指派角色。 如需範圍的詳細資訊,請參閱 瞭解範圍

資源範圍

針對資源範圍,您需要資源的資源標識符。 您可以在 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 容器指派角色

儲存體 Blob 數據參與者角色指派給對象標識碼為 55555555-5555-5555-5555-55555555555 和的服務主體 應用程式標識碼 666666-66666-6666-6666-666666666666666666 在記憶體帳戶的資源範圍中,名為 storage12345 的資源範圍。

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-5555555555 和 Application 的服務主體 標識符 66666666-6666-6666-6666-6666666666666666 在名為 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 的虛擬網路資源範圍中標識符為 aaaa-aaaa-aaaa-aaaa 的製藥銷售 管理員 群組

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

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

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

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

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

下列範例會將虛擬機參與者角色指派給patlong@contoso.com製藥銷售資源群組範圍的使用者

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

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

虛擬機參與者角色指派給服務主體物件標識符為77777777777-7777-7777-7777-777777777777777 的應用程式。

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

下一步