Självstudie: skapa en anpassad Azure-roll med hjälp av Azure PowerShellTutorial: Create an Azure custom role using Azure PowerShell
Om de inbyggda Azure-rollerna inte uppfyller organisationens specifika behov kan du skapa dina egna anpassade roller.If the Azure built-in roles don't meet the specific needs of your organization, you can create your own custom roles. För den här självstudien skapar du en anpassad roll med namnet Reader Support Tickets (Läsare av supportbegäranden) med hjälp av Azure PowerShell.For this tutorial, you create a custom role named Reader Support Tickets using Azure PowerShell. Med den här anpassade rollen kan användare visa allt i en prenumerations hanteringsplan samt öppna supportbegäranden.The custom role allows the user to view everything in the management plane of a subscription and also open support tickets.
I den här guiden får du lära dig att:In this tutorial, you learn how to:
- Skapa en anpassad rollCreate a custom role
- Lista anpassade rollerList custom roles
- Uppdatera en anpassad rollUpdate a custom role
- Ta bort en anpassad rollDelete a custom role
Om du inte har någon Azure-prenumeration kan du skapa ett kostnadsfritt konto innan du börjar.If you don't have an Azure subscription, create a free account before you begin.
Anteckning
Den här artikeln har uppdaterats till att använda Azure Az PowerShell-modulen.This article has been updated to use the Azure Az PowerShell module. Az PowerShell-modulen är den rekommenderade PowerShell-modulen för att interagera med Azure.The Az PowerShell module is the recommended PowerShell module for interacting with Azure. För att komma igång med Az PowerShell kan du läsa artikeln om att installera Azure PowerShell.To get started with the Az PowerShell module, see Install Azure PowerShell. Information om hur du migrerar till Az PowerShell-modulen finns i artikeln om att migrera Azure PowerShell från AzureRM till Az.To learn how to migrate to the Az PowerShell module, see Migrate Azure PowerShell from AzureRM to Az.
FörutsättningarPrerequisites
För att kunna genomföra den här kursen behöver du följande:To complete this tutorial, you will need:
- Behörigheter att skapa anpassade roller som Owner (Ägare) eller User Access Administrator (Administratör för användaråtkomst)Permissions to create custom roles, such as Owner or User Access Administrator
- Azure Cloud Shell eller Azure PowerShellAzure Cloud Shell or Azure PowerShell
Logga in till Azure PowerShellSign in to Azure PowerShell
Logga in på Azure PowerShell.Sign in to Azure PowerShell.
Skapa en anpassad rollCreate a custom role
Det enklaste sättet att skapa en anpassad roll är att utgå från en inbyggd roll, redigera den och sedan skapa en ny roll.The easiest way to create a custom role is to start with a built-in role, edit it, and then create a new role.
I PowerShell använder du kommandot Get-AzProviderOperation för att hämta listan över åtgärder för resursprovidern Microsoft.Support.In PowerShell, use the Get-AzProviderOperation command to get the list of operations for the Microsoft.Support resource provider. Det är bra att känna till de åtgärder som är tillgängliga för att skapa dina behörigheter.It's helpful to know the operations that are available to create your permissions. Du kan också se en lista över alla åtgärder vid Azure Resource Provider-åtgärder.You can also see a list of all the operations at Azure resource provider operations.
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...
Använd kommandot Get-AzRoleDefinition för att mata ut rollen Reader (Läsare) i JSON-format.Use the Get-AzRoleDefinition command to output the Reader role in JSON format.
Get-AzRoleDefinition -Name "Reader" | ConvertTo-Json | Out-File C:\CustomRoles\ReaderSupportRole.json
Öppna filen ReaderSupportRole.json i en textredigerare.Open the ReaderSupportRole.json file in an editor.
Följande visar JSON-utdata.The following shows the JSON output. Information om de olika egenskaperna finns i Azure-anpassade roller.For information about the different properties, see Azure custom roles.
{ "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": [ "/" ] }
Redigera JSON-filen för att lägga till
"Microsoft.Support/*"
-åtgärden tillActions
-egenskapen.Edit the JSON file to add the"Microsoft.Support/*"
operation to theActions
property. Se till att inkludera ett kommatecken efter läsåtgärden.Be sure to include a comma after the read operation. Den här åtgärden tillåter att användare skapar supportbegäranden.This action will allow the user to create support tickets.Hämta ID för din prenumeration med hjälp av kommandot Get-AzSubscription.Get the ID of your subscription using the Get-AzSubscription command.
Get-AzSubscription
I
AssignableScopes
lägger du till ditt prenumerations-ID i följande format:"/subscriptions/00000000-0000-0000-0000-000000000000"
InAssignableScopes
, add your subscription ID with the following format:"/subscriptions/00000000-0000-0000-0000-000000000000"
Du måste lägga till explicita prenumerations-ID:n; annars tillåts du inte importera rollen i din prenumeration.You must add explicit subscription IDs, otherwise you won't be allowed to import the role into your subscription.
Ta bort egenskapsraden
Id
och ändra egenskapenIsCustom
tilltrue
.Delete theId
property line and change theIsCustom
property totrue
.Ändra egenskaperna
Name
ochDescription
till ”Reader Support Tickets” (Läsare av supportbegäranden) och ”View everything in the subscription and also open support tickets” (Visa allt i prenumerationen och även öppna supportbegäranden).Change theName
andDescription
properties to "Reader Support Tickets" and "View everything in the subscription and also open support tickets."Din JSON-fil ska se ut så här:Your JSON file should look like the following:
{ "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" ] }
För att skapa en ny anpassad roll använder du kommandot New-AzRoleDefinition och anger JSON-rolldefinitionsfilen.To create the new custom role, use the New-AzRoleDefinition command and specify the JSON role definition file.
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}
Den nya anpassade rollen är nu tillgänglig i Azure-portalen och kan tilldelas till användare, grupper eller tjänstens huvudnamn precis som inbyggda roller.The new custom role is now available in the Azure portal and can be assigned to users, groups, or service principals just like built-in roles.
Lista anpassade rollerList custom roles
Om du vill lista alla dina anpassade roller använder du kommandot Get-AzRoleDefinition.To list all your custom roles, use the Get-AzRoleDefinition command.
Get-AzRoleDefinition | ? {$_.IsCustom -eq $true} | FT Name, IsCustom
Name IsCustom ---- -------- Reader Support Tickets True
Du kan även visa den anpassade rollen i Azure-portalen.You can also see the custom role in the Azure portal.
Uppdatera en anpassad rollUpdate a custom role
Om du vill uppdatera den anpassade rollen kan du uppdatera JSON-filen eller använda objektet PSRoleDefinition
.To update the custom role, you can update the JSON file or use the PSRoleDefinition
object.
För att uppdatera JSON-filen använder du kommandot Get-AzRoleDefinition för att mata ut den anpassade rollen i JSON-format.To update the JSON file, use the Get-AzRoleDefinition command to output the custom role in JSON format.
Get-AzRoleDefinition -Name "Reader Support Tickets" | ConvertTo-Json | Out-File C:\CustomRoles\ReaderSupportRole2.json
Öppna filen i en textredigerare.Open the file in an editor.
I
Actions
lägger du till åtgärden för att skapa och hantera resursgruppsdistributioner"Microsoft.Resources/deployments/*"
.InActions
, add the operation to create and manage resource group deployments"Microsoft.Resources/deployments/*"
.Din uppdaterade JSON-fil ska se ut så här:Your updated JSON file should look like the following:
{ "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" ] }
För att uppdatera den anpassade rollen använder du kommandot Set-AzRoleDefinition och anger den uppdaterade JSON-filen.To update the custom role, use the Set-AzRoleDefinition command and specify the updated JSON file.
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}
Om du vill använda objektet
PSRoleDefintion
för att uppdatera din anpassade roll använder du först kommandot Get-AzRoleDefinition för att hämta rollen.To use thePSRoleDefintion
object to update your custom role, first use the Get-AzRoleDefinition command to get the role.$role = Get-AzRoleDefinition "Reader Support Tickets"
Anropa metoden
Add
för att lägga till åtgärden för att läsa diagnostikinställningar.Call theAdd
method to add the operation to read diagnostic settings.$role.Actions.Add("Microsoft.Insights/diagnosticSettings/*/read")
Använd kommandot Set-AzRoleDefinition för att uppdatera rollen.Use the Set-AzRoleDefinition to update the role.
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}
Ta bort en anpassad rollDelete a custom role
Använd kommandot Get-AzRoleDefinition för att hämta ID för den anpassade rollen.Use the Get-AzRoleDefinition command to get the ID of the custom role.
Get-AzRoleDefinition "Reader Support Tickets"
Använd kommandot Remove-AzRoleDefinition och ange roll-ID för att ta bort den anpassade rollen.Use the Remove-AzRoleDefinition command and specify the role ID to delete the custom role.
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"):
När du ombeds bekräfta skriver du Y.When asked to confirm, type Y.