Trying to see if there is a way to show users name or emp number when a user is not updated.

I am using the employeeid as the anchor point / source.

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

ForEach-Object {Get-ADUser -Filter "employeeid -eq `"$($_.employeeid)`"" |`
 Set-ADUser -description $_.Jobtitle`
  -title $_.Jobtitle`
  -department $_.HomeDepartmentDescription`
  -replace @{extensionAttribute1 = $_.positionid;`
   physicalDeliveryOfficeName = $_.LocationCode }`
 }

This is the basic script I am using to update a few AD attributes, for the most part it works. Just now and then it errors out because the csv file might be missing something, it happens. I would just like to see / know what user I need to go back to and look at.

5 Spice ups

EDIT: fixed spelling - is it sad that my English is worse than my PowerShell…

that’s what try/catch is there for, wrap it in try catch with erroraction stop.

also please look into splatting, don’t use backtick

e.g.

$csv = Import-Csv "E:\scripts\UpdateADusers\update_file.csv"

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

    try{
        $set = @{
            Identity    = $Aduser.samaccountname
            description = $user.Jobtitle
            title       = $user.Jobtitle
            department  = $user.HomeDepartmentDescription
            replace     = @{
                extensionAttribute1        = $user.positionid
                physicalDeliveryOfficeName = $user.LocationCode
            }                
        }
        set-aduser @set -ErrorAction Stop
        Write-Output "User '$($user.employeeid)' updated"
    }
    catch{
        write-output "Error Updating user '$($user.employeeid)'"
        $error[0].Exception.Message
    }
}

3 Spice ups

Not really trying to stop the script, rather it keep running. Just instead of :

Set-ADUser : replace                                                                                                                
At E:\scripts\UpdateADUsers\update_title_description.ps1:20 char:2                                                                  
+  Set-ADUser -description $_.Jobtitle`                                                                                             
+  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~                                                                                             
    + CategoryInfo          : InvalidOperation: (CN=phil test,OU...selfdomain,DC=local:ADUser) [Set-ADUser], ADInvalidOperationException
    + FullyQualifiedErrorId : ActiveDirectoryServer:0,Microsoft.ActiveDirectory.Management.Commands.SetADUser                       

It either list the name of the user or the employee number.

-backtick is a little easier for me on short scripts

error action stops the action, not the script, so if it can’t set the info for the user it moves on to the next user.

it stops the individual action, not the script.

it’s bad practise, what you do, is up to you ¯_(ツ)_/¯

you can output whatever you want, you can save the ones that threw an error to a variable and then export that so you know which ones you have to fix.

that however needs more code, I just gave you a sample. We’re happy to help, but not a script-writing service.

Give it a try, if you get stuck, feel free to post the code you have tried and we’ll go from there.

Your error contains what user it failed on:

InvalidOperation: (CN=phil test,OU...selfdomain,DC=local:ADUser) [Set-ADUser]

“phil test”

You can also use the $errors variable and look back over the most recent error i.e. $errors[0]

A try / catch is the best way as Neally demonstrated. That way you can output whatever you want if there’s an error.