Reputation: 79
Running a PowerShell script to update user attributes. I am using a test user to start with but everything seems in line. I tried the list file as both an Excel and CSV and get the same error. I matched up the data with the AD attributes as best as possible for simplicity. I'm missing something but cant see where it is.
Excel\CSV Data:
PowerShell code:
$users2 = import-excel "c:\orgstafflist2.xlsx"
foreach ($user in $users2) {get-aduser -filter "SamAccountName -eq '$($user.samaccountname)'" | set-aduser -displayname $($user.name) -surname $($user.last) -givenname $($user.first) -replace @{manager=$($user.manager)} -description $($user.description)}
Error Message:
set-aduser : The name reference is invalid
At line:1 char:97
+ ... ntname)'" | set-aduser -displayname $($user.displayname) -surname $($user.la ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (CN=John Doe,OU=...riese,DC=office:ADUser) [Set-ADUser], ADException
+ FullyQualifiedErrorId : ActiveDirectoryServer:8373,Microsoft.ActiveDirectory.Management.Commands.SetADUser
Upvotes: 0
Views: 776
Reputation: 8868
First issue is your displayname column you try to reference with name in error. Second issue is you need to provide an AD user distinguished name for the manager.
$users2 = import-excel "c:\orgstafflist2.xlsx"
foreach ($user in $users2)
{
get-aduser -filter "SamAccountName -eq '$($user.samaccountname)'" |
set-aduser -displayname $($user.displayname) -surname $($user.last) -givenname $($user.first) -replace @{manager = $(get-aduser $user.Manager).distinguishedname} -description $($user.description)
}
You should also consider splatting for readability.
$users2 = import-excel "c:\orgstafflist2.xlsx"
foreach ($user in $users2)
{
$UserParam = @{
displayname = $user.displayname
surname = $user.last
givenname = $user.first
replace = @{manager = (get-aduser $user.Manager).distinguishedname}
description = $user.description
}
get-aduser -filter "SamAccountName -eq '$($user.samaccountname)'" | set-aduser @UserParam
}
Upvotes: 1