Link to home
Start Free TrialLog in
Avatar of hellblazeruk
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?
Avatar of Qlemo
Qlemo
Flag of Germany image

Yes, of course. Do you want to change only those with no extensionAttribute1, or do you need to overwrite on each run?
Get-ADUser -Filter * -Property homePhone | ? { $_.homePhone } | Set-ADUser -Replace @{extensionAttribute1 = $_.homePhone}

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.
Avatar of oBdA
oBdA

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}}

Open in new window

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 ...
Not unnecessary, since the pipeline variable is referenced.
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.

Open in new window

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

Open in new window

@#~*!!!
Avatar of hellblazeruk

ASKER

Hi,

Thanks for the quick responses

 Get-ADUser -Filter * -Property homePhone | ? { $_.homePhone } | Set-ADUser -Replace @{extensionAttribute4 = $_.homePhone}

Open in new window

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], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.SetADUser

Get-ADUser -Filter "(extensionAttribute1 -like '*') -and -not (homePhone -like '*')" | Set-ADUser -Clear extensionAttribute1

Open in new window

executed as normal but the homePhone number did not copy to extensionAttribute1 ?

Regards,
SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial