This script will create contacts in Active Directory based on a formatted CSV file. It was written to allow automatic synchronisation of contacts using a CSV file as the source.
The usage for the script will echo when run without any parameters. Here’s a summary:
cscript ContactImport.vbs -f <file Name> -o "<organisational Unit Path>"
[-r] [-s] [-a <attributes>] [-d <delimiter>] [-n "<name Format>"] [-t]
-f <file Name> - Input File Name (Required)
-o <organisational Unit Path> - Target OU for Contact Objects (Required)
-r - Read the Header Line as AD Fields
-s - Skip the Header Line
-a <attributes> - AD Attibutes to write Fields to
(Required if not using Header)
-d <delimiter> - File Delimiter. Uses comma if not specified.
-n <name Format> - DisplayName and CN Format by AD Attributes.
Uses TargetAddress if not specified.
-t - Test Only
Usage examples
Reading data from a CSV using the header line as fields
Example CSV file data:
givenName,sn,TargetAddress,streetAddress,streetAddress Chris,Dent,chris.dent@somedomain.example,123 Somewhere Street,London
The following command will read the header line as the fields to import, then create contacts using givenName and sn as the display name / name.
cscript ContactImport.vbs -f Contacts.csv -o "OU=Contacts,DC=somedomain,DC=example" -r -n "givenName sn"
The attribute streetAddress appears twice, it will be concatenated using a comma as a delimiter. The streetAddress attribute will contain “123 Somewhere Street, London” in this example.
Defining attributes on the command line
Example CSV file data:
Chris,Dent,chris.dent@somedomain.example,SomeCorp,SomeOffice
The following command will read the file, then import attributes based on the -a value which must reflect the order of attributes in the import file.
cscript ContactImport.vbs -f Contacts.csv -o "OU=Contacts,DC=somedomain,DC=example" -a "givenName,sn,targetAddress,company,physicalDeliveryOfficeName"
Because no displayName format has been specified the targetAddress field will be used.
Using a custom file delimiter
Example file data:
FirstName|LastName|Address Chris|Dent|chris@somedomain.example
In this example the header line contains names that cannot be used as attributes in Active Directory so the script is instructed to ignore it with -s. The fields are defined instead using the -a option. The Delimited is specified with -d.
cscript ContactImport.vbs -f Contacts.txt -o "OU=Contacts,DC=somedomain,DC=example" -a "givenName,sn,targetAddress" -s -d "|"
As above, because no displayName format is specified the targetAddress is used.
No related posts.
Related posts brought to you by Yet Another Related Posts Plugin.
Jake says:
Hi Chris
This looks like a brilliant script.
Do you perhaps have an example csv file that I can look at?
Thanks
January 6, 2009, 7:15 pmJake
Chris says:
Hey Jake,
I’ve added a few examples, is that enough to get you going?
All the best,
January 8, 2009, 11:51 amChris
Ken K. says:
Hello Chris,
I spent a good bit of time trying to locate a well-written, flexible, VBScript to automate Contact creation. I think yours is the best I’ve found.
-k²
August 4, 2009, 8:11 amKen K. says:
Hello Chris,
I have a question regarding “dangerous” Contact object LDAP attributes: Do you happen to know which LDAP attributes for Contacts can and cannot be imported?
It is fairly obvious that some attributes should not be imported: uSNCreated and uSNChanged for example. No big deal there—there’s no obvious advantage to doing so that I am aware of. That said, there are a number of other attributes that are more ambiguous.
My interest in this is general purpose – don’t want to mess anything up. More specifically though, we’d like to be able to import Contacts and have them mail enabled immediately. It’s not always clear which attributes we need to do so.
Any advice or direction would be greatly appreciated.
Thanks,
August 6, 2009, 8:54 pmKen
David G. says:
I realize this is a few years out, but I had to say I ran this script to import a long list of names and e-mail addresses for an Exchange distribution group and it worked wonderfully. Thanks for all the hard work!
January 7, 2011, 8:41 pmSteve says:
Hi Chris,
3 years later and still posting about this script :)
I agree with everyones comments, this script works rather damn well. Thankyou!
I did have one interesting issue with it though.
In my import CSV if I ran with givename as the first column, for some reason my display name when using the -n switch and specifying givenname (or givenname sn – company) would concatenate both the sn + givenname (bloggsjoe) for the Displayname (bloggsjoe bloggs – company).
If you change the first column to something other than Givenname first (I used company) it works perfectly.
I am not quite sure how this is happening or why, I spent a bit of time looking through the code but just couldn’t pick where it was doing it.
Cheers,
January 14, 2011, 3:03 amSteve
Chris says:
That’s a little unexpected. Would you be able to provide a sample of the file you’re using? I tried to duplicate with this one but cannot reproduce the bug:
givenname,sn,company,targetAddress
bob,hope,somecompany,bob@hope.com
Associated command to execute the script:
cscript ContactImport.vbs -f test.csv -o “OU=Somewhere,DC=domain,DC=example” -r -t -n “givenname sn – company”
January 14, 2011, 9:00 amSteve says:
Hi Chris,
For the life of me I have not been able to replicate the issue again. I have tried a few different combinations with the givenname first but they have all worked successfully.
There is another question I have in regards to Proxyaddresses. Is it possible to import multiple proxyaddresses with the script? For instance I need to be able to include the standard SMTP proxy address but also an X500 address. I have tried using a few different delimiter types but so far I haven’t had any luck.
Cheers,
February 3, 2011, 2:35 amSteve
Chris says:
Hmm in theory it might work if you call the field proxyAddresses, and use the modified CheckUpdate Subroutine below. Ensure you include the address type prefix (e.g. smtp:bob@domain.example X500:/somevalue/).
Afraid I can’t test it, but if you’d like to take the update for a spin? :)
Sub CheckUpdate(objContact, strAttribute, strValue) Const ADS_PROPERTY_APPEND = 3 Dim strCurrentValue Dim arrValues On Error Resume Next If LCase(strAttribute) = "proxyaddresses" Then arrValues = Split(strValue, ", ") For Each strValue in arrValues WriteLog "Appending " & strAttribute & " Attribute: " &_ objContact.Get("displayName") & ": New: " & strValue, True If booTestOnly = False Then Err.Clear objContact.PutEx ADS_PROPERTY_APPEND, strAttribute, Array(strValue) objContact.SetInfo If Err.Number <> 0 Then WriteLog objContact.Name & ": Error Updating " & strAttribute & " with " & strValue, True WriteLog objContact.Name & ": " & Err.Description End If End If Next Else strCurrentValue = "" : strCurrentValue = objContact.Get(strAttribute) If LCase(strCurrentValue) <> LCase(strValue) Then WriteLog "Updating " & strAttribute & " Attribute: " &_ objContact.Get("displayName") & ": Old: " & strCurrentValue &_ " New: " & strValue, True If booTestOnly = False Then Err.Clear objContact.Put strAttribute, strValue objContact.SetInfo If Err.Number <> 0 Then WriteLog objContact.Name & ": Error Updating " & strAttribute & " with " & strValue, True WriteLog objContact.Name & ": " & Err.Description End If End If End If End If On Error Goto 0 End SubChris
February 3, 2011, 8:53 amSteve says:
Amazing! Thank you so much!
I will give it a go today!
Cheers,
February 3, 2011, 8:51 pmSteve
Steve says:
Hi Chris,
I have given it a go with some success. I have replaced the separator with ; for the proxyaddresses and importing multiple SMTP addresses works correctly. When entering in a X500 address with the format like:
X500:/O=FCL/OU=AUST/cn=Recipients/cn=TestContact the import doesn’t work.
This is the log entry for it:
Appending proxyaddresses Attribute: aaEmail aaMigration – company: New: /o
The problem I think is this proxyaddresses is also delimited by = which is breaking up the X500 address.
So I think before it even gets to the adding proxyaddresses the string is broken.
I am working through it here to see if I can get it to parse the address correctly but any assistance would be great!
Cheers,
February 3, 2011, 11:41 pmSteve
Steve says:
Chris!
I got it working. I am not sure just how elegant it is but it definitely works. When reading the file you match the attribute with the value using = then later on you split it using = for creating and updating the contact.
All I did was replace the = with ^ in the 4 locations needed in the script and bing off we go.
Thank you so much for spending your own time in coming up with the Proxyaddress update. It has saved me so much time and pain.
Cheers,
February 4, 2011, 12:42 amSteve
Chris says:
No problem, I’m glad you got it working in the end :)
Chris
February 4, 2011, 8:42 amjosh says:
one other thing that is important if exchange is to send email to these contacts is the field internetEncoding, which needed to be set to 1310720 in order to work..
March 4, 2011, 2:31 pmGary says:
This script is fantastic.
If your domain name changes, update your csv file and then rerun this script. It will update all the contacts.
Absolutely fantastic.
June 8, 2011, 6:16 pm