Created
October 5, 2021 20:12
-
-
Save jaycollett/cac17ec1edf7170dfc32f94219b388af to your computer and use it in GitHub Desktop.
Bulk add users to Azure Application Role
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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