1

I'm trying to update AzureADGroupMember for multiple users in a CSV File by UPN.

This is what I've came across and attempted:

$users = Import-csv "C:\Temp\testgroup2.csv" 

$users | ForEach-Object{
Add-AzureGroupADGroupMember -ObjectId xcv9890-stest999-xcvxc234-xcv2342324 
-RefObjectId (Get-AzureADUser -ObjectId $_.UPN).ObjectId
}

I get the following 2 errors.

Import-csv : The member "Role" is already present.
At line:1 char:10

Get-AzureADUser : Cannot bind argument to parameter 'ObjectId' because it 
is null.
At line:4 char:120

Any idea why this keeps happening? I would really appreciate the help.

Thank you,

Joy Wang
  • 39,905
  • 3
  • 30
  • 54
  • If my reply is helpful, please mark it as the answer(on the left of my reply, there is an option to mark), thanks. – Joy Wang Apr 17 '19 at 11:20
  • Joy Wang, this was very helpful and helped me eliminate the Role error message and it helped me update the employees by UPN! But i have another question, what if i wanted to update by EmployeeID instead of UPN? I tried replacing $_.UPN with $._EmployeeID, but I got the following error messages. Get-AzureADUser : Error occurred while executing GetUser Code: Request_ResourceNotFound Message: Resource '18616' does not exist or one of its queried reference- property objects are not present. @JoyWang – Larry David Apr 17 '19 at 13:52
  • i also verified that the users had employee ID's with the following: Get-AzureADUser -ObjectID Xxxxx@hxxxxxx.com | Select-Object * – Larry David Apr 17 '19 at 14:11
  • Hi, Larry, according to the policy of stackoverflow, you should avoid to ask the different questions in one post. If it is helpful, please mark it as the answer, on the left of my reply, there is an option to mark. Then ask your question in another new post, I will look into it. – Joy Wang Apr 17 '19 at 14:20
  • @JoyWang fair enough! thank you for the UPN help! – Larry David Apr 17 '19 at 14:50

1 Answers1

0

Well, since you did not provide the .csv file, I can just give you a solution that works on my side.

First, let's check the two errors.

Import-csv : The member "Role" is already present.

I can reproduce this issue, this means your .csv file has two columns header 'Role', just modify it into a correct format.

enter image description here

enter image description here

Get-AzureADUser : Cannot bind argument to parameter 'ObjectId' because it is null.

This means this part $_.UPN is null, this may be caused by the first error.

My sample:

testgroup.csv

UPN,Role
leeliu@xxxxxx.onmicrosoft.com,role1
test@xxxxxx.onmicrosoft.com,role2

enter image description here

script (you should use Add-AzureADGroupMember, not Add-AzureGroupADGroupMember):

$users = Import-csv "C:\Users\joyw\Desktop\testgroup.csv" 

$users | ForEach-Object{
Add-AzureADGroupMember -ObjectId 9d42xxxxxx28b600ad -RefObjectId (Get-AzureADUser -ObjectId $_.UPN).ObjectId
}

Note: Before running the script, you need to make sure the users are not already in the group, otherwise you will get a One or more added object references already exist for the following modified properties: 'members' error.

Joy Wang
  • 39,905
  • 3
  • 30
  • 54