Lab Liaison Custom Role

Published May 11 2021 09:00 AM 1,312 Views
Microsoft

Azure provides a few built-in roles to allow or deny actions for Azure Lab Services.  These built-in roles include owner, contributor, lab creator, and reader.  If the built-in roles don’t fit your needs, you can also create and deploy a custom role. That is what we will do in this blog post.

 

In this scenario we need to create a Lab Liaison role.  A lab Liaison is a technical helper that will be able to set lab quota time and reset student VMs for multiple labs and nothing else. We build off of the information in the previous post, Use Custom Role to Tailor Teachers' Lab Management Permissions.

Defining the Lab Liaison Role

First, things first.  Let’s define our custom role. We’ll look at the overall role definition, and then discuss each section.

 

 

{
    "properties": {
        "roleName": "Lab Liaison",
        "description": "Lab Liaison can reset student VMs when necessary.",
        "assignableScopes": [
            "/subscriptions/11111111-1111-1111-1111-11111111"
        ],
        "permissions": [
            {
                "actions": [
                    "Microsoft.LabServices/labAccounts/read",
                    "Microsoft.LabServices/labAccounts/labs/environmentSettings/delete",
                    "Microsoft.LabServices/labAccounts/labs/write",
                    "Microsoft.LabServices/labAccounts/GetPricingAndAvailability/action",
                    "Microsoft.LabServices/labAccounts/GetRestrictionsAndUsage/action"
                ],
                "notActions": [],
                "dataActions": [],
                "notDataActions": []
            }
        ]
    }
}

 

 

Roles contain a name, description, assignable scopes, and a list of allowed or not allowed actions.  Assignable scopes determine at what level a role can be assigned.  This may be a management group (preview), entire subscriptions (as shown above) or a specific resource groups.

 

Our custom role lists five specific actions.  Let’s go over why we need each action.

Action

Purpose

"Microsoft.LabServices/labAccounts/read"

Allows the Lab Liaison to see the labs under each lab account.

"Microsoft.LabServices/labAccounts/labs/environmentSettings/delete"

Allows the Lab Liaison to reset a VM for any VM in a lab.

"Microsoft.LabServices/labAccounts/labs/write"

Allows the Lab Liaison to set the lab usage quota.

"Microsoft.LabServices/labAccounts/labs/write",

"Microsoft.LabServices/labAccounts/GetPricingAndAvailability/action",

"Microsoft.LabServices/labAccounts/GetRestrictionsAndUsage/action"

These three actions are the minimum required set of actions for https://labs.azure.com to successfully load for a user.

 

You’ll notice that we only list specific allowed actions.  The advantage to this approach is that we can assign this role once to a user at the subscription or resource group level and that will affect resources contained within the subscription or resource group.  The Lab Liaison will have access see the VMs, reset the VMs in the Labs Portal, set the lab quota and nothing else.  Lab Liaison will not be able to inadvertently set a schedule that affect the cost or running a lab. 

 

If you are creating a role that is less restrictive, consider using wildcard permissions in conjunction with the notAction section to exclude only specific permissions. 

Deploying Custom Role

There are a few ways to create or add a custom role in Azure including using the Azure Portal, Azure CLI or PowerShell.  We are going to use PowerShell today.

 

First, we to create a new custom role object.

 

