How to add new mail address to AD

One of companies I take care of was bought by another company... I needed to add new primary mail (from name.surname@old_domain.cz to name.surname@new_domain.cz)

So I decided to use script for this. Here is the result:

Set myOU = GetObject("LDAP://OU=Uzivatele, DC=Old_Domain, DC=cz")

For each myUser in myOU
       Set objRecipient = myUser
       bIsFound = False
       vProxyAddresses = objRecipient.ProxyAddresses
       nProxyAddresses = UBound(vProxyAddresses)
       i=0
       Do While i <= nProxyAddresses
              email = vProxyAddresses(i)
              If InStr(mid(email,6),"@New_Domain.cz") Then 
                 bIsFound = True
                 Exit Do
              End If
              If Left (email, 5) = "SMTP:" Then
                  vProxyAddresses(i) = "smtp:" & Mid(email,6)
                  MailIndex =  InStr(mid(email,6),"@")
                  strNewProxyAddresses = "SMTP:" & Mid(email, 6, MailIndex - 1) & "@New_Domain.cz"
                 Wscript.Echo strNewProxyAddresses
             End If
             If vProxyAddresses(i) = strNewProxyAddresses Then
                          bIsFound = True
                          Exit Do
             End If
             i = i + 1
       Loop
       If Not bIsFound Then
           ReDim Preserve vProxyAddresses(nProxyAddresses + 1)
           vProxyAddresses(nProxyAddresses + 1) = strNewproxyAddresses
           objRecipient.ProxyAddresses = vProxyAddresses
           myUser.SetInfo
       End If
Next

Heh, and it is working... It will take all users from specified OU, and if they dont have @new_domain.cz mail, it will take their actual primary mail, take first part (name.surname) and same new mail address in format first_part_of_actual_primary_mail@new_domain.com. And of course it will change current primary mail as normal :)

P.S.: Sorry for formating, looks like this blog system does not support [CODE][/CODE] :(

Published Mon, Sep 26 2005 13:39 by martin
Filed under:

Comments

# re: How to add new mail address to AD

I just want to make sure I'm not missing something, but doesnt Exchange 2k/2k3 use the RUS to accomplish the very same thing?

Thanks,
Tim

Monday, September 26, 2005 3:19 PM by martin

# re: How to add new mail address to AD

Maybe, I dont know :) I am not (and never was) exchange administrator, I just needed to solve this problem, because exchange administrator is not available...

Martin

Monday, September 26, 2005 3:24 PM by martin

# re: How to add new mail address to AD

Thx Tim for informations, I read about RUS, however I dont see how that could help me?

I needed to create @new_domain.cz mail address based on primary mail, that is why I need to find primary mail for every user, split it around "@" and use the first part of this split with @New_Domain.cz...

Monday, September 26, 2005 4:24 PM by martin

# re: How to add new mail address to AD

Hi Martin,
Assuming all your users already used a standard address scheme, your current RUS probobly looked something like @old_domain.cz, which would have made the stmp address firstname.lastname@old_domain.cz.

To add another email address suffix, just update the existing RUS, and add @new_domain.cz, and set it as the primary. The RUS will then go around and update everyone's AD account, and add the new email address.

You can further tweak the formatting RUS does by adding things like %g1.%s@new_domain.cz. This will take the first letter of the first name.lastname@new_domain.cz. Thats from memory, so it may be a bit off, but you can get pretty flexible with a domain based standard emailing convention.

Maybe I'm just wrong, but that seemed like the same direction you were going with your script.

Tim (tim AT nativemode.com)

Monday, September 26, 2005 5:02 PM by martin

# re: How to add new mail address to AD

Didnt know about this feature (as I said, I am not exchange admin)...

Thanks again for sharing this information, maybe I will use it next time :)

Tuesday, September 27, 2005 12:58 PM by martin

# re: How to add new mail address to AD

Just something to be wary of:

'----------------- code start --------------
Set myOU = GetObject("LDAP://OU=Uzivatele, DC=Old_Domain, DC=cz")
For each myUser in myOU
.
.
Next
'----------------- code start --------------

The For ... Each code works very well, but it returns a collection of objects within the OU (in the case of a user object, it will contain things like profilepath, displayName, homeDirectory etc). I have had situations where I have blown the LDAP search response buffer (which typically happens when a sysadmin dumps too many users in an OU).

A way around this is to extract the DNs of the objects within the OU, and to retrieve each object within the OU individually.

If people are interested I can post some sample code.

Wednesday, September 28, 2005 2:28 AM by martin

# re: How to add new mail address to AD

I just tried it in company, where administrators are , ehm, dont know how to say it politely :)

However there is one OU for whole company (cca 3500 employees) and you are right - problem with buffer appeared :(

Also I am thinking about one thing - myUser is object, however based on ADSI, it is empty instance in memory till you use GetInfo or access properties. If you use GetInfoEx method, you should be able to retrieve just informations you needed...

If you send me the code to soulinprague(at)gmail(dot)com, I will post it.

Martin

Wednesday, September 28, 2005 9:31 AM by martin