Solved

Need to Copy Data from One-to-Many Active Directory Objects

Posted on 2009-04-10
3
214 Views
Last Modified: 2013-12-24
For reasons that are too complicated to explain, I have a need to automate a process which will copy the telephoneNumber attribute value from Contact objects in one Active Directory OU ("Telephone Directory") and write that value into the telephoneNumber attribute on User objects in multiple other OU's - all in the same Active Directory domain.  The telephoneNumber attribute from a single Telephone Directory Contact might have a one-to-one OR a one-to-many correlation with User objects elsewhere in the Active Directory.  For example, there is only one "Housekeeping" Contact in the Telephone Directory OU, but there are 20 user accounts in the "Housekeeping" OU that all need the same telephoneNumber value from the Housekeeping contact.  I can use a spare attritbute field in the receiving user records to record the CN of the corresponding Telephone Directory contact, so we can use that to match up the records, but I have no idea how to write the script that would perform this operation.  Can anyone help??
one-to-one-one-to-many.jpg
0
Comment
Question by:isdirect
  • 2
3 Comments
 
LVL 70

Expert Comment

by:Chris Dent
ID: 24148428

So essentially...

If a Contact exists with the same name as a User, then copy the Phone number from there. Otherwise if a Contact exists named after the Users OU, copy that phone number?

Chris
0
 

Author Comment

by:isdirect
ID: 24148507
Yes - that's the idea.  Thanks for any help you can provide!
0
 
LVL 70

Accepted Solution

by:
Chris Dent earned 500 total points
ID: 24156167

Sounds reasonable enough :)

This bit of VbScript will need saving as a ".vbs" file. You must update the path for the TELEPHONE_DIRECTORY constant so it can find the contacts.

Otherwise, if it finds a user with the name of a contact it will add the phone number. If it can't find the user by name, it will check the OU they're in and update the phone number based on that if it exists.

HTH

Chris
Option Explicit
 

' Search for Contacts in the Telephone Directory and get the telephone number
 

Const TELEPHONE_DIRECTORY = "OU=Telephone Directory,DC=yourdomain,DC=com"
 

Dim objTelephoneDirectory : Set objTelephoneDirectory = CreateObject("Scripting.Dictionary")
 

Dim objConnection : Set objConnection = CreateObject("ADODB.Connection")

objConnection.Provider = "ADsDSOObject"

objConnection.Open "Active Directory Provider"
 

Dim objCommand : Set objCommand = CreateObject("ADODB.Command")

Set objCommand.ActiveConnection = objConnection

objCommand.Properties("Page Size") = 1000
 

objCommand.CommandText = "<LDAP://" & TELEPHONE_DIRECTORY & ">;" & _

  "(&(objectClass=contact)(telephoneNumber=*));distinguishedName,name,telephoneNumber;subtree"
 

Dim objRecordSet : Set objRecordSet = objCommand.Execute
 

Do While Not objRecordSet.EOF

  If Not objTelephoneDirectory.Exists(objRecordSet.Fields("name").Value) Then
 

    objTelephoneDirectory.Add objRecordSet.Fields("name").Value, objRecordSet.Fields("telephoneNumber").Value
 

  End If

  objRecordSet.MoveNext  

Loop
 

Set objRecordSet = Nothing
 

' Search for all users in AD
 

Dim objRootDSE : Set objRootDSE = GetObject("LDAP://RootDSE")

objCommand.CommandText = "<LDAP://" & objRootDSE.Get("defaultNamingContext") & ">;" & _

  "(&(objectClass=user)(objectCategory=person));distinguishedName,name;subtree"

Set objRootDSE = Nothing
 

Set objRecordSet = objCommand.Execute
 

Do While Not objRecordSet.EOF
 

  Dim strName : strName = objRecordSet.Fields("name").Value

  Dim strDN : strDN = objRecordSet.Fields("distinguishedName").Value

  Dim strParent : strParent = Left(Split(strDN, "=")(2), InStrRev(Split(strDN, "=")(2), ",") - 1)
 

  ' Check if the user has an entry for their name in the directory
 

  Dim objUser

  If objTelephoneDirectory.Exists(strName) Then
 

    ' Set the telephone Number

    Set objUser = GetObject("LDAP://" & strDN)

    objUser.Put "telephoneNumber", objTelephoneDirectory(strName)

    objUser.SetInfo

    Set objUser = Nothing
 

  ' Otherwise Check if the user has an entry for their department in the directory
 

  ElseIf objTelephoneDirectory.Exists(strParent) Then
 

    ' Set the telephone Number

    Set objUser = GetObject("LDAP://" & strDN)

    objUser.Put "telephoneNumber", objTelephoneDirectory(strParent)

    objUser.SetInfo

    Set objUser = Nothing
 

  End If
 

  objRecordSet.MoveNext

Loop

Open in new window

0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now