Forum Discussion
Mike_F_MF
Jun 04, 2021Copper Contributor
Looking to run a Powershell update for Azure from a list.
c:\Temp\userlist.csv looks like user@testdomain.com mailto:user2@domain.com # Assign the values to the variables $username = get-content -path "c:\Temp\userlist.csv" $app_name = "custom...
AndySvints
Jun 07, 2021Steel Contributor
Hello Mike_F_MF,
I think you have a problem in your Foreach-Object statement.
If you want to use ForEach-Object you need to pipe (|) some collection to it.
In your case you can run something like this:
$username | ForEach-Object{
# Get the user to assign, and the service principal for the app to assign to
$user = Get-AzureADUser -ObjectId $_
$sp = Get-AzureADServicePrincipal -Filter "displayName eq '$app_name'"
$appRole = $sp.AppRoles | Where-Object { $_.DisplayName -eq $app_role_name }
# Assign the user to the app role
New-AzureADUserAppRoleAssignment -ObjectId $user.ObjectId -PrincipalId $user.ObjectId -ResourceId $sp.ObjectId -Id $appRole.Id
}Another option would be to use foreach($item in $Collection){...} :
foreach($u in $username){
# Get the user to assign, and the service principal for the app to assign to
$user = Get-AzureADUser -ObjectId $u
$sp = Get-AzureADServicePrincipal -Filter "displayName eq '$app_name'"
$appRole = $sp.AppRoles | Where-Object { $_.DisplayName -eq $app_role_name }
# Assign the user to the app role
New-AzureADUserAppRoleAssignment -ObjectId $user.ObjectId -PrincipalId $user.ObjectId -ResourceId $sp.ObjectId -Id $appRole.Id
}Hope that helps.