Bulk update Azure AD from Powershell - FAILED to update

Copper Contributor

Trying to run script to bulk update users attributes in AAD. 

getting error (tried with multiple users)

what am I doing wrong here?

 

see below the script, the csv and the error

 

 

 

$CSVrecords = Import-Csv C:\Temp\Test.csv -Delimiter ","

$SkippedUsers = @()
$FailedUsers = @()


foreach ($CSVrecord in $CSVrecords) {
    $upn = $CSVrecord.UserPrincipalName
    $user = Get-AzureADUser -Filter "userPrincipalName eq '$upn'"  
    if ($user) {
        try{
        $user | Set-AzureADUser -jobTitle $CSVrecord.jobTitle -department $CSVrecord.department -officeLocation $CSVrecord.officeLocation -streetAddress $CSVrecord.streetAddress -city $CSVrecord.city -state $CSVrecord.state -postalCode $CSVrecord.postalCode -telephoneNumber $CSVrecord.telephoneNumber -mobile $CSVrecord.mobile
        } catch {
        $FailedUsers += $upn
        Write-Warning "$upn user found, but FAILED to update."
        }
    }
    else {
        Write-Warning "$upn not found, skipped"
        $SkippedUsers += $upn
    }
}
userPrincipalName,jobTitle,department,officeLocation,streetAddress,city,state,postalCode,telephoneNumber,mobile
email address removed for privacy reasons,Title Test,Dept Test,Office Test,Street Test,City Test,State Test,Postcode Test,0111 1111111,07111 111111
WARNING: email address removed for privacy reasons user found, but FAILED to update.

 

 

 

 

3 Replies

@JasonG91 

 

Hi, Jason.

 

We'd need to know what the real error thrown by Set-AzureADUser is rather than the generic error you're reporting from the catch block to have any idea of where to start.

 

As an important side note, the AzureAD modules are now deprecated (along with others) and you should be moving to the replacemenet Microsoft.Graph.* modules.

 

 

Cheers,

Lain

@LainRobertson  

 

I would love to know the error but it just isnt giving one. 

 

ill look into Microsoft Graph, Thanks 

@JasonG91 

 

Here's your script with the try...catch block removed - perhaps save it as a different name while you diagnose the root issue.

 

If you run this version, you should see the real error(s) being listed.

 

If your CSV is large, consider reducing it in size for this diagnostic-only version of the script, as there's no value in having a thousand of the same error popping up.

 

$CSVrecords = Import-Csv C:\Temp\Test.csv -Delimiter ","

$SkippedUsers = @()
$FailedUsers = @()


foreach ($CSVrecord in $CSVrecords) {
    $upn = $CSVrecord.UserPrincipalName
    $user = Get-AzureADUser -Filter "userPrincipalName eq '$upn'"  
    if ($user) {
        $user | Set-AzureADUser -jobTitle $CSVrecord.jobTitle -department $CSVrecord.department -officeLocation $CSVrecord.officeLocation -streetAddress $CSVrecord.streetAddress -city $CSVrecord.city -state $CSVrecord.state -postalCode $CSVrecord.postalCode -telephoneNumber $CSVrecord.telephoneNumber -mobile $CSVrecord.mobile
    }
    else {
        Write-Warning "$upn not found, skipped"
        $SkippedUsers += $upn
    }
}

 

Cheers,

Lain