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

Posted on 2005-04-06
Last Modified: 2010-04-13
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.
Question by:YankeeFan03
    LVL 16

    Expert Comment

    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
    LVL 16

    Expert Comment

    ' 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 !!!

    Author Comment

    will do thanks.  do i need to run this on the server?
    LVL 16

    Accepted Solution

    no, you can run it from your workstation.
    The more important part is the user who runs it.


    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    Server Hard Drive Expansion 2 131
    Windows 2000 and 2008 Servers Network Stopping 7 911
    P2V Windows NT/2000 SP4 3 1,805
    Install Window 2012 Domain on 9 124
    NTFS file system has been developed by Microsoft that is widely used by Windows NT operating system and its advanced versions. It is the mostly used over FAT file system as it provides superior features like reliability, security, storage, efficienc…
    Outlook Free & Paid Tools
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.

    754 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