We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Import Email addresses from CSV file into Active Directory Windows 2003

Medium Priority
900 Views
Last Modified: 2012-05-06
Hi,

I have a CSV file that containing Last Name, First Name , User Name , Email data.
Last Name, First Name and User Name are already in the AD and in use, but i need to add the Email part without generating more User accout problem :o(

Any script or command syntax that I can use?

Best regards,

Bjarte
Comment
Watch Question

Chris DentPowerShell Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:

Hey,

With Exchange or without?

Chris
Bjarte FjellandIntegration Architect

Author

Commented:
Hi Chris,

It's without Exchange, we are using Lotus Notes.
PowerShell Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:

Great, just didn't want to break that :)

The following VbScript (save as .vbs) takes a simple CSV file in the following format:

FirstName,LastName,Username,Email

And imports the Email into the mail field (as seen on the general tab in AD Users and Computers) for each Username.

I recommend you test it on a very small set of users before feeding it the entire CSV. Do let me know if the CSV file also includes quotes, that'll take a little more effort.

Chris
Option Explicit
 
Const FILE_NAME = "TheFile.csv"
 
Function GetObjectDN(strObject, strDomain)
  ' Return Type: String
  '
  ' Returns a Distinguished Name for an Object from it's NT SAM ID.
  ' This will only function for valid object types within an NT Domain structure.
 
  Const ADS_NAME_INITTYPE_GC = 3
  Const ADS_NAME_TYPE_1779 = 1
  Const ADS_NAME_TYPE_NT4 = 3
 
  Dim objNameTranslate
  Dim strObjectDN
 
  On Error Resume Next : Err.Clear
  Set objNameTranslate = CreateObject("NameTranslate")
 
  objNameTranslate.Init ADS_NAME_INITTYPE_GC, ""
  objNameTranslate.Set ADS_NAME_TYPE_NT4, strDomain & "\" & strObject
  strObjectDN = objNameTranslate.Get(ADS_NAME_TYPE_1779)
  If Err.Number <> 0 Then  ' Make the DN Blank for a Failed Search
    strObjectDN = ""
  End If
 
  Set objNameTranslate = Nothing
  On Error Goto 0
 
  GetObjectDN = strObjectDN
End Function
 
Dim objFileSystem : Set objFileSystem = CreateObject("Scripting.FileSystemObject")
Dim objFile : Set objFile = objFileSystem.OpenTextFile(FILE_NAME, 1, False, 0)
 
Dim objNetwork : Set objNetwork = CreateObject("WScript.Network")
Dim strDomain : strDomain = objNetwork.UserDomain
 
Do Until objFile.AtEndOfStream
  Dim arrLine : arrLine = Split(objFile.ReadLine, ",")
 
  Dim strUsername : strUsername = arrLine(2)
  Dim strEmail : strEmail = arrLine(3)
 
  Dim strDN : strDN = GetObjectDN(strUsername, strDomain)
 
  If strDN <> "" Then
    Dim objUser : Set objUser = GetObject("LDAP://" & strDN)
 
    objUser.Put "mail", strEmail
    objUser.SetInfo
 
    Set objUser = Nothing
  End If
Loop

Open in new window

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Bjarte FjellandIntegration Architect

Author

Commented:
Thank you, that worked perfectly!
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.