Welcome › Forums › General PowerShell Q&A › change aduser attributes
- This topic has 14 replies, 5 voices, and was last updated 1 week, 4 days ago by
Participant.
-
AuthorPosts
-
-
December 28, 2020 at 11:05 am #282715
Hello,
I need to change existing values in multiple aduser, attribute fields.
Only when I run this script Powershell does not return a error, unfortunately it also does not change anything.I have a imported csv file, with adusers and a to be replaced /filled Atrribute field and its new value
Example:Samaccountname : adm-test-RevVDisplayname : Revel VinDescription : Admin account Servicedesk CTASK0902707Emailaddress : [email protected]Company : test-CompanyWhen I use a set-aduser -replace on a test account it works fine.
In the foreach loop is looks fine only it does not change any attributeI am not sure what I am missing at this moment.PowerShell12345678910111213141516$Users = import-csv .\Output\Beheeraccounts1.csv -Delimiter ";"$Sam = ($Users).samaccountname$Display = ($Users).Displayname$Description =($Users).Description$Emailaddress = ($Users).Emailaddress$Company = ($Users).Companyforeach($user in $sam){{Get-ADUser -Filter "SamAccountName -eq '$($Sam)'" |Set-aduser -Replace @{$Displayname= $($Display)}{Get-ADUser -Filter "SamAccountName -eq '$($Sam)'" |Set-aduser -Replace @{$Description = $($Description)}{Get-ADUser -Filter "SamAccountName -eq '$($Sam)'" |Set-aduser -Replace @{$Emailadress = $($Emailaddress)}{Get-ADUser -Filter "SamAccountName -eq '$($Sam)'" |Set-ADUser -Identity $_ -Company $Company}}#}}}Since the company attribute field is empty, I use the -add instead of – replace.
thx
-
December 28, 2020 at 11:48 am #282727
There are a couple of things wrong.
- It appears you want to process each row for a user. When you use the dot notation (e.g ($Users).samaccountname), that is creating an array of just samAccountNames. The only data you have in your loop is that sam, so you do not have the rest of the row (DisplayName, Description, etc.) for the user.
- The Set-ADUser should be called one time, with the Server set to the DC you want to write to, otherwise it will be updating all over the place and you can’t validate until replication is complete.
- The Set-ADUser replace is a hashtable with the AD Attribute you want to set and the value you want to set. The code you posted has a variable setting a variable
- Recommend collecting the results. You have a row with the user data, so you can just append a ‘Status’ to capture success\issues so you can review what happened for each user
PowerShell123456789101112131415161718192021222324252627282930313233343536$Users = import-csv .\Output\Beheeraccounts1.csv -Delimiter ";"#for each row in the csv...$results = foreach ( $user in $users ){$sam = $user.samaccountname#try to find the user$adUser = Get-ADUser -Filter {SamAccountName -eq $sam}#if you find the user, e.g. $adUser is not null...if ($adUser) {#the set can be a single call#in the replace, you need to reference the AD attribute name and then a variable$params = @{Identity = $adUserCompany = $user.CompanyServer = "My_Domain_Controller"ErrorAction = 'Stop'Replace = @{Displayname = $user.DisplayNameDescription = $user.DescriptionMail = $user.Emailaddress}}try {Set-ADUser @params$user | Select-Object -Property *, {Name='Status';Expression={'Success'}}}catch {#Set failed$setErr = $_$user | Select-Object -Property *, {Name='Status';Expression={'Failed: {0}' -f $setErr}}}}else {$user | Select-Object -Property *, {Name='Status';Expression={'Not_Found'}}}} -
December 29, 2020 at 3:58 am #282883
@Rob Simmers,
Thank you for the reply, I will “investigate” this solution, and compare it with that what I did sofar.
Copy paste is one thing, to see the difference and knowing why is more my goal. 🙂 -
December 29, 2020 at 5:58 pm #283108PowerShell1234567891011121314151617181920212223#Names in upper case come from input csv#Keys of user email address provided in csv$creds = Get-Credential$server = 'adserver.contoso.com'$users = import-csv '.\users.csv'ForEach ($u in $users) {try {$email = $u.EMAIL$user = Get-ADUser -Credential $creds -Server $server -Filter {EmailAddress -eq $email} -Properties *$user.Title = $u.TITL$user.EmployeeID = $u.EMPID$user.Department = $u.DEPARTMENT$user.Division = $u.DIVSet-ADUser -Instance $user}catch {Write-Warning $Error[0]Continue}}
-
December 29, 2020 at 6:27 pm #283111
Rob, I have this same question. In your answer code, can I still use the “REPLACE” block if my Users’ (extension) attributes are blank?
-
December 30, 2020 at 10:07 am #283258
Jeff, Have you tried testing it on a single user with a single attribute? Key or lookup attribute to find the user.
-
December 30, 2020 at 1:47 pm #283315
@Rob, yes I’m trying with this code:
PowerShell1234567$Users = import-csv .\Users.csv -Delimiter ";"#for each row in the csv...$results = foreach ( $user in $users ){$sam = $user.samaccountname }#try to find the user$adUser = Get-ADUser -Filter {SamAccountName -eq $sam}..but it’s balking that: “Get-ADUser : Variable: ‘sam’ found in expression: $sam is not defined.”
My $Users variable is properly formatted and import-csv is working with it (for one test user)
-
-
January 4, 2021 at 9:46 am #284005
$results = foreach ( $user in $users ){
#try to find the user
$adUser = Get-ADUser $user.samaccountname }you have your aduser query outside of your foreach loop.
as well, you don’t need to utilize the filter on your get-aduser statement, get-aduser accepts samaccountname for the identity value-
January 4, 2021 at 11:54 am #284056
David, even with these tweaks:
PowerShell123456$Users = import-csv .\Users.csv -Delimiter ";"#for each row in the csv...$results = foreach ( $user in $users ){#try to find the user$adUser = Get-ADUser $user.samaccountname}..I get the error:
PowerShell1Get-ADUser : Cannot validate argument on parameter 'Identity'. The argument is null. Provide a valid value for the argument, and then try running the command again.I do have value(S) inside of $adUser including one for SamAccountName (I’m testing with one User)
-
-
January 4, 2021 at 12:12 pm #284062
The users.csv contains users with a samaccountname? If it’s a single user, try wrapping $users in @() to ensure it’s an array:
PowerShell1$results = foreach ( $user in @($users) ){...Note that I normally use a -Filter to find a user rather than -Identity to connect to a user. Filter you are checking to see if a result returned (e.g. If ($aduser)…) whereas -Identity you have to use a try\catch to capture the error if the user doesn’t exist.
-
January 5, 2021 at 3:43 pm #284383
yep, have samAccoutnName in csv and per your first suggestion, testing with one user.
Revised code:
PowerShell123456$Users = import-csv .\Users.csv -Delimiter ";"#for each row in the csv...$results = foreach ($user in @($users)){#try to find the user$adUser = Get-ADUser -identity $user.samaccountname}Error still on both Get_ADUser with -identity (and -filter)
PowerShell12Get-ADUser : Cannot validate argument on parameter 'Identity'. The argument is null. Provide a valid value for the argument, andthen try running the command again.I got my work done with Obijuan’s code but still trying to do same thing with yours for the sake of the education. Thanks
-
This reply was modified 1 week, 6 days ago by
Jeff Taylor.
-
This reply was modified 1 week, 6 days ago by
-
-
January 6, 2021 at 9:26 am #284488
Let’s do some basic 101 troubleshooting. Both errors indicate you are passing NULL or nothing to the parameters. The csv content has not been shared.
PowerShell12345678$csv = @"samAccountNameuser1234"@ | ConvertFrom-Csvforeach ($user in @($csv)) {$user.SamAccountName}I always highlight the code that is importing the csv and check it:
PowerShell12345678910PS C:\Users\rasim> $csv = @"samAccountNameuser1234"@ | ConvertFrom-CsvPS C:\Users\rasim> $csvsamAccountName--------------user1234Before I start writing code for Active Directory or anything else, I verify my for loop is getting my data:
PowerShell12345PS C:\Users\rasim>foreach ($user in @($csv)) {$user.SamAccountName}user1234-
January 7, 2021 at 5:18 pm #284692
Yes, all that checks out on my end. I do Return the samAccountName from the $csv > $user variable.
-
-
-
AuthorPosts
- You must be logged in to reply to this topic.