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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Display e-mail address 1

Status
Not open for further replies.

cdtech9198

IS-IT--Management
Feb 14, 2008
48
US
Hello I am working on a script to change default smtp address for a group of users in a ou. I can not do it with exchange using recipient policy. Once I get the default email hopefully I can store everything to the left of the @ sign and amend @newdomain.com

Right now I am just trying to query AD to display the mail address but having trouble. Its been a while since I touched vbscript
Code:
Set objShell = WScript.CreateObject("WScript.Shell")
Set objNetwork = CreateObject("Wscript.Network")
Set objFSO = CreateObject("Scripting.FileSystemObject")


OUPath = "LDAP://OU=test,DC=foo,DC=com"

Set CNUsers = GetObject (OUPath)
CNUsers.Filter = Array("user")
For Each User in CNUsers
     address = User.mail  
     wscript.echo address

Next
Thank you.
 
You are using the wrong property. You need to enumerate ProxyAddresses. The default email address uses upper case SMTP, recondary addresses use lower case smtp.

You don't say how you want to change the email so I've just given you an example of changing the default domain.
Code:
Const ADS_PROPERTY_APPEND = 3
Const ADS_PROPERTY_DELETE = 4
Dim qQuery, objConnection, objCommand, objRecordSet, obj
Dim oRootDSE, strDomain

Set oRootDSE = GetObject("LDAP://rootDSE")
strDomain = oRootDSE.get("defaultNamingContext")

strOU = "test"

' other categories = computer, user, printqueue, group
qQuery = "<LDAP://OU=" & strOU & "," & strDomain &">;" & _
		"(objectCategory=person)" & _
       ";distinguishedName;subtree"

Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Open "Provider=ADsDSOObject;"
objCommand.ActiveConnection = objConnection
objCommand.CommandText = qQuery
Set objRecordSet = objCommand.Execute

While Not objRecordSet.EOF
    Set objUser = GetObject("LDAP://" & objRecordSet.Fields("distinguishedName"))
    arrEmails = objUser.ProxyAddresses
    For Each email In arrEmails
    	If Left(email,4)="SMTP" Then
    	'Assume we are changing the default domain for a user just as an example
    		NewMail = Replace(email,"oldDomain.com","newDomain.com")
		    objUser.PutEx ADS_PROPERTY_DELETE, "proxyAddresses", array(email)
		    objUser.SetInfo  
		    objUser.PutEx ADS_PROPERTY_APPEND, "proxyAddresses", array(newMail) 
			objUser.SetInfo
    	End If
    Next
    objRecordSet.MoveNext
Wend

objConnection.Close

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
Yes I figured out the ProxyAddress is where I need to be. I will be adding a new default domain name and setting it as default SMTP. Do not want to delete the old one. Thank you.
 
You will need to delete and then add back in the address as a secondary address. Change the For Each as follows:

Code:
For Each email In arrEmails
        If Left(email,4)="SMTP" Then
        'Assume we are changing the default domain for a user just as an example
            SaveMail = Replace(email,"SMTP","smtp")
            NewMail = Replace(email,"oldDomain.com","newDomain.com")
            objUser.PutEx ADS_PROPERTY_DELETE, "proxyAddresses", array(email)
            objUser.SetInfo  
            objUser.PutEx ADS_PROPERTY_APPEND, "proxyAddresses", array(newMail) 
            objUser.SetInfo
            objUser.PutEx ADS_PROPERTY_APPEND, "proxyAddresses", array(SaveMail) 
            objUser.SetInfo
        End If
    Next

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
Very nice. I was almost there. I was first querying the proxyaddress then striping the @olddomain.com off using Right and Len commands. Next was I adding name@newdomain.com using
objUser.PutEx ADS_PROPERTY_APPEND.

Only thing I noticed is that the mail attribute in ADUC is not changing to reflect the new email address.
 
Code:
For Each email In arrEmails
        If Left(email,4)="SMTP" Then
        'Assume we are changing the default domain for a user just as an example
            SaveMail = Replace(email,"SMTP","smtp")
            NewMail = Replace(email,"oldDomain.com","newDomain.com")
            objUser.PutEx ADS_PROPERTY_DELETE, "proxyAddresses", array(email)
            objUser.SetInfo  
            objUser.PutEx ADS_PROPERTY_APPEND, "proxyAddresses", array(newMail) 
            objUser.SetInfo
            objUser.PutEx ADS_PROPERTY_APPEND, "proxyAddresses", array(SaveMail) 
            objUser.SetInfo[b]
            objuser.Put "mail" , newMail
	    objUser.SetInfo[/b]
        End If
    Next

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top