Skip to content

Instantly share code, notes, and snippets.

@jaycollett
Created October 5, 2021 20:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaycollett/cac17ec1edf7170dfc32f94219b388af to your computer and use it in GitHub Desktop.
Save jaycollett/cac17ec1edf7170dfc32f94219b388af to your computer and use it in GitHub Desktop.
Bulk add users to Azure Application Role
# file with a header of userprincipalname and a list of UPNs to add to the specified app and role
$data = import-csv C:\temp\userstoadd.csv
# app and role names
$app_name = "YOUR APP NAME HERE"
$app_role_name = "YOUR APP ROLE HERE"
# Connect to Azure AD
Connect-AzureAD -Confirm
$sp = Get-AzureADServicePrincipal -Filter "displayName eq '$app_name'"
$appRole = $sp.AppRoles | Where-Object { $_.DisplayName -eq $app_role_name }
foreach ($userToAdd in $data){
try{
$user = Get-AzureADUser -ObjectId $userToAdd.userprincipalname
New-AzureADUserAppRoleAssignment -ObjectId $user.ObjectId -PrincipalId $user.ObjectId -ResourceId $sp.ObjectId -Id $appRole.Id
}catch{
Write-Warning -Message "Could not add user to group"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment