i want to add a country code to our phone numbers in active directory

MY boss wants me to add the country area code before the numbers in our active directory.  Is there an easy way to do this besides going into each user and updating it manually?


I am running active directory on windows 2000 and have each country in it's own OU.
Who is Participating?
mdiglioConnect With a Mentor Commented:
no, you can run it from your workstation.
The more important part is the user who runs it.

Here is some code that should work for you.
If you have questions just ask and I can provide more details as to what it is doing.

Before running this you must change a few lines.
The lines you need to change are all commented by using '!!!!!!

Error control is turned off

Copy and paste the code into notepad and save it with a .vbs extension.
( If you have VB6 it will go into a project by adding the Microsoft ActiveX Data Objects 2.7 library reference)

run it as a user that has the appropriate rights to make these updates

You will have to make 2 changes to the script for every ou you have.
This script will do one ou ( country ) at a time,  and any sub OUs it might have
' Begin Copy !!!

'On Error Resume Next

Set cnn = CreateObject("ADODB.Connection")
Set cmd = CreateObject("ADODB.Command")
cnn.Provider = "ADsDSOObject"  
cnn.Open "Active Directory Provider"
Set cmd.ActiveConnection = cnn
cmd.Properties("Page Size") = 1000
cmd.Properties("Timeout") = 30

'!!!! Change this line depending upon how far down the starting OU you
'!!!! would like to enumerate....ADS_SCOPE_ONELEVEL or ADS_SCOPE_SUBTREE... **see Below

cmd.Properties("Searchscope") = ADS_SCOPE_SUBTREE  
cmd.Properties("Cache Results") = True

'this will enumerate all users and grab their DN and current phone number
'!!!!you will have to change the 2nd line to match your OU Design!!!!!
    cmd.CommandText = "select distinguishedname, telephonenumber from 'LDAP://" & _
    " ou=THeCountry,ou=TOPLevelOU,dc=yourdomain,dc=com' " & _
    "WHERE objectCategory='user'"

Set rs = cmd.Execute
Do While Not rs.EOF

    strDN = rs.Fields("distinguishedname")
    strPhone = rs.Fields("telephonenumber")
'!!!!!! change this line for each country
    strPhone = "Insert Country Code Here" & strPhone

    ' example: strPhone = "0123-" & strPhone

    strLDAP = "LDAP:// " & strDN & ""
    Set x = GetObject(strLDAP)
    x.Put "telephonenumber", strPhone


msgbox "Done"

' **using the example above of the fake domain with the 2 OUs...leaving the ADS_SCOPE_SUBTREE will enumerate all ous beneath
' **the ou I called 'THeCountry'. changing it to ADS_SCOPE_ONELEVEL will only enumerate the users in that ou
' From your question it sounds like you do can jsut leave it as ADS_SCOPE_SUBTREE

' End copy !!!
YankeeFan03Author Commented:
will do thanks.  do i need to run this on the server?
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.