Scripting help - need to get phone number of AD accounts and then add them to contacts in trusted domain

I have domain accounts that I have created as contacts in another trusted domain so that they can see them in their Exchange GAL.

I need a way to extract the phone number field from UserA (user account) in DomainA and import it into UserA (contact) in DomainB.

I get the logic, it's just the code (vbscript/powershell/whatever) that eludes me.

The logic as I see it:

  1. Connect to source AD (ou/subtree)
  2. Extract user accounts from OU and subcontainers including first name, last name, display name, and phone number
  3. Connect to target AD (ou/subtree)
  4. Verify/match contact with extract in #2 above based on display name
  5. Update phone field with phone number in extract
  6. Write log of success and failures

Anybody able to help?


Solution 1:

The problem is how do you match the account on one side with the account on the other. This will do the extract.

Any ideas on how to do the match on the import? Do you have a common field like employeeID in both? Otherwise a complicated multi-field matching might be needed.

   get-aduser -filter * -searchbase "ou=users,dc=adomain,dc=com" -properties Telephonenumber|select displayname, givenname, sn, telephonenumber|export-csv phones.csv

import-csv phones.csv| foreach { 
  get-aduser -displayname $_.displayname -searchbase "ou=contacts dc=Bdomain,dc=com"  | set-aduser -telephoneNumber $_.telephonenumber
}

Solution 2:

As long as all of your DisplayNames in Domain A truly do match your DisplayNames in Domain B, the following should do the trick.

In Domain A, run:

Get-ADUser -filter * -properties TelephoneNumber,DisplayName | select DisplayName,TelephoneNumber | export-csv users.csv

In Domain B, run:

Import-CSV users.csv | ForEach { get-aduser -filter "displayname -eq '$($_.displayname)'" | set-aduser -replace @{telephonenumber="$($_.TelephoneNumber)"} }