Question

Locked

Add user script from csv file

By imapatriot243 ·
I downloaded a script from this site for adding AD users from a csv. The problem is that I cannot get the script to go past the first user. It gets hung at the first user and stuck in an endless loop saying the first user already exsists. The script creates the first user but will not go past the error message. Any Ideas?

' ---------------------------------------------------
' Script: createusersfromcsv.vbs
' Author: Scott Lowe
' Input: CSV file with layout logonname,firstname,lastname,password
' Date: December 21, 2005
' Change log:
' no changes
'----------------------------------------------------

Option Explicit

Dim sCSVFileLocation
Dim sCSVFile
Dim oConnection
Dim oRecordSet
Dim oNewUser

' Variables needed for LDAP connection
Dim oRootLDAP
Dim oContainer

' Holding variables for information import from CSV file
Dim sLogon
Dim sFirstName
Dim sLastName
Dim sDisplayName
Dim sPassword
Dim nPwdLastSet
Dim nUserAccountControl ' Used to enable the account
Dim sDomain

' Modify this to match your company's AD domain
sDomain="sbcfenton.com"

' Input file location
sCSVFileLocation = "D:\Apps\Scripts\" 'KEEP TRAILING SLASH!

' Full path to input file
sCSVFile = sCSVFileLocation&"NewUsers.csv"

' Commands used to open the CSV file and select all of the records
set oConnection = createobject("adodb.connection")
set oRecordSet = createobject("adodb.recordset")
oConnection.open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " & sCSVFileLocation & ";Extended Properties=""text;HDR=yes;FMT=Delimited"""
oRecordSet.open "SELECT * FROM " & sCSVFile ,oConnection

' Create a connection to the Active Directory Users container.
Set oRootLDAP = GetObject("LDAP://rootDSE")
Set oContainer = GetObject("LDAP://ou=Test," & _
oRootLDAP.Get("defaultNamingContext"))

' Allows processing to continue even if an error occurs (i.e. dup user)
' We put this below the CSV and AD information since processing can
' continue with a single bad record, but not if there is a problem with
' the CSV file or AD connection
on error resume next

do until oRecordSet.EOF ' Reads the values (cells) in the sInputFile file.


' --------- Start creating user account
' Read variable information from the CSV file
' and build everything needed to create the account
sLogon = oRecordSet.Fields.Item(0).value
sFirstName = oRecordSet.Fields.Item(1).value
sLastName = oRecordSet.Fields.Item(2).value
sDisplayName = sLastName&", "&sFirstName
sPassword = oRecordSet.Fields.Item(3).value

' Build the User account
Set oNewUser = oContainer.Create("User","cn="&sFirstName&" "&SLastName)

oNewUser.put "sAMAccountName",lcase(sLogon)
oNewUser.put "givenName",sFirstName
oNewUser.put "sn",sLastName
oNewUser.put "UserPrincipalName",lcase(SLogon)&"@"&sDomain
oNewUser.put "DisplayName",sDisplayName
oNewUser.put "name",lcase(sLogon)

' Write this information into Active Directory so we can
' modify the password and enable the user account
oNewUser.SetInfo

' Change the users password
oNewUser.SetPassword sPassword
oNewUser.Put "pwdLastSet", 0

' Enable the user account
oNewUser.Put "userAccountControl", 512
oNewUser.SetInfo

' Used only for debugging
if err.number = -2147019886 then
msgbox "User logon " & sLogon & "already exists"
end if

loop


' --------- End of user account creation

This conversation is currently closed to new comments.

11 total posts (Page 2 of 2)   Prev   01 | 02
| Thread display: Collapse - | Expand +

All Answers

Collapse -

Spot the differences and then compare to the answer that was provided

by Jacky Howe In reply to nothing happens with the ...

Comparing files 1.txt and 2.TXT
***** 1.txt
' Modify this to match your company's AD domain
sDomain="sbcfenton.com"

***** 2.TXT
' Modify this to match your company's AD domain
sDomain="XYZ.COM"

*****

***** 1.txt
' Input file location
sCSVFileLocation = "D:\Apps\Scripts\" 'KEEP TRAILING SLASH!

***** 2.TXT
' Input file location
sCSVFileLocation = "C:\Scripts\" 'KEEP TRAILING SLASH!

*****

***** 1.txt
' Full path to input file
sCSVFile = sCSVFileLocation&"NewUsers.csv"

***** 2.TXT
' Full path to input file
sCSVFile = sCSVFileLocation&"example.csv"

*****

***** 1.txt
set oRecordSet = createobject("adodb.recordset")
oConnection.open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " & sCSVFileLocation & ";Extended Properties=""text;HDR=yes;FMT
Delimited"""
oRecordSet.open "SELECT * FROM " & sCSVFile ,oConnection
***** 2.TXT
set oRecordSet = createobject("adodb.recordset")
oConnection.open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " & sCSVFileLocation & ";Extended Properties=""text;HDR=NO;FMT=
elimited"""
oRecordSet.open "SELECT * FROM " & sCSVFile ,oConnection
*****

***** 1.txt
Set oRootLDAP = GetObject("LDAP://rootDSE")
Set oContainer = GetObject("LDAP://ou=Test," & _
oRootLDAP.Get("defaultNamingContext"))
***** 2.TXT
Set oRootLDAP = GetObject("LDAP://rootDSE")
Set oContainer = GetObject("LDAP://cn=Users," & _
oRootLDAP.Get("defaultNamingContext"))
*****

***** 1.txt


' --------- Start creating user account
***** 2.TXT

' --------- Start creating user account
*****

***** 1.txt
' Used only for debugging
if err.number = -2147019886 then
msgbox "User logon " & sLogon & "already exists"
end if

loop
***** 2.TXT
' Used only for debugging
'if err.number = -2147019886 then
' msgbox "User logon " & sLogon & "already exists"
'End If

oRecordSet.MoveNext
loop
*****

***** 1.txt


' --------- End of user account creation
***** 2.TXT

' --------- End of user account creation
*****

How did you find this old thread as it seems to appear and dissapear at will.

Back to Software Forum
11 total posts (Page 2 of 2)   Prev   01 | 02

Related Discussions

Related Forums