Need help using CSV file with script to add users to an enterprise application in Azure

The script below works fine for adding users to a enterprise application with azure, but I have not been able to figure out how to use this with a CSV list of users, any help appreciated.

$username = "user@domain.com"
$appname = "ENT_App"
$spo = Get-AzureADServicePrincipal -Filter "Displayname eq '$appname'"
$user = Get-AzureADUser -ObjectId $username
New-AzureADUserAppRoleAssignment -ObjectId $user.ObjectId -PrincipalId $user.ObjectId -ResourceId $spo.ObjectId -Id $spo.Approles[1].id

@tuser Welcome to PowerShell.org forums.

The simplest approach is to know Import-CSV and Foreach-Object cmdlet.
You can import the csv and iterate through the rows one at a time using Foreach-Object or foreach. Below links will help you to start.

1 Like

I made the changes below, it is reading the users from CSV file, but now it is erroring when adding to the application, this works fine if I specify a user, not sure what the difference could be now, error below, if i look at $user it is correct all info there

Script==

$upn = Import-Csv -Path C:\Temp\APPUsers1.csv | ForEach {
    $UPN=$_.upn
    $appname = "<ENT_App>"
	$spo = Get-AzureADServicePrincipal -Filter "Displayname eq '$appname'"
	$user = Get-AzureADUser -ObjectId $_.upn
	New-AzureADUserAppRoleAssignment -ObjectId $user.ObjectId -PrincipalId $user.ObjectId -ResourceId $spo.ObjectId -Id $spo.Approles[1].id
}

Error==Cannot index into a null array.
At line:1 char:1
+ New-AzureADUserAppRoleAssignment -ObjectId $user.ObjectId -PrincipalI ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : NullArray

Im good, the <> around the app name causing the issue, thanks for your help