<\/div>\n<\/aside>\n\n
e.g.<\/p>\n
$csv = Import-Csv \"E:\\scripts\\UpdateADusers\\update_file.csv\"\n\nforeach($user in $csv){\n $ADUser = $null\n $ADUser = Get-ADUser -Filter \"employeeid -eq '$($user.employeeid)'\"\n\n try{\n $set = @{\n Identity = $Aduser.samaccountname\n description = $user.Jobtitle\n title = $user.Jobtitle\n department = $user.HomeDepartmentDescription\n replace = @{\n extensionAttribute1 = $user.positionid\n physicalDeliveryOfficeName = $user.LocationCode\n } \n }\n set-aduser @set -ErrorAction Stop\n Write-Output \"User '$($user.employeeid)' updated\"\n }\n catch{\n write-output \"Error Updating user '$($user.employeeid)'\"\n $error[0].Exception.Message\n }\n}\n\n<\/code><\/pre>","upvoteCount":3,"datePublished":"2022-07-12T13:20:48.000Z","url":"https://community.spiceworks.com/t/show-error-when-updating-ad-user/930857/2","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"Not really trying to stop the script, rather it keep running. Just instead of :<\/p>\n
Set-ADUser : replace \nAt E:\\scripts\\UpdateADUsers\\update_title_description.ps1:20 char:2 \n+ Set-ADUser -description $_.Jobtitle` \n+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \n + CategoryInfo : InvalidOperation: (CN=phil test,OU...selfdomain,DC=local:ADUser) [Set-ADUser], ADInvalidOperationException\n + FullyQualifiedErrorId : ActiveDirectoryServer:0,Microsoft.ActiveDirectory.Management.Commands.SetADUser \n<\/code><\/pre>\nIt either list the name of the user or the employee number.<\/p>\n
-backtick is a little easier for me on short scripts<\/p>","upvoteCount":0,"datePublished":"2022-07-12T13:59:51.000Z","url":"https://community.spiceworks.com/t/show-error-when-updating-ad-user/930857/3","author":{"@type":"Person","name":"Phillip1661","url":"https://community.spiceworks.com/u/Phillip1661"}},{"@type":"Answer","text":"
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.<\/p>\n
it stops the individual action, not the script.<\/p>","upvoteCount":0,"datePublished":"2022-07-12T14:04:15.000Z","url":"https://community.spiceworks.com/t/show-error-when-updating-ad-user/930857/4","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"\n\n
<\/div>\n
Phillip1661:<\/div>\n
\nbacktick is a little easier for me on short scripts<\/p>\n<\/blockquote>\n<\/aside>\n
it’s bad practise, what you do, is up to you ¯_(ツ)_/¯<\/p>","upvoteCount":0,"datePublished":"2022-07-12T14:04:59.000Z","url":"https://community.spiceworks.com/t/show-error-when-updating-ad-user/930857/5","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"\n\n
<\/div>\n
Phillip1661:<\/div>\n
\nIt either list the name of the user or the employee number.<\/p>\n<\/blockquote>\n<\/aside>\n
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.<\/p>\n
that however needs more code, I just gave you a sample. We’re happy to help, but not a script-writing service.<\/p>\n
Give it a try, if you get stuck, feel free to post the code you have tried and we’ll go from there.<\/p>","upvoteCount":0,"datePublished":"2022-07-12T14:06:13.000Z","url":"https://community.spiceworks.com/t/show-error-when-updating-ad-user/930857/6","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"
Your error contains what user it failed on:<\/p>\n
InvalidOperation: (CN=phil test,OU...selfdomain,DC=local:ADUser) [Set-ADUser]\n<\/code><\/pre>\n“phil test”<\/p>\n
You can also use the $errors variable and look back over the most recent error i.e. $errors[0]<\/p>\n
A try / catch is the best way as Neally demonstrated. That way you can output whatever you want if there’s an error.<\/p>","upvoteCount":0,"datePublished":"2022-07-12T15:12:34.000Z","url":"https://community.spiceworks.com/t/show-error-when-updating-ad-user/930857/7","author":{"@type":"Person","name":"saidbrandon","url":"https://community.spiceworks.com/u/saidbrandon"}}]}}
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
Neally
(Neally)
July 12, 2022, 1:20pm
2
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
Neally
(Neally)
July 12, 2022, 2:04pm
4
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.
Neally
(Neally)
July 12, 2022, 2:04pm
5
it’s bad practise, what you do, is up to you ¯_(ツ)_/¯
Neally
(Neally)
July 12, 2022, 2:06pm
6
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.