教學課程:使用 Azure PowerShell 建立 Azure 自訂角色

如果 Azure 內建角色 不符合組織的特定需求,您可以建立自己的自訂角色。 在本教學課程中,您會使用 Azure PowerShell 建立名為讀者支援票證的自訂角色。 自訂角色可讓使用者在訂用帳戶的控制平面中檢視所有專案,並開啟支援票證。

在本教學課程中,您會了解如何:

  • 建立自訂角色
  • 列出自訂角色
  • 更新自訂角色
  • 刪除自訂角色

如尚未擁有 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 格式輸出 讀取器 角色。

    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 檔案,將動作新增 "Microsoft.Support/*"Actions 屬性。 請務必在讀取動作後面加上逗號。 這個動作將允許使用者建立支援票證。

  5. 使用 Get-AzSubscription 命令取得訂用帳戶的識別碼。

    Get-AzSubscription
    
  6. AssignableScopes 中,新增格式如下的訂用帳戶識別碼: "/subscriptions/00000000-0000-0000-0000-000000000000"

    您必須新增明確的訂用帳戶識別碼,否則您無法將角色匯入訂用帳戶。

  7. Id刪除屬性行,並將 屬性變更 IsCustomtrue

  8. NameDescription 屬性變更為「讀者支援票證」和「檢視訂用帳戶中的所有內容,並開啟支援票證」。

    您的 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 命令來取得自訂角色的識別碼。

    Get-AzRoleDefinition "Reader Support Tickets"
    
  2. 使用 Remove-AzRoleDefinition 命令,並指定角色識別碼來刪除自訂角色。

    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

下一步