Link to home
Start Free TrialLog in
Avatar of cullyk
cullykFlag for Australia

asked on

Import Contacts into Active Directory

Hi,
I need to import 700+ contacts into active directory. I have seen a previous question in relation to this and have included the code and the excel file however I need some changes made and some guidance. I would like the script to import the following fields:-
LastName      FirstName      Job Title      eMail Address
Office #      Mobile #      Division      Department         Office
I also need to know what parts of the script I need to change and if this needs to be run on the server or not? I have some previous experience with vbs just not interacting with AD. Thanks in advance.

Option Explicit
Dim objRootLDAP, objContainer, objContact, objExcel, objSheet
Dim strOU, strContactName, strPathExcel, strEmail, strProxy
Dim intRow, strYourDescription, strFirst, strLast, strMainDefault
Dim strMailbox, strNick
Const xlUp = -4162
 
' Set string variables
strOU = "OU=Contacts,OU=TestOU," ' Note the comma
strYourDescription = "Founder"
strPathExcel = "c:\contactsEx3.xls"
strPathExcel = Replace(WScript.ScriptFullName, WScript.ScriptName, "") & "contactsEx3.xls"
strYourDescription = "Guy's Contact"
intRow = 3 ' Row 1 contains headings
 
' Section to bind to Active Directory
If Trim(strOU) <> "" Then
	If Right(strOU, 1) <> "," Then strOU = strOU & ","
Else
	strOU = ""
End If
Set objRootLDAP=GetObject("LDAP://rootDSE")
Set objContainer=GetObject("LDAP://" & strOU & objRootLDAP.Get("defaultNamingContext"))
 
' Open the Excel spreadsheet
Set objExcel = CreateObject("Excel.Application")
Set objSheet = objExcel.Workbooks.Open(strPathExcel)
objExcel.Visible = True
 
' Here is the loop that cycles through the cells
For intRow = 2 To objExcel.Cells(65536, "A").End(xlUp).Row
	strContactName = objExcel.Cells(intRow, 1).Value
	strEmail = objExcel.cells(intRow, 2).Value
	strFirst = objExcel.cells(intRow, 3).Value
	strLast = objExcel.cells(intRow, 4).Value
	strProxy = objExcel.cells(intRow, 5).Value
	strMainDefault = objExcel.cells(intRow, 6).Value
	strMailbox = objExcel.cells(intRow, 10).Value
	strNick =strContactName
	 
	 
	' Build the actual contacts.
	Set objContact = objContainer.Create("Contact",_
	"cn=" & strContactName)
	objContact.Put "Mail", strEmail
	objContact.Put "givenName", strFirst
	objContact.Put "sn", strLast
	objContact.Put "proxyAddresses", strProxy
	objContact.Put "targetAddress", strMainDefault
	objContact.Put "legacyExchangeDN", strMailbox
	objContact.Put "mailNickname", strNick
	objContact.SetInfo
	 
Next
objExcel.Quit
WScript.Quit

Open in new window

ContactsEx3.xls
Avatar of William Elliott
William Elliott
Flag of United States of America image

not sure what fields you are using for jobtitle, division, department

Last name = sn
First name = givenName
Job Title
Email = mail
Telephone number = telephoneNumber
Other Telephone numbers = otherTelephone
Mobile = mobile
Other Mobile numbers = otherMobile
Division
Department
Company = company
Office = physicalDeliveryOfficeName


given this information from the script
   strContactName = objExcel.Cells(intRow, 1).Value
   strEmail = objExcel.cells(intRow, 2).Value
you can assertane that
the contactname would be in column 1
the email address is in column 2

now to enter this information into AD
you need to define the person "objcontact"
   Set objContact = objContainer.Create("Contact", "cn=" & strContactName)
and with this person add they stuff
   objContact.Put "Mail", strEmail
from above you have
objcontact.put = defining that you are adding something to the particular user
"mail" = this is the AD value denoting email address
stremail = the value pulled from excel
a little more information...

this line
strOU = "OU=Contacts,OU=TestOU," ' Note the comma
tells you which OU to add the users.



and no, it doesn't need to be run from a domain crontroller, you shouel be able to run it from an xp workstation,.. but you need to be using an ID that has access to create accounts
Avatar of cullyk

ASKER

Thanks for that. After some playing around I have come up with the following code.
This works well but I have one problem. If all the data isnt in the speadsheet ie if a contact doesnt have a mobile number, the script will just ignore it all together. I need this to still create the contact with the information provided. Thanks
Option Explicit
Dim objRootLDAP, objContainer, objContact, objExcel, objSheet
Dim strOU, strContactName, strPathExcel, strEmail
Dim intRow, strYourDescription, strFirst, strLast
Dim strOfficeNo, strMobileNo, strTitle, strOffice
Dim strDepartment, strCompany
 
' Set string variables
' Note: Assume an OU called TestContacts exists.
strOU = "OU=TestContacts ," ' Note the comma
strPathExcel = "C:\contacts.xls"
strYourDescription = "Company Contacts"
intRow = 3 ' Row 1 contains headings
 
' Section to bind to Active Directory
Set objRootLDAP = GetObject("LDAP://rootDSE")
Set objContainer = GetObject("LDAP://" & strOU _
& objRootLDAP.Get("DefaultNamingContext")) 
 
' Open the Excel spreadsheet
Set objExcel = CreateObject("Excel.Application")
Set objSheet = objExcel.Workbooks.Open(strPathExcel)
 
' Here is the loop that cycles through the cells
Do Until (objExcel.Cells(intRow,1).Value) = ""
   strContactName = objExcel.Cells(intRow, 1).Value
   strEmail = objExcel.cells(intRow, 2).Value
   strFirst = objExcel.cells(intRow, 3).Value
   strLast = objExcel.cells(intRow, 4).Value
   strOfficeNo = objExcel.cells(intRow, 5).Value
   strMobileNo = objExcel.cells(intRow, 6).Value
   strTitle = objExcel.cells(intRow, 7).Value
   strOffice = objExcel.cells(intRow, 8).Value
   strDepartment = objExcel.cells(intRow, 9).Value
   strCompany = objExcel.cells(intRow, 10).Value
   
   ' Build the actual contacts.
   Set objContact = objContainer.Create("Contact","cn=" _ 
   & Replace(strContactName,",","\,"))
   objContact.Put "Mail", strEmail
   objContact.Put "givenName", strFirst
   objContact.Put "sn", strLast
   objContact.Put "telephoneNumber", strOfficeNo
   objContact.Put "mobile", strMobileNo
   objContact.Put "title", strTitle
   objContact.Put "physicalDeliveryOfficeName", strOffice
   objContact.Put "department", strDepartment
   objContact.Put "company", strCompany
   objContact.SetInfo 
intRow = intRow + 1
Loop
objExcel.Quit 
 
WScript.Quit 
 
' End of Sample ContactExcel VBScript

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of William Elliott
William Elliott
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of cullyk

ASKER

thanks for the follow up mate, you have done well
glad to help :)