Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


Replace the Primary mail address in AD

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,48670
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Replace the Primary mail address in AD
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1]
Login
Message << Older Topic   Newer Topic >>
 Replace the Primary mail address in AD - 6/24/2007 5:25:03 AM   
  stubar

 

Posts: 53
Score: 0
Joined: 7/14/2005
Status: offline
Hello

Having a bit of trouble here.
What I want to do is replace the Primary mail address, the objUser.mail, with a second address. Currently I have tried many permutations around the script below. The core of it comes from MS which was to put a secondary, or proxy, address in. This works nicely no complaints. But then I want to take that secondary address and make it a primary whilst relegating the previous address to secondary. (Make sense?).
However, I can successfully get half of it to work but it's not correct. The new primary shows up in the general tab as the mail address and in the GAL but in the E-Mail Addresses tab the old primary is still there as the primary.

As I say I have tried many permutations and get a variety of results but not the one I want.
I have scoured Google and various forums using a different seach keywords but I'm not having a lot of luck.

Please help  

'  Set up the connections
'----------------------------------------------------------------
Const ADS_PROPERTY_UPDATE = 2
Const ADS_PROPERTY_APPEND = 3

dim mail, newmail
newmail = ("first.surname.cllr@domain.org.uk")
Set objUser = GetObject _
   ("LDAP://serv/cn=first surname,ou=Test,ou=Service,dc=domain,dc=org,dc=uk")
' Capture the elements
'----------------------------------------------------------------
currmail = objUser.mail
wscript.echo("This is the current mail address " & currmail)

' *** New proxy email address
        objUser.PutEx ADS_PROPERTY_APPEND, "proxyAddresses", _
             Array("smtp:" & newmail)
                 wscript.echo("This is the new proxy mail address " & newmail)
           objUser.SetInfo

' *** New primary email address
        objUser.PutEx ADS_PROPERTY_UPDATE, "mail", _
             Array("SMTP:" & newmail)
                 wscript.echo("This is the new Primary mail address " & newmail)
         objUser.SetInfo

' *** New proxy email address
        objUser.PutEx ADS_PROPERTY_APPEND, "proxyAddresses", _
            Array("smtp:" & currmail)
               wscript.echo("This is the new proxy mail address " & currmail)
         objUser.SetInfo

' *** Turn off recipient policy updates
         objUser.Put "msexchpoliciesexcluded", "{26491CFC-9E50-4857-861B-0CB8DF22B5D7}"
     objUser.SetInfo

wscript.echo("Complete")
 
 
Post #: 1
 
 RE: Replace the Primary mail address in AD - 6/24/2007 2:43:21 PM   
  DiGiTAL.SkReAM


Posts: 1194
Score: 7
Joined: 9/6/2005
From: Florida, USA
Status: offline
Not sure why you care about the 'mail' field, but all you have to do to set someone's "Primary" email address (the one that is used when they send an email) is to modify the proxyAddresses field.  Whichever one of those addresses starts with SMTP instead of smtp is the Primary.  Secondary addresses all start with a lower-case smtp, but primaries start with upper-case SMTP.

Other than that, the mail field is just for display purposes.

_____________________________

"Would you like to touch my monkey?" - Dieter (Mike Meyers)

"It is better to die like a tiger, than to live like a pussy."
-Master Wong, from Balls of Fury

(in reply to stubar)
 
 
Post #: 2
 
 RE: Replace the Primary mail address in AD - 6/24/2007 7:53:29 PM   
  stubar

 

Posts: 53
Score: 0
Joined: 7/14/2005
Status: offline
If I do as advised and run this with the new address and 'SMTP:' before the variable it does go into Proxaddresses but doesn't over-ride the previous primary. So I have the general tab as the old primary still and the E-Mail addresses tab displaying the new address as a second primary(!!) AdsiEdit still shows the MAIL attribute with the old address in it and the 2 primary addresses. Both bold with uppercase SMTP preceeding the address.
Any thoughts please would be appreciated.

Thanks 

(in reply to DiGiTAL.SkReAM)
 
 
Post #: 3
 
 RE: Replace the Primary mail address in AD - 6/24/2007 8:40:08 PM   
  4scriptmoni


Posts: 208
Score: 0
Joined: 5/3/2007
Status: offline
maybe if you first set both email address to "" and then first set SMTP then smtp...

_____________________________

Enterprise Microsoft Scripts
Exchange, Login/Logout Monitor,TS, Monitoring, Security, AD, etc...
http://www.felipeferreira.net

(in reply to stubar)
 
 
Post #: 4
 
 RE: Replace the Primary mail address in AD - 6/24/2007 11:51:32 PM   
  stubar

 

