Link to home
Start Free TrialLog in
Avatar of DJDoug
DJDougFlag for United States of America

asked on

Read AD Users and Create String to Update Email Address

Hi all,

We are changing the structure of our company's email address to "firstname.lastname@ourcompany.com".  Does anyone know of code (VB.Net) that would read (loop) through our Users in Active Directory, create the new email string from First Name and Last Name in AD, then update their primary email address in Exchange Server 2003 to the new address?  Also, the old email address would need to be maintained as an alias.  We have over 1500 users worldwide, so to do this manually would be very time-consuming.  Any help is greatly appreciated.

We are Using:
Windows Server 2003
Exchange Server 2003
.Net Framework 1.1

Thanks.
Avatar of Mikal613
Mikal613
Flag of United States of America image

You can use the System.DirectoryServices namespace to do this:

Dim ADGroups As Object
Dim ADGroup As Object
Dim ADEntry As New DirectoryEntry("WinNT://domainname/username")

ADGroups = ADEntry.Invoke("Groups")
For Each ADGroup In ADGroups
Console.WriteLine(ADGroup.Name)
Next


then you can split the name to get the first name
Avatar of DJDoug

ASKER

"Name" returns the full name.  When a tool like Dameware reads AD, it pulls First Name and Last Name, as well as the full name.  What are the fields for first name and last name? (Or, is it that obvious? lol).  Also, once I get that, how do I update the primary email address on Exchange.  Thanks.
Avatar of ihenry
ihenry

Hello DJDoug,

AD attribute for first name is Given-Name and Surname for last name.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/adschema/adschema/a_givenname.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/adschema/adschema/a_sn.asp

To get full name, that depends on how you create the user object. When you do that programmatically, it also depends on the type of ldap provider used to bind to AD.

To update primary email address in MS Exchange, you can make query to AD and for each user get one attribute named mail. However, mail is actually a proxyaddresses which stores multi-value of email addresses such as SMTP, x.400, etc. So your code needs to iterate the string array and find one that starts with "smtp" will be the primary address.
Avatar of DJDoug

ASKER

Thanks iHenry, that gets me pretty far along.  I have the following code so far which sets everything up:

        Sub Main()
            ' Add code to log each event.
            Dim RootDSE As New DirectoryServices.DirectoryEntry("LDAP://rootDSE")
            Dim DomainDN As String = RootDSE.Properties("DefaultNamingContext").Value
            Dim ADEntry As New DirectoryServices.DirectoryEntry("LDAP://" & DomainDN)
            Dim ADSearch As New System.DirectoryServices.DirectorySearcher(ADEntry)

            Dim ADSearchResult As System.DirectoryServices.SearchResult
            ADSearch.Filter = ("(objectClass=user)")
            ADSearch.SearchScope = DirectoryServices.SearchScope.Subtree
            Dim results As DirectoryServices.SearchResultCollection = ADSearch.FindAll()
            Dim UserFound As DirectoryServices.SearchResult
            For Each UserFound In results
                If Not IsNothing(UserFound) AndAlso Not Trim(UserFound.GetDirectoryEntry().Properties.Item("mail").Value) = "" _
                        AndAlso Not Trim(UserFound.GetDirectoryEntry().Properties.Item("givenName").Value) = "" AndAlso Not Trim(UserFound.GetDirectoryEntry().Properties.Item("sn").Value) = "" Then
                    Console.WriteLine("Name: " & UserFound.GetDirectoryEntry().Properties.Item("displayName").Value & " ")
                    Debug.WriteLine("Name: " & UserFound.GetDirectoryEntry().Properties.Item("displayName").Value & " ")

                    Dim oldEmail As String = UserFound.GetDirectoryEntry().Properties.Item("mail").Value.ToString().ToLower()
                    Console.WriteLine("Current Email: " & oldEmail)
                    Debug.WriteLine("Current Email: " & oldEmail)

                    Dim firstName As String = UserFound.GetDirectoryEntry().Properties.Item("givenName").Value.ToString().ToLower()
                    Dim lastName As String = UserFound.GetDirectoryEntry().Properties.Item("sn").Value.ToString().ToLower()
                    Dim newEmail As String = firstName & "." & lastName & "@mycompany.com"
                    Console.WriteLine("New Email: " & newEmail)
                    Debug.WriteLine("New Email: " & newEmail)

                    If oldEmail <> newEmail Then
                        ' Code here to update Exchange Server.
                        ' Need help with this part.
                    End If

                    Console.WriteLine(vbCrLf)
                    Debug.WriteLine(vbCrLf)
                End If
            Next
        End Sub

Can you help with the Exhange updating code, or point me to an article that can explain?  Also, I need to make sure that once I update the smtp with new email address, that the old email address is added as an alias.  Where do I add the alias?  Also, additional 250 points is added for this question (750 total).  Thanks.
ASKER CERTIFIED SOLUTION
Avatar of ihenry
ihenry

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
mm..forgot something

If oldMail.Length > 0 Then
    values.Remove(oldMail)
    values.Add("smtp:" + newEmail)
    deUser.Properties("mail").Clear()
    deUser.Properties("mail").AddRange(values.ToArray())
    deUser.CommitChanges()
End If


Avatar of DJDoug

ASKER

Will this keep the old mail as an alias?
mm..nope sorry, forgot your requirement

comment out this line
values.Remove(oldMail)
Avatar of DJDoug

ASKER

Thanks ihenry!  That works perfect.  Saved many, many hours of manual updates.  Really appreciate that.  ...I had posted another 250 points earlier for this question.  Please post a generic comment in that question and I will give you those points as well.  Thanks again.  Here's the other question:

https://www.experts-exchange.com/questions/21201627/Need-Directory-Services-Help-with-AD-Exchange-Server-2003-750-Total-Points.html

DJ Doug
You're welcome DJ Doug, glad could help.