DJDoug
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@ourcom pany.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.
We are changing the structure of our company's email address to "firstname.lastname@ourcom
We are Using:
Windows Server 2003
Exchange Server 2003
.Net Framework 1.1
Thanks.
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.
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.
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.
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.Director yEntry("LD AP://rootD SE")
Dim DomainDN As String = RootDSE.Properties("Defaul tNamingCon text").Val ue
Dim ADEntry As New DirectoryServices.Director yEntry("LD AP://" & DomainDN)
Dim ADSearch As New System.DirectoryServices.D irectorySe archer(ADE ntry)
Dim ADSearchResult As System.DirectoryServices.S earchResul t
ADSearch.Filter = ("(objectClass=user)")
ADSearch.SearchScope = DirectoryServices.SearchSc ope.Subtre e
Dim results As DirectoryServices.SearchRe sultCollec tion = ADSearch.FindAll()
Dim UserFound As DirectoryServices.SearchRe sult
For Each UserFound In results
If Not IsNothing(UserFound) AndAlso Not Trim(UserFound.GetDirector yEntry().P roperties. Item("mail ").Value) = "" _
AndAlso Not Trim(UserFound.GetDirector yEntry().P roperties. Item("give nName").Va lue) = "" AndAlso Not Trim(UserFound.GetDirector yEntry().P roperties. Item("sn") .Value) = "" Then
Console.WriteLine("Name: " & UserFound.GetDirectoryEntr y().Proper ties.Item( "displayNa me").Value & " ")
Debug.WriteLine("Name: " & UserFound.GetDirectoryEntr y().Proper ties.Item( "displayNa me").Value & " ")
Dim oldEmail As String = UserFound.GetDirectoryEntr y().Proper ties.Item( "mail").Va lue.ToStri ng().ToLow er()
Console.WriteLine("Current Email: " & oldEmail)
Debug.WriteLine("Current Email: " & oldEmail)
Dim firstName As String = UserFound.GetDirectoryEntr y().Proper ties.Item( "givenName ").Value.T oString(). ToLower()
Dim lastName As String = UserFound.GetDirectoryEntr y().Proper ties.Item( "sn").Valu e.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.
Sub Main()
' Add code to log each event.
Dim RootDSE As New DirectoryServices.Director
Dim DomainDN As String = RootDSE.Properties("Defaul
Dim ADEntry As New DirectoryServices.Director
Dim ADSearch As New System.DirectoryServices.D
Dim ADSearchResult As System.DirectoryServices.S
ADSearch.Filter = ("(objectClass=user)")
ADSearch.SearchScope = DirectoryServices.SearchSc
Dim results As DirectoryServices.SearchRe
Dim UserFound As DirectoryServices.SearchRe
For Each UserFound In results
If Not IsNothing(UserFound) AndAlso Not Trim(UserFound.GetDirector
AndAlso Not Trim(UserFound.GetDirector
Console.WriteLine("Name: " & UserFound.GetDirectoryEntr
Debug.WriteLine("Name: " & UserFound.GetDirectoryEntr
Dim oldEmail As String = UserFound.GetDirectoryEntr
Console.WriteLine("Current
Debug.WriteLine("Current Email: " & oldEmail)
Dim firstName As String = UserFound.GetDirectoryEntr
Dim lastName As String = UserFound.GetDirectoryEntr
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
mm..forgot something
If oldMail.Length > 0 Then
values.Remove(oldMail)
values.Add("smtp:" + newEmail)
deUser.Properties("mail"). Clear()
deUser.Properties("mail"). AddRange(v alues.ToAr ray())
deUser.CommitChanges()
End If
If oldMail.Length > 0 Then
values.Remove(oldMail)
values.Add("smtp:" + newEmail)
deUser.Properties("mail").
deUser.Properties("mail").
deUser.CommitChanges()
End If
ASKER
Will this keep the old mail as an alias?
mm..nope sorry, forgot your requirement
comment out this line
values.Remove(oldMail)
comment out this line
values.Remove(oldMail)
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
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.
Dim ADGroups As Object
Dim ADGroup As Object
Dim ADEntry As New DirectoryEntry("WinNT://do
ADGroups = ADEntry.Invoke("Groups")
For Each ADGroup In ADGroups
Console.WriteLine(ADGroup.
Next
then you can split the name to get the first name