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.

12 total posts (Page 1 of 2)   01 | 02   Next
| Thread display: Collapse - | Expand +

All Answers

Collapse -

Infinite loop

by Tony Hopkinson In reply to Add user script from csv ...

Can't remember the exact syntax but there should be
oRecordSet.NextRecord
(or something like that)
inside the do loop

At the moment it's staying on the first line of the file.

Collapse -

Scripting

by Jacky Howe In reply to Add user script from csv ...

http://www.computerperformance.co.uk/vbscript/index.htm

Try this site it is very informative. It took me awhile to figure what i had to do but it works perfectly. If you are adding mail as well there is a script that will add it as well. If you cant work it out contact me and i will give you a working copy.
Rob

Collapse -

test

by Jacky Howe In reply to Scripting

<img src="http://i184.photobucket.com/albums/x195/fargornbeautiesmate/roberror.jpg" alt="Photobucket - Video and Image Hosting"><br><br>

Collapse -

Thank you

by imapatriot243 In reply to Add user script from csv ...

Thanks! the next record portion made it function. I appreciate the help.

Collapse -

Add user script from csv file

by maria_perez2 In reply to Add user script from csv ...

can u provide the entire script because I'm having the same problem. You mentioned u revised your script and added Next Record function.

I can't seem to figure out what u added.

Please provide the entire script or the line that u added and let me know where to add it.

Will appreciate this greatly

Collapse -

Revised the script to read the next record

by maria_perez2 In reply to Add user script from csv ...

Just FYI. was able to figure out how to read the next record by adding the line below

oRecordSet.MoveNext
loop

Collapse -

Add users from a .xls or csv file

by shaun.matheney In reply to Revised the script to rea ...

Hello, I had seen this thread and was wondering if it was the same problem I am having. I would like to add a list of users to AD using a script and and xls or csv file. Would also like to know if I have to do this from a server with active directory on it or if I can do it from my workstation. Thanks

Collapse -

add a line before loop

by wenyibing In reply to Add user script from csv ...

oRecordSet.MoveNext

Collapse -

test2

by Jacky Howe In reply to Add user script from csv ...

What is this?

Collapse -

nothing happens with the cscripts

by madhukar_m In reply to test2

Hi there,
when I run the script, It comes with no error message but I cant see any user created.
pls advice, see below output-

C>cscript createusersfromcsv.vbs
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

C>

script:-
' ---------------------------------------------------
' 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="XYZ.COM"

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

' Full path to input file
sCSVFile = sCSVFileLocation&"example.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=NO;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://cn=Users," & _
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

oRecordSet.MoveNext
loop

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

Back to Software Forum
12 total posts (Page 1 of 2)   01 | 02   Next

Related Discussions

Related Forums