$role = New-Object ` 
 -TypeName 'Microsoft.Azure.Commands.Resources.Models.Authorization.PSRoleDefinition'
$role.Id = $null
$role.Name = "Lab Liaison Role"
$role.Description = "Can view labs in Azure Lab portal and reset student VMs."
$role.IsCustom = $true
$role.Actions = @()
$role.AssignableScopes = @()

 

 

Next, let’s set the assignable scope to the current subscription.  (Run Connect-AzAccount first, if not done already.)  You can add more than one assignable scope if there are several management groups, subscriptions and/or resource groups that should have this role available.

 

$currentSubscriptionId = Get-AzContext `
 | Select-Object -expand Subscription `
 | Select-Object -expand Id
$role.AssignableScopes.Add("/subscriptions/$currentSubscriptionId")

 

 

Next, let’s add the specific actions we need for the Lab Liaison Role.

 

$roleAssignmentsToAdd = @(
"Microsoft.LabServices/*/read", 
"Microsoft.LabServices/labAccounts/labs/environmentSettings/environments/delete",
"Microsoft.LabServices/labAccounts/labs/write",
"Microsoft.LabServices/labaccounts/getRestrictionsAndUsage/action",
"Microsoft.LabServices/labaccounts/getPricingAndAvailability/action"
)

$roleAssignmentsToAdd | 
ForEach-Object {
    $role.Actions.Add($_)
}

 

 

Lastly, let’s add the newly created custom role to Azure.

 

New-AzRoleDefinition -Role $role

 

 

Go to Import-LabLiaisonRole.ps1 to see this script in its entirety.  The full script also contains the ability to assign the role to several subscriptions at once, ability to update an existing role, and extra error checking.

Assigning a Custom Role

Yeah! Our custom role is now available for use.  Now let’s assign the role to someone.  Roles can be assigned to users, groups, and service principals. 

 

For our example, we will assign a specific user, liaison@contoso.com,  access at the resource group level.  The user will be able to reset VMs and set lab quotas under any lab under any lab account in that resource group. Role assignments require the

  • Active Directory object id.  This is the id of the user, group, or service principal
  • Scope the assignment affects.  For example if assignment is made at the resource group level, it will affect all resources in that group, including any lab accounts and labs in that resource group.
  • Name of role definition.  In our case, that’s ‘Lab Liaison’

We are going to assign the role to a user, so let’s find the id for the user first.

 

#Get AD object id for user.  Try both user principal name and email
$email = 'liaison@contoso.com'
$userAdObject = $null
$userAdObject = Get-AzADUser `
-UserPrincipalName $email.ToString().Trim() `
-ErrorAction SilentlyContinue
if (-not $userAdObject){
$userAdObject = Get-AzADUser `
   -Mail $email.ToString().Trim() `
   -ErrorAction SilentlyContinue
}
if (-not $userAdObject){
 	Write-Error "Couldn't find user '$email' in Azure AD."
}

 

 

Next, we need to get the id of the resource group, so we can set the scope of the role assignment.

 

$resourceGroupId = Get-AzResourceGroup `
 -ResourceGroupName '{resource-group-name}' `
 | Select-Object -ExpandProperty ResourceId

 

 

Now we are all set to make the role assignment.  Creating a role assignment with the same object id, definition name and scope will throw an error, so we’ll only create the role assignment if it doesn’t exist already.

 

$RoleDefinitionName = "Lab Services Liaison"
if (-not (Get-AzRoleAssignment `
 -ObjectId $userAdObject.Id `
 -RoleDefinitionName $RoleDefinitionName `
 -Scope $resourceGroupId `
 -ErrorAction SilentlyContinue)) {

New-AzRoleAssignment `
    -ObjectId $userAdObject.Id `
    -RoleDefinitionName $RoleDefinitionName 
    -Scope $resourceGroupId
}

 

 

Role assignments can be made with the subscription, resource group, lab account and even specific lab as the scope.  Just pass in the resource id for that resource to the scope argument.  Consider using the Az.LabServices PowerShell module (preview) to make the task of getting resource id for lab accounts and labs easier. 

Conclusion

You are all set!  We’ve defined, imported, and assigned our Lab Liaison custom role.  If you want to create a role with more permission, refer back to the Use Custom Role to Tailor Teachers' Lab Management Permissions - Microsoft Tech Community blog post.  It has a nice list of Lab Services permissions and their purpose.

 

We hope that you find this post helpful!

~Az Labs team

%3CLINGO-SUB%20id%3D%22lingo-sub-2323761%22%20slang%3D%22en-US%22%3ELab%20Liaison%20Custom%20Role%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2323761%22%20slang%3D%22en-US%22%3E%3CP%3EAzure%20provides%20a%20few%20built-in%20roles%20to%20allow%20or%20deny%20actions%20for%20Azure%20Lab%20Services.%26nbsp%3B%20These%20built-in%20roles%20include%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fazure%2Frole-based-access-control%2Fbuilt-in-roles%23owner%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Eowner%3C%2FA%3E%2C%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fazure%2Frole-based-access-control%2Fbuilt-in-roles%23contributor%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Econtributor%3C%2FA%3E%2C%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fazure%2Frole-based-access-control%2Fbuilt-in-roles%23lab-creator%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Elab%20creator%3C%2FA%3E%2C%20and%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fazure%2Frole-based-access-control%2Fbuilt-in-roles%23reader%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ereader%3C%2FA%3E.%26nbsp%3B%20If%20the%20built-in%20roles%20don%E2%80%99t%20fit%20your%20needs%2C%20you%20can%20also%20create%20and%20deploy%20a%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Frole-based-access-control%2Fcustom-roles%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ecustom%20role%3C%2FA%3E.%20That%20is%20what%20we%20will%20do%20in%20this%20blog%20post.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%20this%20scenario%20we%20need%20to%20create%20a%20Lab%20Liaison%20role.%26nbsp%3B%20A%20lab%20Liaison%20is%20a%20technical%20helper%20that%20will%20be%20able%20to%20reset%20student%20VMs%20for%20multiple%20labs%20and%20nothing%20else.%20We%20build%20off%20of%20the%20information%20in%20the%20previous%20post%2C%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fazure-lab-services%2Fuse-custom-role-to-tailor-teachers-lab-management-permissions%2Fba-p%2F2113016%22%20target%3D%22_blank%22%3EUse%20Custom%20Role%20to%20Tailor%20Teachers'%20Lab%20Management%20Permissions%3C%2FA%3E.%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--467854440%22%20id%3D%22toc-hId--467854440%22%3EDefining%20the%20Lab%20Liaison%20Role%3C%2FH2%3E%0A%3CP%3EFirst%2C%20things%20first.%26nbsp%3B%20Let%E2%80%99s%20define%20our%20custom%20role.%20We%E2%80%99ll%20look%20at%20the%20overall%20role%20definition%2C%20and%20then%20discuss%20each%20section.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-json%22%3E%3CCODE%3E%7B%0A%20%20%20%20%22properties%22%3A%20%7B%0A%20%20%20%20%20%20%20%20%22roleName%22%3A%20%22Lab%20Liaison%22%2C%0A%20%20%20%20%20%20%20%20%22description%22%3A%20%22Lab%20Liaison%20can%20reset%20student%20VMs%20when%20necessary.%22%2C%0A%20%20%20%20%20%20%20%20%22assignableScopes%22%3A%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22%2Fsubscriptions%2F11111111-1111-1111-1111-11111111%22%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20%22permissions%22%3A%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22actions%22%3A%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Microsoft.LabServices%2FlabAccounts%2Fread%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Microsoft.LabServices%2FlabAccounts%2Flabs%2FenvironmentSettings%2Fdelete%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Microsoft.LabServices%2FlabAccounts%2Flabs%2Fwrite%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Microsoft.LabServices%2FlabAccounts%2FGetPricingAndAvailability%2Faction%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Microsoft.LabServices%2FlabAccounts%2FGetRestrictionsAndUsage%2Faction%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22notActions%22%3A%20%5B%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22dataActions%22%3A%20%5B%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22notDataActions%22%3A%20%5B%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20%7D%0A%7D%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ERoles%20contain%20a%20name%2C%20description%2C%20assignable%20scopes%2C%20and%20a%20list%20of%20allowed%20or%20not%20allowed%20actions.%26nbsp%3B%20Assignable%20scopes%20determine%20at%20what%20level%20a%20role%20%3CEM%3Ecan%3C%2FEM%3E%20be%20assigned.%26nbsp%3B%20This%20may%20be%20a%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fgovernance%2Fmanagement-groups%2Foverview%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Emanagement%20group%3C%2FA%3E%20(preview)%2C%20entire%20subscriptions%20(as%20shown%20above)%20or%20a%20specific%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fazure-resource-manager%2Fmanagement%2Fmanage-resource-groups-portal%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Eresource%20groups.%3C%2FA%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EOur%20custom%20role%20lists%20five%20specific%20actions.%26nbsp%3B%20Let%E2%80%99s%20go%20over%20why%20we%20need%20each%20action.%3C%2FP%3E%0A%3CTABLE%3E%0A%3CTBODY%3E%0A%3CTR%3E%0A%3CTD%3E%3CP%3EAction%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3EPurpose%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E%3CP%3E%22Microsoft.LabServices%2FlabAccounts%2Fread%22%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3EAllows%20the%20Lab%20Liaison%20to%20see%20the%20labs%20under%20each%20lab%20account.%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E%3CP%3E%22Microsoft.LabServices%2FlabAccounts%2Flabs%2FenvironmentSettings%2Fdelete%22%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3EAllows%20the%20Lab%20Liaison%20to%20reset%20a%20VM%20for%20any%20VM%20in%20a%20lab.%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E%3CP%3E%22Microsoft.LabServices%2FlabAccounts%2Flabs%2Fwrite%22%2C%3C%2FP%3E%0A%3CP%3E%22Microsoft.LabServices%2FlabAccounts%2FGetPricingAndAvailability%2Faction%22%2C%3C%2FP%3E%0A%3CP%3E%22Microsoft.LabServices%2FlabAccounts%2FGetRestrictionsAndUsage%2Faction%22%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3EThese%20three%20actions%20are%20the%20minimum%20required%20set%20of%20actions%20for%20%3CA%20href%3D%22https%3A%2F%2Flabs.azure.com%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3Ehttps%3A%2F%2Flabs.azure.com%3C%2FA%3E%20to%20successfully%20load%20for%20a%20user.%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3C%2FTBODY%3E%0A%3C%2FTABLE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EYou%E2%80%99ll%20notice%20that%20we%20only%20list%20specific%20allowed%20actions.%26nbsp%3B%20The%20advantage%20to%20this%20approach%20is%20that%20we%20can%20assign%20this%20role%20once%20to%20a%20user%20at%20the%20subscription%20or%20resource%20group%20level%20and%20that%20will%20affect%20resources%20contained%20within%20the%20subscription%20or%20resource%20group.%26nbsp%3B%20The%20Lab%20Liaison%20will%20have%20access%20see%20the%20VMs%2C%20reset%20the%20VMs%20in%20the%20Labs%20Portal%20and%20nothing%20else.%26nbsp%3B%20Lab%20Liaison%20will%20not%20be%20able%20to%20inadvertently%20set%20a%20schedule%20or%20change%20lab%20settings%20that%20affect%20the%20cost%20or%20running%20a%20lab.%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIf%20you%20are%20creating%20a%20role%20that%20is%20less%20restrictive%2C%20consider%20using%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fazure%2Frole-based-access-control%2Fcustom-roles%23wildcard-permissions%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ewildcard%20permissions%3C%2FA%3E%20in%20conjunction%20with%20the%20notAction%20section%20to%20exclude%20only%20specific%20permissions.%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-2019658393%22%20id%3D%22toc-hId-2019658393%22%3EDeploying%20Custom%20Role%3C%2FH2%3E%0A%3CP%3EThere%20are%20a%20few%20ways%20to%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Frole-based-access-control%2Fcustom-roles%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ecreate%20or%20add%20a%20custom%20role%3C%2FA%3E%20in%20Azure%20including%20using%20the%20Azure%20Portal%2C%20Azure%20CLI%20or%20PowerShell.%26nbsp%3B%20We%20are%20going%20to%20use%20PowerShell%20today.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFirst%2C%20we%20to%20create%20a%20new%20custom%20role%20object.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%24role%20%3D%20New-Object%20%60%20%0A%20-TypeName%20'Microsoft.Azure.Commands.Resources.Models.Authorization.PSRoleDefinition'%0A%24role.Id%20%3D%20%24null%0A%24role.Name%20%3D%20%22Lab%20Liaison%20Role%22%0A%24role.Description%20%3D%20%22Can%20view%20labs%20in%20Azure%20Lab%20portal%20and%20reset%20student%20VMs.%22%0A%24role.IsCustom%20%3D%20%24true%0A%24role.Actions%20%3D%20%40()%0A%24role.AssignableScopes%20%3D%20%40()%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ENext%2C%20let%E2%80%99s%20set%20the%20assignable%20scope%20to%20the%20current%20subscription.%26nbsp%3B%20(Run%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fpowershell%2Fmodule%2Faz.accounts%2FConnect-AzAccount%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EConnect-AzAccount%3C%2FA%3E%20first%2C%20if%20not%20done%20already.)%20%26nbsp%3BYou%20can%20add%20more%20than%20one%20assignable%20scope%20if%20there%20are%20several%20management%20groups%2C%20subscriptions%20and%2For%20resource%20groups%20that%20should%20have%20this%20role%20available.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%24currentSubscriptionId%20%3D%20Get-AzContext%20%60%0A%20%7C%20Select-Object%20-expand%20Subscription%20%60%0A%20%7C%20Select-Object%20-expand%20Id%0A%24role.AssignableScopes.Add(%22%2Fsubscriptions%2F%24currentSubscriptionId%22)%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ENext%2C%20let%E2%80%99s%20add%20the%20specific%20actions%20we%20need%20for%20the%20Lab%20Liaison%20Role.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%24roleAssignmentsToAdd%20%3D%20%40(%0A%22Microsoft.LabServices%2F*%2Fread%22%2C%20%0A%22Microsoft.LabServices%2FlabAccounts%2Flabs%2FenvironmentSettings%2Fenvironments%2Fdelete%22%2C%0A%22Microsoft.LabServices%2FlabAccounts%2Flabs%2Fwrite%22%2C%0A%22Microsoft.LabServices%2Flabaccounts%2FgetRestrictionsAndUsage%2Faction%22%2C%0A%22Microsoft.LabServices%2Flabaccounts%2FgetPricingAndAvailability%2Faction%22%0A)%0A%0A%24roleAssignmentsToAdd%20%7C%20%0AForEach-Object%20%7B%0A%20%20%20%20%24role.Actions.Add(%24_)%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ELastly%2C%20let%E2%80%99s%20add%20the%20newly%20created%20custom%20role%20to%20Azure.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3ENew-AzRoleDefinition%20-Role%20%24role%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EGo%20to%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2FAzure%2Fazure-devtestlab%2Fblob%2Fmaster%2Fsamples%2FClassroomLabs%2FModules%2FLibrary%2FUtilities%2FImport-LabLiaisonRole.ps1%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EImport-LabLiaisonRole.ps1%3C%2FA%3E%20to%20see%20this%20script%20in%20its%20entirety.%26nbsp%3B%20The%20full%20script%20also%20contains%20the%20ability%20to%20assign%20the%20role%20to%20several%20subscriptions%20at%20once%2C%20ability%20to%20update%20an%20existing%20role%2C%20and%20extra%20error%20checking.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-212203930%22%20id%3D%22toc-hId-212203930%22%3EAssigning%20a%20Custom%20Role%3C%2FH2%3E%0A%3CP%3EYeah!%20Our%20custom%20role%20is%20now%20available%20for%20use.%26nbsp%3B%20Now%20let%E2%80%99s%20assign%20the%20role%20to%20someone.%26nbsp%3B%20Roles%20can%20be%20assigned%20to%20users%2C%20groups%2C%20and%20service%20principals.%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20data-unlink%3D%22true%22%3EFor%20our%20example%2C%20we%20will%20assign%20a%20specific%20user%2C%20liaison%40contoso.com%2C%20%26nbsp%3Baccess%20at%20the%20resource%20group%20level.%26nbsp%3B%20The%20user%20will%20be%20able%20to%20reset%20VMs%20under%20any%20lab%20under%20any%20lab%20account%20in%20that%20resource%20group.%20Role%20assignments%20require%20the%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3EActive%20Directory%20object%20id.%26nbsp%3B%20This%20is%20the%20id%20of%20the%20user%2C%20group%2C%20or%20service%20principal%3C%2FLI%3E%0A%3CLI%3EScope%20the%20assignment%20affects.%26nbsp%3B%20For%20example%20if%20assignment%20is%20made%20at%20the%20resource%20group%20level%2C%20it%20will%20affect%20all%20resources%20in%20that%20group%2C%20including%20any%20lab%20accounts%20and%20labs%20in%20that%20resource%20group.%3C%2FLI%3E%0A%3CLI%3EName%20of%20role%20definition.%26nbsp%3B%20In%20our%20case%2C%20that%E2%80%99s%20%E2%80%98Lab%20Liaison%E2%80%99%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3EWe%20are%20going%20to%20assign%20the%20role%20to%20a%20user%2C%20so%20let%E2%80%99s%20find%20the%20id%20for%20the%20user%20first.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%23Get%20AD%20object%20id%20for%20user.%20%20Try%20both%20user%20principal%20name%20and%20email%0A%24email%20%3D%20'liaison%40contoso.com'%0A%24userAdObject%20%3D%20%24null%0A%24userAdObject%20%3D%20Get-AzADUser%20%60%0A-UserPrincipalName%20%24email.ToString().Trim()%20%60%0A-ErrorAction%20SilentlyContinue%0Aif%20(-not%20%24userAdObject)%7B%0A%24userAdObject%20%3D%20Get-AzADUser%20%60%0A%20%20%20-Mail%20%24email.ToString().Trim()%20%60%0A%20%20%20-ErrorAction%20SilentlyContinue%0A%7D%0Aif%20(-not%20%24userAdObject)%7B%0A%20%20Write-Error%20%22Couldn't%20find%20user%20'%24email'%20in%20Azure%20AD.%22%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ENext%2C%20we%20need%20to%20get%20the%20id%20of%20the%20resource%20group%2C%20so%20we%20can%20set%20the%20scope%20of%20the%20role%20assignment.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%24resourceGroupId%20%3D%20Get-AzResourceGroup%20%60%0A%20-ResourceGroupName%20'%7Bresource-group-name%7D'%20%60%0A%20%7C%20Select-Object%20-ExpandProperty%20ResourceId%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ENow%20we%20are%20all%20set%20to%20make%20the%20role%20assignment.%26nbsp%3B%20Creating%20a%20role%20assignment%20with%20the%20same%20object%20id%2C%20definition%20name%20and%20scope%20will%20throw%20an%20error%2C%20so%20we%E2%80%99ll%20only%20create%20the%20role%20assignment%20if%20it%20doesn%E2%80%99t%20exist%20already.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%24RoleDefinitionName%20%3D%20%22Lab%20Services%20Liaison%22%0Aif%20(-not%20(Get-AzRoleAssignment%20%60%0A%20-ObjectId%20%24userAdObject.Id%20%60%0A%20-RoleDefinitionName%20%24RoleDefinitionName%20%60%0A%20-Scope%20%24resourceGroupId%20%60%0A%20-ErrorAction%20SilentlyContinue))%20%7B%0A%0ANew-AzRoleAssignment%20%60%0A%20%20%20%20-ObjectId%20%24userAdObject.Id%20%60%0A%20%20%20%20-RoleDefinitionName%20%24RoleDefinitionName%20%0A%20%20%20%20-Scope%20%24resourceGroupId%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ERole%20assignments%20can%20be%20made%20with%20the%20subscription%2C%20resource%20group%2C%20lab%20account%20and%20even%20specific%20lab%20as%20the%20scope.%26nbsp%3B%20Just%20pass%20in%20the%20resource%20id%20for%20that%20resource%20to%20the%20scope%20argument.%26nbsp%3B%20Consider%20using%20the%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Flab-services%2Freference-powershell-module%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EAz.LabServices%20PowerShell%20module%20(preview)%3C%2FA%3E%20to%20make%20the%20task%20of%20getting%20resource%20id%20for%20lab%20accounts%20and%20labs%20easier.%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--1595250533%22%20id%3D%22toc-hId--1595250533%22%3EConclusion%3C%2FH2%3E%0A%3CP%3EYou%20are%20all%20set!%26nbsp%3B%20We%E2%80%99ve%20defined%2C%20imported%2C%20and%20assigned%20our%20Lab%20Liaison%20custom%20role.%26nbsp%3B%20If%20you%20want%20to%20create%20a%20role%20with%20more%20permission%2C%20refer%20back%20to%20the%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fazure-lab-services%2Fuse-custom-role-to-tailor-teachers-lab-management-permissions%2Fba-p%2F2113016%22%20target%3D%22_blank%22%3EUse%20Custom%20Role%20to%20Tailor%20Teachers'%20Lab%20Management%20Permissions%20-%20Microsoft%20Tech%20Community%3C%2FA%3E%20blog%20post.%26nbsp%3B%20It%20has%20a%20nice%20list%20of%20Lab%20Services%20permissions%20and%20their%20purpose.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWe%20hope%20that%20you%20find%20this%20post%20helpful!%3C%2FP%3E%0A%3CP%3E~Az%20Labs%20team%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-2323761%22%20slang%3D%22en-US%22%3E%3CP%3EAzure%20provides%20a%20few%20built-in%20roles%20to%20allow%20or%20deny%20actions%20for%20Azure%20Lab%20Services.%26nbsp%3B%20These%20built-in%20roles%20include%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fazure%2Frole-based-access-control%2Fbuilt-in-roles%23owner%22%20rel%3D%22noopener%20noreferrer%22%20target%3D%22_blank%22%3Eowner%3C%2FA%3E%2C%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fazure%2Frole-based-access-control%2Fbuilt-in-roles%23contributor%22%20rel%3D%22noopener%20noreferrer%22%20target%3D%22_blank%22%3Econtributor%3C%2FA%3E%2C%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fazure%2Frole-based-access-control%2Fbuilt-in-roles%23lab-creator%22%20rel%3D%22noopener%20noreferrer%22%20target%3D%22_blank%22%3Elab%20creator%3C%2FA%3E%2C%20and%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fazure%2Frole-based-access-control%2Fbuilt-in-roles%23reader%22%20rel%3D%22noopener%20noreferrer%22%20target%3D%22_blank%22%3Ereader%3C%2FA%3E.%26nbsp%3B%20If%20the%20built-in%20roles%20don%E2%80%99t%20fit%20your%20needs%2C%20you%20can%20also%20create%20and%20deploy%20a%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Frole-based-access-control%2Fcustom-roles%22%20rel%3D%22noopener%20noreferrer%22%20target%3D%22_blank%22%3Ecustom%20role%3C%2FA%3E.%20That%20is%20what%20we%20will%20do%20in%20this%20blog%20post.%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2323761%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3ELab%20Services%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Co-Authors
Version history
Last update:
‎Jun 14 2021 09:39 AM
Updated by: