若要使用 PowerShell 將 Azure 角色指派給安全主體,請呼叫 New-AzRoleAssignment 指令。 若要執行此命令,您需要在對應範圍或更高層級中,具備包含 Microsoft.Authorization/roleAssignments/write 權限的角色。
指令格式會依指派範圍不同而異,但 -ObjectId 和 -RoleDefinitionName 參數是必要的。 雖然 -Scope 參數不是必須的,但為了保留最小權限原則,請加入它。 透過限制角色和授權範圍,即使安全主體遭到入侵,仍可控制承受風險的資源。
-ObjectId 參數是你指派角色的使用者、群組或服務主體的Microsoft Entra物件 ID。 要取得識別碼,請使用 Get-AzADUser 來篩選Microsoft Entra使用者,如下範例所示。
Get-AzADUser -DisplayName '<Display Name>'
(Get-AzADUser -StartsWith '<substring>').Id
第一個回應會傳回安全性主體,而第二個回應會傳回安全性主體的物件識別碼。
UserPrincipalName : markpdaniels@contoso.com
ObjectType : User
DisplayName : Mark P. Daniels
Id : aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb
Type :
aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb
-RoleDefinitionName 參數值是需要指派給主體的 RBAC 角色名稱。 若要使用 Microsoft Entra 認證存取 Azure 入口網站中的 Blob 資料,使用者必須擁有下列角色指派:
- 資料存取角色,例如儲存體 Blob 資料參與者或儲存體 Blob 資料讀取者
- Azure Resource Manager 讀者角色
若要指派角色至 Blob 容器或儲存帳戶,請為 -Scope 參數指定一個包含該資源作用範圍的字串。 此動作符合最低權限的原則,這是一種資訊安全性概念,其中使用者會獲得執行其工作功能所需的最低存取層級。 這種做法可減少非必要權限所可能導致意外或意圖損害的潛在風險。
容器的範圍格式如下:
/subscriptions/<subscription-id>/resourceGroups/<resource-group-name>/providers/Microsoft.Storage/storageAccounts/<storage-account-name>/blobServices/default/containers/<container-name>
儲存體帳戶的範圍格式如下:
/subscriptions/<subscription-id>/resourceGroups/<resource-group-name>/providers/Microsoft.Storage/storageAccounts/<storage-account-name>
若要指派限於儲存體帳戶的角色,請針對 --scope 參數指定一個定義容器範圍的字串。
下列範例會將儲存體 Blob 資料參與者角色指派給使用者。 角色指派的範圍限制於容器的層級。 請務必以自身的值來取代括弧中的範例值和預留位置值 (<>):
New-AzRoleAssignment -SignInName <email> `
-RoleDefinitionName "Storage Blob Data Contributor" `
-Scope "/subscriptions/<subscription-id>/resourceGroups/<resource-group-name>/providers/Microsoft.Storage/storageAccounts/<storage-account-name>/blobServices/default/containers/<container-name>"
下列範例會藉由指定物件識別碼,將儲存體 Blob 資料讀取者角色指派給使用者。 角色的範圍是儲存體帳戶層級。 請務必以自身的值來取代括弧中的範例值和預留位置值 (<>):
New-AzRoleAssignment -ObjectID "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" `
-RoleDefinitionName "Storage Blob Data Reader" `
-Scope "/subscriptions/<subscription-id>/resourceGroups/<resource-group-name>/providers/Microsoft.Storage/storageAccounts/<storage-account-name>"
您的輸出應如下所示:
RoleAssignmentId : /subscriptions/<subscription ID>/resourceGroups/<Resource Group>/providers/Microsoft.Storage/storageAccounts/<Storage Account>/providers/Microsoft.Authorization/roleAssignments/<Role Assignment ID>
Scope : /subscriptions/<subscription ID>/resourceGroups/<Resource Group>/providers/Microsoft.Storage/storageAccounts/<Storage Account>
DisplayName : Mark Patrick
SignInName : markpdaniels@contoso.com
RoleDefinitionName : Storage Blob Data Reader
RoleDefinitionId : <Role Definition ID>
ObjectId : <Object ID>
ObjectType : User
CanDelegate : False
如需在訂用帳戶或資源群組範圍中使用 PowerShell 指派角色的詳細資訊,請參閱使用 Azure PowerShell 指派 Azure 角色。
若要使用 Azure CLI 將 Azure 角色指派給安全主體,請使用 az 角色分配 create 指令。 命令的格式會根據指派的範圍而有所不同。 若要執行此命令,您必須在對應範圍或更高層級中,具備包含 Microsoft.Authorization/roleAssignments/write 權限的角色。
若要指派範圍限定於容器的角色,請針對 --scope 參數指定包含容器範圍的字串。 容器的範圍格式如下:
/subscriptions/<subscription-id>/resourceGroups/<resource-group-name>/providers/Microsoft.Storage/storageAccounts/<storage-account-name>/blobServices/default/containers/<container-name>
下列範例會將儲存體 Blob 資料參與者角色指派給使用者。 角色指派的範疇限定於容器的層級。 請務必以自身的值來取代括弧中的範例值和預留位置值 (<>):
az role assignment create \
--role "Storage Blob Data Contributor" \
--assignee <email> \
--scope "/subscriptions/<subscription-id>/resourceGroups/<resource-group-name>/providers/Microsoft.Storage/storageAccounts/<storage-account-name>/blobServices/default/containers/<container-name>"
下列範例會藉由指定物件識別碼,將儲存體 Blob 資料讀取者角色指派給使用者。 若要深入了解 --assignee-object-id 和 --assignee-principal-type 參數,請參閱 az 角色指派。 於此範例中,角色指派的範圍是限制在儲存帳戶層級。 請務必以自身的值來取代括弧中的範例值和預留位置值 (<>):
az role assignment create \
--role "Storage Blob Data Reader" \
--assignee-object-id "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" \
--assignee-principal-type "User" \
--scope "/subscriptions/<subscription-id>/resourceGroups/<resource-group-name>/providers/Microsoft.Storage/storageAccounts/<storage-account-name>"
當您指派角色或移除角色指派時,最多可能需要 10 分鐘的時間,變更才會生效。
如需在訂用帳戶、資源群組或儲存體帳戶範圍中使用 Azure CLI 指派角色的詳細資訊,請參閱使用 Azure CLI 指派 Azure 角色。