Bulk importing users into Active Directory from an MS Access database


This VBScript sample imports user details from an MS Access format database and creates users in your Active Directory.

It is completely flexible and puts you in control of just how you wish to populate your directory.

With simple script code changes, users can be added to groups just after they are created, properties assigned and other properties set.

Please use this script as a guide only. Simply add your code for setting the properties you require!

 


This screen shot below shows some simple data from an access database table (userdata.mdb). For production imports, simply add the fields you require.



 

'-----------------------------------------------------------------------------------
' JustLDAP script for bulk importing users into Active Directory from
' an MS Access database
.
'
' Demonstrates standard text error message retrieval to assist debugging.
' Input is an Access 2000 Jet 4.0 database file in the same directory as this script.
'
'-----------------------------------------------------------------------------------
Option Explicit

Dim
oLdap, Result, FileName, First, Last, Login, EmployeeID
Dim ConnString, usercommonname
Dim conection ' ADO connection
Dim RecordSet ' ADO recordset

'-------- Open the database file and read the records.

FileName =
"userdata.mdb"
Set conection = CreateObject("ADODB.Connection")
Set oLdap = CreateObject("JustLDAP.Admin")

ConnString =
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & FileName

conection.Open ConnString

' Select all rows in the 'UserDetails' table.
Set RecordSet = conection.Execute("SELECT * FROM UserDetails")

' Now simply loop through each user 'row' and create them in the standard "Users"
' container of your Active Directory.
' Add some properties like employeeID to the Active Directory user database.
' Just modify this script to suit the properties you wish to import/set for each user.
 
 
Do While Not RecordSet.EOF
  ' Trim the fields in case there are leading or trailing spaces.

  First = Trim(RecordSet.Fields(
"FirstName").Value)
  Last = Trim(RecordSet.Fields(
"LastName").Value)
  Login = Trim(RecordSet.Fields(
"LoginID").Value)
  EmployeeID = Trim(RecordSet.Fields(
"employeeID").Value)

  usercommonname = First +
" " + Last

  ' All users are assigned the same password initialy
  Result = oLdap.CreateUser(Login, usercommonname, "[email protected]")

  If oLdap.ErrorOccurred() = True Then
    Result = oLdap.GetLastErrorAsMessage()
    Wscript.Echo Result
    Set oLdap = Nothing
 
  Wscript.Quit
  End If

  Result = oLdap.SetUserProperty(Login, "givenName", First)
  If oLdap.ErrorOccurred() = True Then
    Result = oLdap.GetLastErrorAsMessage()
    Wscript.Echo Result
    Set oLdap = Nothing
    Wscript.Quit
  End If
 
  Result = oLdap.SetUserProperty(Login,
"sn", Last)
  If oLdap.ErrorOccurred() = True Then
    Result = oLdap.GetLastErrorAsMessage()
    Wscript.Echo Result
    Set oLdap = Nothing
    Wscript.Quit
  End If

  Result = oLdap.SetUserProperty(Login, "userPrincipalName", Login & "@easterndigital.biz")
  If oLdap.ErrorOccurred() = True Then
    Result = oLdap.GetLastErrorAsMessage()
    Wscript.Echo Result
    Set oLdap = Nothing
   
Wscript.Quit
  End If

  Result = oLdap.SetUserProperty(Login,
"employeeID", EmployeeID)
  If oLdap.ErrorOccurred() = True Then
    Result = oLdap.GetLastErrorAsMessage()
    Wscript.Echo Result
    Set oLdap = Nothing
    Wscript.Quit
  End If

  ' Use JustLDAP account control to force the user to change the default password

  Result = oLdap.UserMustChangePasswordAtNextLogon(Login,
True)
  If oLdap.ErrorOccurred() = True Then
    Result = oLdap.GetLastErrorAsMessage()
    Wscript.Echo Result
    Set oLdap = Nothing
    Wscript.Quit
  End If
 
 
  RecordSet.MoveNext ' Loop back and read in the next account to create.
Loop
 

RecordSet.Close
Set RecordSet = Nothing
conection.Close
Set conection = Nothing
Set
oLdap = Nothing
Wscript.Echo("Done"
)

 

 



 

Google
 
Web easterndigitalsoftware.com

Copyright 2006 Eastern Digital Pty. Ltd. Australia.  ABN 87 004 274 051
Home | Contact us | Products | Downloads | Support | Privacy Policy