'==========================================================================
'
' NAME: AppendSMTPbasedonUSerOUPrimaryAddress.vbs
'
' AUTHOR: Mark D. MacLachlan , The Spider's Parlor
' URL: [URL unfurl="true"]http://www.thespidersparlor.com[/URL]
' DATE : 7/26/2005
'
' COMMENT: Thanks go out to Tek-Tips user K0b3 for the
' SearchDistinguishedName function.
'
'==========================================================================
On Error Resume Next
'open the file system object
Set oFSO = CreateObject("Scripting.FileSystemObject")
set WSHShell = wscript.createObject("wscript.shell")
'open the data file
Set oTextStream = oFSO.OpenTextFile("user.txt")
'make an array from the data file
UsersList = Split(oTextStream.ReadAll, vbNewLine)
'close the data file
oTextStream.Close
For Each UserString In UsersList
'First grab the DistinguishedName
UserDN = SearchDistinguishedName(UserString)
'Now bind to the user object
Set UserObj = GetObject("LDAP://" & UserDN)
'Find the Relative Distinguished Name (RDN)
UserRDN = UserObj.Name
'Subtract the length of the RDN plus 4 more for the comma OU=
'You now have the full path to the users OU
FullOU = Right(UserDN,Len(UserDN)-Len(UserRDN)-4)
'Now split the OU on the comma to get the users OU.
OUArray = Split(FullOU,",")
UserOU= OUArray(0)
'Finally now we can append an SMTP address.
Set objRecip = UserObj
sAddress = "SMTP:" & UserString & "@" & UserOU& ".companyname.com"
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
bIsFound = 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
oUser.SetInfo
End If
Next
Public Function SearchDistinguishedName(ByVal vSAN)
' Function: SearchDistinguishedName
' Description: Searches the DistinguishedName for a given SamAccountName
' Parameters: ByVal vSAN - The SamAccountName to search
' Returns: The DistinguishedName Name
Dim oRootDSE, oConnection, oCommand, oRecordSet
Set oRootDSE = GetObject("LDAP://rootDSE")
Set oConnection = CreateObject("ADODB.Connection")
oConnection.Open "Provider=ADsDSOObject;"
Set oCommand = CreateObject("ADODB.Command")
oCommand.ActiveConnection = oConnection
oCommand.CommandText = "<LDAP://" & oRootDSE.get("defaultNamingContext") & _
">;(&(objectCategory=User)(samAccountName=" & vSAN & "));distinguishedName;subtree"
Set oRecordSet = oCommand.Execute
On Error Resume Next
SearchDistinguishedName = oRecordSet.Fields("DistinguishedName")
On Error GoTo 0
oConnection.Close
Set oRecordSet = Nothing
Set oCommand = Nothing
Set oConnection = Nothing
Set oRootDSE = Nothing
End Function