Archive

Posts Tagged ‘proxyAddresses’

Change primary proxyAddresses

November 12th, 2008 2 comments

This script creates and sets a new Primary E-mail Address based on:

  • Organizational Unit;
  • UPN Suffix.

The way the new  Primary E-mail Address is build depends on the way your sAMAccountName is build. This is something you can adjust the way you like. In the script below it is set to:

“1st letter givenName.something in between.sn@email domain”

For example:

m.vander.plas@martius.nl

Variables you can adjust are:

  • LDAP Connection;
  • EmailDomain;
  • UPNSuffix;
  • PriEmailAddress (the way it is set-up).

And then now the script you came for!

On Error Resume Next

Dim objRecip

Const ADS_PROPERTY_UPDATE = 2

‘ LDAP connection to specific OU
Set CNUsers = GetObject (“
LDAP://OU=Personel,OU=Accounts,DC=testdomain,DC=local“)
CNUsers.Filter = Array(“user”)
For Each User in CNUsers

‘ #### Edit below here ####
EmailDomain = “domain.com”  ’ Put your mail domain here
UPNSuffix = “testdomain.local”  ’ Put the UNP suffix to which this script applies to here
PriEmailAddress = LCase(LEFT(givenName,1)) &LCase(RIGHT(User.sAMAccountName, Len(User.sAMAccountName) – InStr(User.sAMAccountName, “.”)+1)) &”@” &EmailDomain
‘ #### No editing below here ####
givenName = User.givenName
sn = User.sn
sAMAccountName = User.sAMAccountName
userPrincipalName = User.userPrincipalName
SamUPN = User.sAMAccountName &”@” &UPNSuffix
sAddress = “SMTP:”&PriEmailAddress
Set objRecip = User

‘ ********************************
‘  For test purposes only
‘ MsgBox (“PriEmailAddress = “&PriEmailAddress)
‘ ********************************

If Not userPrincipalName = SamUPN Then

‘ Do Nothing

Else

‘ Disable”Automatically update e-mail addresses based on recipient policy”
User.PutEx ADS_PROPERTY_UPDATE, “msExchPoliciesExcluded”, Array(“{26491CFC-9E50-4857-861B-0CB8DF22B5D7}”)
User.SetInfo

‘ Remove already present email address which is the same that is created later on. This is a dirty check, but it works :-)
User.PutEx 4, “ProxyAddresses”, Array(“smtp:”&PriEmailAddress)
User.SetInfo

‘ ##### Begin creation new primary E-mail Address #####
bIsFound = False

vProxyAddresses = objRecip.ProxyAddresses
nProxyAddresses = UBound(vProxyAddresses)

i = 0

Do While i <= nProxyAddresses
email = vProxyAddresses(i)
If Left (email,5) = “SMTP:” Then
vProxyAddresses (i) = “smtp:” & Mid (email,6)
End If

If vProxyAddresses(i) = sAddress  Then
IsFound = True
Exit Do
End If

i = i + 1

Loop

If Not bIsFound Then
ReDim Preserve vProxyAddresses(nProxyAddresses + 1)
vProxyAddresses(nProxyAddresses + 1) = sAddress
objRecip.ProxyAddresses = vProxyAddresses
User.SetInfo
End If
‘ ##### Endcreation new primary E-mail Address #####

‘ Adjusting E-mail description field to new primary e-mail address
User.Put “mail”, PriEmailAddress
User.SetInfo

End If

Next

‘ ********************************
‘  For test purposes only
‘ MsgBox (“Einde Script :-) ”)
‘ ********************************