Change primary proxyAddresses
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
”)
‘Â ********************************

Recent Comments