你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

教程:使用 Azure PowerShell 创建 Azure 自定义角色

如果 Azure 内置角色不满足组织的特定需求,你可以创建自己的自定义角色。 对于本教程,你将使用 Azure PowerShell 创建名为 Reader Support Tickets 的自定义角色。 通过该自定义角色,用户可在订阅的控制平面中查看所有内容,还可创建支持票证。

在本教程中,你将了解如何执行以下操作:

  • 创建自定义角色
  • 列出自定义角色
  • 更新自定义角色
  • 删除自定义角色

如果没有 Azure 订阅,请在开始之前创建一个免费帐户

注意

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

先决条件

若要完成本教程,需要:

登录到 Azure PowerShell

登录到 Azure PowerShell

创建自定义角色

创建自定义角色的最简单方法是从内置角色着手,对其进行编辑,然后创建新角色。

  1. 在 PowerShell 中,使用 Get-AzProviderOperation 命令获取适用于 Microsoft.Support 资源提供程序的操作列表。 这有助于了解可用来创建你的权限的操作。 还可以在 Azure 资源提供程序操作中查看所有操作的列表。

    Get-AzProviderOperation "Microsoft.Support/*" | FT Operation, Description -AutoSize
    
    Operation                              Description
    ---------                              -----------
    Microsoft.Support/register/action      Registers to Support Resource Provider
    Microsoft.Support/supportTickets/read  Gets Support Ticket details (including status, severity, contact ...
    Microsoft.Support/supportTickets/write Creates or Updates a Support Ticket. You can create a Support Tic...
    
  2. 使用 Get-AzRoleDefinition 命令以 JSON 格式输出 Reader 角色。

    Get-AzRoleDefinition -Name "Reader" | ConvertTo-Json | Out-File C:\CustomRoles\ReaderSupportRole.json
    
  3. 在编辑器中打开 ReaderSupportRole.json 文件。

    下面显示了 JSON 输出。 有关不同属性的信息,请参阅 Azure 自定义角色

    {
      "Name": "Reader",
      "Id": "acdd72a7-3385-48ef-bd42-f606fba81ae7",
      "IsCustom": false,
      "Description": "Lets you view everything, but not make any changes.",
      "Actions": [
        "*/read"
      ],
      "NotActions": [],
      "DataActions": [],
      "NotDataActions": [],
      "AssignableScopes": [
        "/"
      ]
    }
    
  4. 编辑 JSON 文件来向 Actions 属性添加 "Microsoft.Support/*" 操作。 请确保在读取操作后包括一个逗号。 此操作将允许用户创建支持票证。

  5. 使用 Get-AzSubscription 命令获取订阅的 ID。

    Get-AzSubscription
    
  6. AssignableScopes 中,采用以下格式添加订阅 ID:"/subscriptions/00000000-0000-0000-0000-000000000000"

    必须添加显式的订阅 ID,否则将不允许将角色导入到订阅中。

  7. 删除 Id 属性行并将 IsCustom 属性更改为 true

  8. NameDescription 属性更改为 "Reader Support Tickets" 和 "View everything in the subscription and also open support tickets"。

    JSON 文件应如下所示:

    {
      "Name": "Reader Support Tickets",
      "IsCustom": true,
      "Description": "View everything in the subscription and also open support tickets.",
      "Actions": [
        "*/read",
        "Microsoft.Support/*"
      ],
      "NotActions": [],
      "DataActions": [],
      "NotDataActions": [],
      "AssignableScopes": [
        "/subscriptions/00000000-0000-0000-0000-000000000000"
      ]
    }
    
  9. 若要新建自定义角色,请使用 New-AzRoleDefinition 命令,并指定 JSON 角色定义文件。

    New-AzRoleDefinition -InputFile "C:\CustomRoles\ReaderSupportRole.json"
    
    Name             : Reader Support Tickets
    Id               : 22222222-2222-2222-2222-222222222222
    IsCustom         : True
    Description      : View everything in the subscription and also open support tickets.
    Actions          : {*/read, Microsoft.Support/*}
    NotActions       : {}
    DataActions      : {}
    NotDataActions   : {}
    AssignableScopes : {/subscriptions/00000000-0000-0000-0000-000000000000}
    

    现在,新的自定义角色在 Azure 门户中可用,并可分配给用户、组或服务主体,就像内置角色一样。

列出自定义角色

  • 若要列出所有自定义角色,请使用 Get-AzRoleDefinition 命令。

    Get-AzRoleDefinition | ? {$_.IsCustom -eq $true} | FT Name, IsCustom
    
    Name                   IsCustom
    ----                   --------
    Reader Support Tickets     True
    

    还可以在 Azure 门户中查看自定义角色。

    screenshot of custom role imported in the Azure portal

更新自定义角色

若要更新自定义角色,可以更新 JSON 文件或使用 PSRoleDefinition 对象。

  1. 若要更新 JSON 文件,请使用 Get-AzRoleDefinition 命令以 JSON 格式输出自定义角色。

    Get-AzRoleDefinition -Name "Reader Support Tickets" | ConvertTo-Json | Out-File C:\CustomRoles\ReaderSupportRole2.json
    
  2. 在编辑器中打开该文件。

  3. Actions 中,添加用于创建和管理资源组部署 "Microsoft.Resources/deployments/*" 的操作。

    更新后的 JSON 文件应如下所示:

    {
      "Name": "Reader Support Tickets",
      "Id": "22222222-2222-2222-2222-222222222222",
      "IsCustom": true,
      "Description": "View everything in the subscription and also open support tickets.",
      "Actions": [
        "*/read",
        "Microsoft.Support/*",
        "Microsoft.Resources/deployments/*"
      ],
      "NotActions": [],
      "DataActions": [],
      "NotDataActions": [],
      "AssignableScopes": [
        "/subscriptions/00000000-0000-0000-0000-000000000000"
      ]
    }
    
  4. 若要更新自定义角色,请使用 Set-AzRoleDefinition 命令并指定更新后的 JSON 文件。

    Set-AzRoleDefinition -InputFile "C:\CustomRoles\ReaderSupportRole2.json"
    
    Name             : Reader Support Tickets
    Id               : 22222222-2222-2222-2222-222222222222
    IsCustom         : True
    Description      : View everything in the subscription and also open support tickets.
    Actions          : {*/read, Microsoft.Support/*, Microsoft.Resources/deployments/*}
    NotActions       : {}
    DataActions      : {}
    NotDataActions   : {}
    AssignableScopes : {/subscriptions/00000000-0000-0000-0000-000000000000}
    
  5. 若要使用 PSRoleDefintion 对象更新你的自定义角色,请首先使用 Get-AzRoleDefinition 命令来获取该角色。

    $role = Get-AzRoleDefinition "Reader Support Tickets"
    
  6. 调用 Add 方法来添加用于读取诊断设置的操作。

    $role.Actions.Add("Microsoft.Insights/diagnosticSettings/*/read")
    
  7. 使用 Set-AzRoleDefinition 来更新角色。

    Set-AzRoleDefinition -Role $role
    
    Name             : Reader Support Tickets
    Id               : 22222222-2222-2222-2222-222222222222
    IsCustom         : True
    Description      : View everything in the subscription and also open support tickets.
    Actions          : {*/read, Microsoft.Support/*, Microsoft.Resources/deployments/*,
                       Microsoft.Insights/diagnosticSettings/*/read}
    NotActions       : {}
    DataActions      : {}
    NotDataActions   : {}
    AssignableScopes : {/subscriptions/00000000-0000-0000-0000-000000000000}
    

删除自定义角色

  1. 使用 Get-AzRoleDefinition 命令获取自定义角色的 ID。

    Get-AzRoleDefinition "Reader Support Tickets"
    
  2. 使用 Remove-AzRoleDefinition 命令并指定角色 ID 来删除自定义角色。

    Remove-AzRoleDefinition -Id "22222222-2222-2222-2222-222222222222"
    
    Confirm
    Are you sure you want to remove role definition with id '22222222-2222-2222-2222-222222222222'.
    [Y] Yes  [N] No  [S] Suspend  [?] Help (default is "Y"):
    
  3. 系统要求确认时,请键入“Y” 。

后续步骤