使用 PowerShell 授予对 Azure 预留的 RBAC 访问权限

本文介绍如何使用 PowerShell 授予对 Azure 预留的 Role-Based 访问控制(RBAC)访问权限。 若要查看和管理 Azure 门户中的 RBAC 访问权限,请参阅 查看和管理 Azure 预留的权限

注释

建议使用 Azure Az PowerShell 模块与 Azure 交互。 请参阅安装 Azure PowerShell 以开始使用。 若要了解如何迁移到 Az PowerShell 模块,请参阅 将 Azure PowerShell 从 AzureRM 迁移到 Az

使用 PowerShell 授予访问权限

拥有预留订单所有者访问权限的用户、具有提升访问权限的用户和 用户访问管理员 可以委托他们有权访问的所有预留订单的访问管理。

Azure 门户中未显示使用 PowerShell 授予的访问权限。 但是,你可以在以下部分使用 get-AzRoleAssignment 命令来查看分配的角色。

为所有预留分配所有者角色

使用以下 Azure PowerShell 脚本向用户授予 Azure RBAC 对其Microsoft Entra 租户(目录)中的所有预留订单的访问权限。


Import-Module Az.Accounts
Import-Module Az.Resources
 
Connect-AzAccount -Tenant <TenantId>
 
$response = Invoke-AzRestMethod -Path /providers/Microsoft.Capacity/reservations?api-version=2020-06-01 -Method GET
 
$responseJSON = $response.Content | ConvertFrom-JSON
 
$reservationObjects = $responseJSON.value
 
foreach ($reservation in $reservationObjects)
{
  $reservationOrderId = $reservation.id.substring(0, 84)
  Write-Host "Assigning Owner role assignment to "$reservationOrderId
  New-AzRoleAssignment -Scope $reservationOrderId -ObjectId <ObjectId> -RoleDefinitionName Owner
}

使用 PowerShell 脚本分配所有权角色并成功运行时,不会返回成功消息。

参数

-ObjectId Microsoft用户、组或服务主体的 Entra ObjectId。

  • 类型:字符串
  • 别名:ID、PrincipalId
  • 位置:命名
  • 默认值: 无
  • 接受管道输入:True
  • 接受通配符:False

-TenantId 租户唯一标识符。

  • 类型:字符串
  • 位置:5
  • 默认值: 无
  • 接受管道输入:False
  • 接受通配符:False

租户级访问

必须先拥有用户访问管理员权限,然后才能在租户级别授予用户或组预留管理员、预留参与者和预留读取者角色。 若要在租户级别获取用户访问管理员权限,请遵循 Elevate 访问 步骤。

在租户级别添加预留管理员角色、预留参与者角色或预留读者角色

只有全局管理员可以从 Azure 门户分配这些角色。

  1. 登录 Azure 门户并导航到“预留”。
  2. 选择有权访问的预留。
  3. 在页面顶部,选择 “角色分配”。
  4. 选择“ 角色 ”选项卡。
  5. 若要进行修改,请使用访问控制将用户添加为预留管理员、预留参与者或预留读取者。

使用 Azure PowerShell 脚本在租户级别添加预留管理员角色

使用以下 Azure PowerShell 脚本通过 PowerShell 在租户级别添加预留管理员角色。

Import-Module Az.Accounts
Import-Module Az.Resources
Connect-AzAccount -Tenant <TenantId>
New-AzRoleAssignment -Scope "/providers/Microsoft.Capacity" -PrincipalId <ObjectId> -RoleDefinitionName "Reservations Administrator"

参数

-ObjectId Microsoft用户、组或服务主体的 Entra ObjectId。

  • 类型:字符串
  • 别名:ID、PrincipalId
  • 位置:命名
  • 默认值: 无
  • 接受管道输入:True
  • 接受通配符:False

-TenantId 租户唯一标识符。

  • 类型:字符串
  • 位置:5
  • 默认值: 无
  • 接受管道输入:False
  • 接受通配符:False

使用 Azure PowerShell 脚本在租户级别添加预留参与者角色

使用以下 Azure PowerShell 脚本通过 PowerShell 在租户级别添加预留参与者角色。

Import-Module Az.Accounts
Import-Module Az.Resources
Connect-AzAccount -Tenant <TenantId>
New-AzRoleAssignment -Scope "/providers/Microsoft.Capacity" -PrincipalId <ObjectId> -RoleDefinitionName "Reservations Contributor"

参数

-ObjectId Microsoft用户、组或服务主体的 Entra ObjectId。

  • 类型:字符串
  • 别名:ID、PrincipalId
  • 位置:命名
  • 默认值: 无
  • 接受管道输入:True
  • 接受通配符:False

-TenantId 租户唯一标识符。

  • 类型:字符串
  • 位置:5
  • 默认值: 无
  • 接受管道输入:False
  • 接受通配符:False

使用 Azure PowerShell 脚本在租户级别分配预留读取者角色

使用以下 Azure PowerShell 脚本通过 PowerShell 在租户级别分配预留读取者角色。


Import-Module Az.Accounts
Import-Module Az.Resources

Connect-AzAccount -Tenant <TenantId>

New-AzRoleAssignment -Scope "/providers/Microsoft.Capacity" -PrincipalId <ObjectId> -RoleDefinitionName "Reservations Reader"

参数

-ObjectId Microsoft用户、组或服务主体的 Entra ObjectId。

  • 类型:字符串
  • 别名:ID、PrincipalId
  • 位置:命名
  • 默认值: 无
  • 接受管道输入:True
  • 接受通配符:False

-TenantId 租户唯一标识符。

  • 类型:字符串
  • 位置:5
  • 默认值: 无
  • 接受管道输入:False
  • 接受通配符:False

后续步骤