azureazure-active-directoryazure-powershellazure-ad-powershell-v2

Updating Multiple Users ADGroup by EmployeeID


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

This is what I've got assisted with, but looking to have it update by UPN instead of EmployeeID. This was the successful code that updates ADGroupMember by UPN.

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

$users | ForEach-Object{
Add-AzureADGroupMember -ObjectId 599992-xxxxxxxxxx-699999e9e - 
RefObjectId (Get-AzureADUser -ObjectId $_.UPN).ObjectId
}

This is the code where I changed UPN to update by EmployeeID in the CSV.

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

$users | ForEach-Object{
Add-AzureADGroupMember -ObjectId 599992-xxxxxxx-6ee9999e - 
RefObjectId (Get-AzureADUser -ObjectId $_.EmployeeID).ObjectId
}

This is the error message I get when trying to update by EmployeeID.

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. 

This is what I used to verify that the employee actually has an EmployeeID in Azure.

Get-AzureADUser -ObjectID Xxxxx@hxxxxxx.com | Select-Object *

Any idea why it's reading that the employeeID doesn't exist in Azure even though I've verified?

Thank you,

Update: Adding screenshot of my csv setup, I only have Employee ID in there: CSV Setup

Update 2: Screenshot of the script I'm running in powershell: Script in PS


Solution

  • The employeeId is not the same with ObjectId, so you could not pass employeeId to the ObjectId property.

    Try the script as below, it works fine on my side.

    $users = Import-csv "C:\Users\joyw\Desktop\testgroup.csv" 
    foreach($user in $users){
        $refobjectid = (Get-AzureADUser | Where-Object {$_.ExtensionProperty.employeeId -eq $user.employeeId}).ObjectId
        Add-AzureADGroupMember -ObjectId 9d42d3ea-xxxxxxxx-c31428b600ad -RefObjectId $refobjectid
    }
    

    My .csv file:

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

    enter image description here