' Section to bind to Active Directory
Set objContainer=GetObject("LDAP://OU=OnStream,DC=MyCompany,DC=local")
' Delete all the current contacts from the OnStream OU
for each objContact in objContainer
objContainer.delete "contact", "cn=" & objContact.CN
next
' Loop through the list of contacts
do while not rs.EOF
' Set string variables
strMainDefault = "SMTP:" & rs("Email")
strContactName = rs("name")
strFirst = rs("firstname")
strLast= rs("surname")
strDisplay = rs("compName") & " - " & rs("name")
strTitle = "" & rs("title")
strEmail = rs("Email")
strNickName = Replace(rs("name"), " ", "")
strMailbox = "/o=My Company/ou=MY_COMPANY/cn=Recipients/cn=" & strNickName
ChecContact(strContactName)
' Build the contact and store in AD
Set objContact = objContainer.Create("Contact","cn=" & strContactName)
objContact.Put "givenName", strFirst
objContact.Put "sn", strLast
if strTitle <> "" then objContact.Put "title", strTitle
objContact.Put "description", "OnStream"
objContact.Put "displayName", strDisplay
objContact.Put "company", "" & rs("compName")
objContact.Put "Mail", strEmail
objContact.Put "targetAddress", strMainDefault
objContact.Put "legacyExchangeDN", strMailbox
objContact.Put "mailNickname", strNickName
objContact.SetInfo
' Close the conact object
Set objContact = Nothing
' Move to the next contact
rs.MoveNext
loop
' Close the LDAP Container Object
Set objContainer = Nothing
Function CheckContact(name)
Set objConnection = CreateObject("ADODB.Connection")
objConnection.Open "Provider=ADsDSOObject;"
Set objCommand = CreateObject("ADODB.Command")
objCommand.ActiveConnection = objConnection
objCommand.CommandText = _
"<LDAP://OU=OnStream,DC=MyCompany,DC=local>;;name;onelevel"
Set objRecordSet = objCommand.Execute
While Not objRecordset.EOF
If objRecordset.Fields("name") = strContactName Than
strContactName = strContactName & rs("CompName")
objRecordset.MoveNext
Else
objRecordset.MoveNext
End If
Wend
objConnection.Close
End Function