hellblazeruk
asked on
powershell to copy data from AD attribute
Hi,
I'm trying to see if its possible to copy a telephone number from the homePhone attribute to extensionAttribute1 for every user within my domain and then schedule the 'sync' each day, can this be done?
I'm trying to see if its possible to copy a telephone number from the homePhone attribute to extensionAttribute1 for every user within my domain and then schedule the 'sync' each day, can this be done?
Better to filter directly at the source (that is, the DC), instead of retrieving all user accounts and then filtering in PS:
Get-ADUser -Filter "homePhone -like '*'" -Property homePhone | ForEach-Object {Set-ADUser -Identity $_.SamAccountName -Replace @{'extensionAttribute1' = $_.homePhone}}
I was assuming most of the accounts are to be changed anyway, so it should not matter, but in general oBdA is correct. With exception of the unnecessary ForEach, that is.
It would even get better if we were able to check for differences in the filter, but we can only compare against values, not properties ...
It would even get better if we were able to check for differences in the filter, but we can only compare against values, not properties ...
Not unnecessary, since the pipeline variable is referenced.
Try your script, it won't work:
Try your script, it won't work:
PS C:\PS> Get-ADUser -Filter * -Property homePhone | ? { $_.homePhone } | Set-ADUser -Replace @{extensionAttribute1 = $_.homePhone}
Set-ADUser : Cannot validate argument on parameter 'Replace'. The argument is null or an element of the argument collection contains a null value.
And for the sake of completeness, this one would clear extensionAttribute1 for all users were homePhone is empty, but extensionAttribute1 isn't:
Get-ADUser -Filter "(extensionAttribute1 -like '*') -and -not (homePhone -like '*')" | Set-ADUser -Clear extensionAttribute1
@#~*!!!
ASKER
Hi,
Thanks for the quick responses
Set-ADUser : Cannot validate argument on parameter 'Replace'. The argument is null or an element of the argument collection contains a null value.
At line:1 char:85
+ ... mePhone } | Set-ADUser -Replace @{extensionAttribute4 = $_.homePhone}
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~ ~
+ CategoryInfo : InvalidData: (:) [Set-ADUser], ParameterBindingValidation Exception
+ FullyQualifiedErrorId : ParameterArgumentValidatio nError,Mic rosoft.Act iveDirecto ry.Managem ent.Comman ds.SetADUs er
Regards,
Thanks for the quick responses
Get-ADUser -Filter * -Property homePhone | ? { $_.homePhone } | Set-ADUser -Replace @{extensionAttribute4 = $_.homePhone}
did return an error Set-ADUser : Cannot validate argument on parameter 'Replace'. The argument is null or an element of the argument collection contains a null value.
At line:1 char:85
+ ... mePhone } | Set-ADUser -Replace @{extensionAttribute4 = $_.homePhone}
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Set-ADUser], ParameterBindingValidation
+ FullyQualifiedErrorId : ParameterArgumentValidatio
Get-ADUser -Filter "(extensionAttribute1 -like '*') -and -not (homePhone -like '*')" | Set-ADUser -Clear extensionAttribute1
executed as normal but the homePhone number did not copy to extensionAttribute1 ?Regards,
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Open in new window
would do the latter, but only if there is a homePhone set. This also means that you cannot remove the property by just emptying homePhone.