Am getting error(s) when running powershell script to update AD user attributes.
Little strange I never got errors unless if could not find user, but now it seems to cycling though a few times creating a log file with over 2 errors logged.

Script used to update user attributes

Import-Csv 'E:\scripts\UpdateADusers\ADUpdate.csv' |

ForEach-Object {Get-ADUser -Filter "employeeid -eq `"$($_.badgeid)`"" |`
 Set-ADUser -replace @{
 extensionAttribute1 = $_.positionid
 title = $_.jobtitle
 department = $_.homedepartmentdescription
 description = $_.jobtitle
 physicalDeliveryOfficeName = $_.ADLocation
 employeenumber = $_.associateID
 
  }
  }

Nothing to nutty

Csv layout
column-A=badgeID
column-B=posutionID
column-C=assicuateID
column-D=jobtitle
column-E=homedepartmentdescription
column-F=ADLocation

Currently only 1 user’s data listed for extra testing. Even though it errors out on output it still updates the AD user details.

Here is error log - I stopped it from running just a few seconds created 500 lines (using notepad++)
ad_error.txt (6.4 KB)

Thoughts?

(i can run the script and update items and deal with errors later but little extra checks until figure this out)

2 Spice ups

When I have a few min I’ll dig into the code, but my first recommendation is your input file should have exactly one test user in it and you should be able to successfully run against that user before you try and mass update AD.

2 Spice ups

Get-ADUser : The search filter cannot be recognized

Do you have the RSAT tools installed on the machine you are running this on?
Are you running it as a user who has AD rights?

I’d try something like so:

$csv = Import-Csv 'E:\scripts\UpdateADusers\ADUpdate.csv'

foreach($user in $csv){
    $ad = $null
    $ad = Get-ADUser -Filter "employeeid -eq '$($user.badgeid)'" 

    if($ad){
        $updateUser = @{
            identity       = $ad
            title          = $user.jobtitle
            department     = $user.homedepartmentdescription
            description    = $user.jobtitle
            employeenumber = $user.associateID
            replace        = @{ 
                extensionAttribute1        = $user.positionid
                physicalDeliveryOfficeName = $user.ADLocation
            }
        }
        
        try{
            Set-ADUser @updateUser -ErrorAction Stop
        }
        catch{
            $Error[0].Exception.Message
        }
    }

}
4 Spice ups

Neally is spot on. Import your CSV as a variable, then loop through that and then you should be able to use the $_ for your columns.