Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Script to change contacts email addresses in AD

Status
Not open for further replies.

scottd29

Technical User
May 3, 2007
5
0
0
US
Let me first start with the caveat that I am in no way a scripting guru. I know enough to be dangerous. Anyway, I received an email today informing me that all contacts with a certain domain email address, let's say it's bobby@domain.user.com, are changing to bobby@newdomain.user.com. They are all contacts in the same OU. I wanted to know if anyone knew of a script to change the email domain in their general properties as well as their SMTP addresses. I've been searching around for about 3 hours and just can't seem to find anything even close for my needs. I'd hate to have to hand jam around 500 contacts. Any help would be greatly appreciated.
 
bind to the OU
loop through all the contacts in the OU
bind to each contact
change contact information, .SetInfo
 
This script is to change email addresses of user accounts, perhaps you can alter it for contacts:

On Error Resume Next

Const ADS_SCOPE_SUBTREE = 2

Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection

objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE

strUserF = "(&(objectClass=user)(mail=*))"
strUserA = "distinguishedName,sAMAccountName"
strUserQ = "<LDAP://DC=mydomain,DC==com>;" & strUserF & ";" & strUserA & ";subtree"

objCommand.CommandText = strUserQ
Set objRecordSet = objCommand.Execute

If ( objRecordSet.RecordCount <> 0 ) Then
objRecordSet.MoveFirst
Do Until objRecordSet.EOF
strUserDN = objRecordSet.Fields("distinguishedName").Value
Set objUser = GetObject("LDAP://" & strUserDN)
strMail = objUser.mail
If ( InStr(strMail, "@olddomain.com") <> 0 ) Then
Set objRecip = objUser
sAddress = "SMTP:" & Replace(objUser.mail, "olddomain.com", "newdomain.co.za")
vProxyAddresses = objRecip.ProxyAddresses
nProxyAddresses = UBound(vProxyAddresses)
i = 0
Do While i <= nProxyAddresses
email = vProxyAddresses(i)
If ( InStr(email, "olddomain.com") > 0 ) Then
vProxyAddresses(i) = ""
End If
If ( InStr(email, "newdomain.co.za") > 0 ) Then
vProxyAddresses(i) = "SMTP:" & Mid(email, 6)
Exit Do
End If
i = i + 1
Loop
objRecip.ProxyAddresses = vProxyAddresses
objUser.Put "mail", Right(sAddress, Len(sAddress) - 5)
objUser.SetInfo
End If
objRecordSet.MoveNext
Loop
Else
Set objRecordSet = Nothing
Set objConnection = Nothing
WScript.Echo "No users found with the email address @olddomain.com"
WScript.Quit
End If

Set objRecordSet = Nothing
Set objConnection = Nothing
WScript.Echo "Done"
WScript.Quit
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top