How Can I Add an Email Address to the proxyAddresses Attribute?

How Can I Add an Email Address to the proxyAddresses Attribute?

  • Comments 4
  • Likes

Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I add an email address to the proxyAddresses attribute without overwriting all the existing attributes?

-- DH

SpacerHey, Scripting Guy! AnswerScript Center

Hey, DH. For those of you who aren’t too familiar with the proxyAddresses attribute (and, alas, that includes the Scripting Guys) proxyAddresses is an attribute added to Active Directory by Microsoft Exchange; it’s simply an alias by which an Exchange user can be recognized by a non-Exchange mail system. If you echo back the proxy addresses for a given user you’re likely to see something similar to this:

NUMERIC:4257050743
sip:kenmyer@fabrikam.com
X500:/o=fabrikam/ou=APPS-WGA/cn=Xenix_Users/cn=kenmyer
X500:/o=fabrikam/ou=First Administrative Group/cn=Recipients/cn=kenmyer
x400:c=US;a=MCI;p=fab;o=northamerica;s=Myer;g=Ken;
X500:/O=fabrikam/OU=northamerica/cn=Recipients/cn=kenmyer
X400:c=US;a=finance;p=fabrikam;o=fabrikam;s=Myer;g=Ken;
x500:/o=fabrikam/ou=northamerica/cn=Recipients/cn=556899
smtp:kenmyer@fabrikam.com
SMTP:kenmyer@finance.fabrikam.com

If you’re thinking, “Wow, that’s s lot of stuff for one little attribute to have,” well, that’s because proxyAddresses happens to be a multi-valued attribute, an attribute that can contain more than one value. And that’s why this particular question is so tricky. Suppose we wanted to add a new proxy address - kenmyer@northamerica.fabrikam.com - to the list of proxyAddresses. We don’t want to delete the existing addresses; we just want to add an additional one.

Hey, no problem; after all, everyone knows how to modify an Active Directory attribute, right? And so you try a script similar to this:

Set objUser = GetObject _
    ("LDAP://atl-dc-1/CN=Ken Myer,OU=Finance,DC=fabrikam,DC=com")
objUser.proxyAddresses = kenmyer@northamerica.fabrikam.com
objUser.SetInfo

And guess what you get back the next time you echo Ken Myer’s proxy addresses:

kenmyer@northamerica.fabrikam.com

Uh-oh: we deleted all the old proxy addresses and replaced them with the new address. Not what we wanted to do at all.

So how can we simply tack an additional value onto the end of a multi-valued attribute? Why, we just need to use the PutEx method:

Const ADS_PROPERTY_APPEND = 3

Set objUser = GetObject _
    ("LDAP://cn=KenMyer,ou=Finance,dc=fabrikam,dc=com") 

objUser.PutEx ADS_PROPERTY_APPEND, "proxyAddresses", _
    Array("kenmyer@northamerica.fabrikam.com ")

PutEx is an ADSI method designed for working with multi-valued attributes; in fact, PutEx offers four different options for working with multi-valued attributes:

Constant

Value

Description

ADS_PROPERTY_CLEAR

1

Clears the value (or values) from the specified attribute.

ADS_PROPERTY_UPDATE

2

Replaces the value in the specified attribute with new values.

ADS_PROPERTY_APPEND

3

Appends a new value (or values) to the specified attribute.

ADS_PROPERTY_DELETE

4

Deletes the value (or values) from the specified attribute.

We won’t spend a lot of time detailing how PutEx works; for more information you might check out the Modifying Multivalued Attributes section in the Microsoft Windows 2000 Scripting Guide.

Now, back to our script. We want to append a new value to proxyAddresses; consequently in the first line of the script we define a constant named ADS_PROPERTY_APPEND and set the value to 3:

Const ADS_PROPERTY_APPEND = 3

We bind to the Ken Myer user account and then call the PutEx method, passing three parameters:

The constant ADS_PROPERTY_APPEND, which tells the script to append the new value to any existing values.

The name of the attribute we want to work with (proxyAddresses).

The value we want to append (in other words, the new proxy address).

Note that even though we’re adding only a single value we need to pass that value as an array; hence our actual code looks like this:

objUser.PutEx ADS_PROPERTY_APPEND, "proxyAddresses", _
    Array("kenmyer@northamerica.fabrikam.com")

And what do we get when we echo the proxy addresses for Ken Myer:

kenmyer@northamerica.fabrikam.com 
NUMERIC:4257050743
sip:kenmyer@fabrikam.com
X500:/o=fabrikam/ou=APPS-WGA/cn=Xenix_Users/cn=kenmyer
X500:/o=fabrikam/ou=First Administrative Group/cn=Recipients/cn=kenmyer
x400:c=US;a=MCI;p=fab;o=northamerica;s=Myer;g=Ken;
X500:/O=fabrikam/OU=northamerica/cn=Recipients/cn=kenmyer
X400:c=US;a=finance;p=fabrikam;o=fabrikam;s=Myer;g=Ken;
x500:/o=fabrikam/ou=northamerica/cn=Recipients/cn=556899
smtp:kenmyer@fabrikam.com
SMTP:kenmyer@finance.fabrikam.com

Cool, huh?

Incidentally, there’s no guarantee that the new proxy address will show up first in the list of addresses; when it comes to multi-valued attributes the order of the individual attributes is not guaranteed. But that doesn’t matter just as long as kenmyer@northamerica.fabrikam.com shows up somewhere in the list.

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • Hey Scripting Guy

    I think something is not right here.

    The Append Method does not work like this, I think  you forgot to add the objUser.SetInfo.

    Also "SMTP:" is missing in the Argument. In addition, this sets the appended SMTP Address as primary Address, if the user object already has a primary address, he'll have 2 now. Is there a way to get around this?

    Tom

  • Tom has a good point.  If your code is run it will not set the address type.  This may not be a major issue as I believe the internal address object is correctly set but the label and the Promary Address can get in trouble.

    If you do use uppercase on SMTP then it will become impossible with ADUC to remove either of the addresses set as PrimaryAddress.  YOu can, however, copy and clear the address list then fix it either in code or manually.

    This is one of those things we all love to hate about AD.  This bit was just not really designed. It just was sort of dropped in.(WS2003 -Is it fixed in 2007/10?)

    The solution is to NOT use uppercase in the address.

    Here is one solution that does not munge the Primary Address.

    userDN= "cn=Some User, ,,,,,dc=dom,dc=com"

    newAddress="sales@somewhere.com"

    AddProxyAddress userDN,newAddress

    Function AddProxyAddress( userDN, newAddress )

        If Not InStr(userDN,"LDAP") Then

             userDN="LDAP://" & userDN

        End If

        Set user=GetObject(userDN)

        ' smtp MUST beinlower case to avoid damage to the proxyaddresses

        newAddress = "smtp:" & newAddress

        user.PutEx 3, "proxyAddresses", Array(newAddress)

        user.SetInfo

    End Function

    Hope this helps someone avoid issues.

  • Hi! Thansk for this script. But, when I put the value to proxyaddresses attribute (no error) on Exchange Management Console appears disable and not valid. It's necessary to change or update any othe attribute?

  • thanks