Posts: 53
Score: 0
Joined: 7/14/2005
Status: offline
Sorry, Nope :o(
Created a variable caleed blankmail = ("")
Error is "The attribute syntax specified to the directory service is invalid".

Tried it with (" ") and although the script ran it still did not do as I need it to.

Thanks for trying though.

(in reply to 4scriptmoni)
 
 
Post #: 5
 
 RE: Replace the Primary mail address in AD - 6/25/2007 12:57:33 AM   
  DiGiTAL.SkReAM


Posts: 1194
Score: 7
Joined: 9/6/2005
From: Florida, USA
Status: offline
you are *appending* the proxyaddresses field with the new data.
If you instead, replace teh data witht he new data, you'd be fine.
I have to do this once in a while with my clients' addresses, for example, when they want to change their email addresses, or something.
if you use the below format, you'd be ok.
.PutEx 4, "ProxyAddresses", Array

_____________________________

"Would you like to touch my monkey?" - Dieter (Mike Meyers)

"It is better to die like a tiger, than to live like a pussy."
-Master Wong, from Balls of Fury

(in reply to stubar)
 
 
Post #: 6
 
 RE: Replace the Primary mail address in AD - 6/25/2007 3:09:49 AM   
  stubar

 

Posts: 53
Score: 0
Joined: 7/14/2005
Status: offline
Thanks 'Digital'
Do you mean:
       objUser.PutEx ADS_PROPERTY_DELETE, "ProxyAddresses", Array ("SMTP:" & newmail) 
        objUser.SetInfo
                               ?

Because I'm afraid it's still not doing what I need it to.
I get a variety of the general tab showing the correct address but the email addresses tab showing the old primary address or vice versa.

It's a pain.

I did get this script and it's very close:




'  Set up the connections
'----------------------------------------------------------------
Const ADS_PROPERTY_UPDATE = 2
Const ADS_PROPERTY_APPEND = 3
dim mail, newmail
newmail = ("stuart test")
Set objUser = GetObject _
  ("LDAP://domain")
' Capture the elements
'----------------------------------------------------------------
currmail = objUser.mail
wscript.echo("This is the current mail address " & currmail)
arrProxyAddresses = objUser.Get("proxyAddresses")
' Get rid of the current Primary
For i = 0 To UBound(arrProxyAddresses)
     If InStr(arrProxyAddresses(i), "SMTP:") > 0 Then
           arrProxyAddresses(i) = Replace(arrProxyAddresses(i), "SMTP:", "smtp:")
     End If
Next
' Add the new Primary
ReDim Preserve arrProxyAddresses(UBound(arrProxyAddresses) + 1)
arrProxyAddresses(UBound(arrProxyAddresses)) = "SMTP:" & newmail
' Apply the Changes. Mail field will update automatically.
objUser.Put "proxyAddresses", arrProxyAddresses
objUser.SetInfo
'
' *** Turn off recipient policy updates
        objUser.Put "msexchpoliciesexcluded", "{26491CFC-9E50-4857-861B-0CB8DF22B5D7}"
    objUser.SetInfo
wscript.echo("Complete")




But where it sets the new address as the primary it does not replicate to the general tab. I've tried it on a test account and will leave it till tomorrow to allow for replication.

Thanks for all the help though.

Stuart

(in reply to DiGiTAL.SkReAM)
 
 
Post #: 7
 
 RE: Replace the Primary mail address in AD - 6/25/2007 6:56:42 AM   
  DiGiTAL.SkReAM


Posts: 1194
Score: 7
Joined: 9/6/2005
From: Florida, USA
Status: offline
Think of it this way:
the .mail and .proxyAddresses are two different fields.
.mail is a string field
.proxyAddresses is an array field

Look at this:
a1 = array(1,2,3)

a1 = array(4,5,6)

in the second line, I overwrote what I had in the array from the first line.  This is what you need to do with the proxyAddresses field.  OVERWRITE anything that you already have in it with the new data.
For the .mail field, put the SMTp address in there.  Am rushed for time right now, but will hack out a script that will do what I am talking about for you this afternoon.

_____________________________

"Would you like to touch my monkey?" - Dieter (Mike Meyers)

"It is better to die like a tiger, than to live like a pussy."
-Master Wong, from Balls of Fury

(in reply to stubar)
 
 
Post #: 8
 
 RE: Replace the Primary mail address in AD - 6/25/2007 12:38:30 PM   
  DiGiTAL.SkReAM


Posts: 1194
Score: 7
Joined: 9/6/2005
From: Florida, USA
Status: offline
Ok, here's the code.  It's pretty simple and straightforward. 

      
it even has comments to help you understand it.

_____________________________

"Would you like to touch my monkey?" - Dieter (Mike Meyers)

"It is better to die like a tiger, than to live like a pussy."
-Master Wong, from Balls of Fury

(in reply to DiGiTAL.SkReAM)
 
 
Post #: 9
 
 RE: Replace the Primary mail address in AD - 6/25/2007 7:32:05 PM   
  stubar

 

Posts: 53
Score: 0
Joined: 7/14/2005
Status: offline
Thanks for your time on this Digital, much appreciated.
Managed to get what I need.
Stuart

(in reply to DiGiTAL.SkReAM)
 
 
Post #: 10
 
 
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Replace the Primary mail address in AD Page: [1]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts