Discussion on:
DOWNLOAD: Create users in Active Directory from a CSV file with VBScript

22
Comments

Join the conversation!

Follow via:
RSS
Email Alert
Just In
what's the content of the csv file?
palloma 27th Oct 2011
what's the content of the csv file?
Creating users can be tedious, especially when user information already resides in your company's HR system. Instead of manually creating a user and assigning passwords, you can use a script that accomplishes the same goal. The benefits: Fewer iterations of the information will result in fewer errors; you don't need to worry about whether or not a user account gets created; and, you can focus on more strategic IT tasks. This handy, VBScript will create users with passwords in Active Directory in the Users container.

Download and try the script:
http://techrepublic.com.com/5138-10877-6019533.html

Join this ongoing discussion and let us know if this script is helpful and if there's anything we can do to improve the download's format or content.
0 Votes
+ -
Loop Missing
bowlsys 8th Jan 2006
The script appears to have a missing loop statement
I fixed it. I found out that it was the select statement in this script. The concatenation of sCSVfile with the select statement was not working right. Here is what I changed it to.

oInputRecordSet.open "SELECT * FROM example.csv", oInputConnection

Rather than calling the variable since that was not working right I put the name of the file in the select statement.

I also made a change to the oContainer getobject because I wanted to populate to an OU and not the users container.

Set oContainer = GetObject("LDAP://OU= Name,dc=example,dc=com")

Also this is a common error message -2147016657. It is in reference to exchange sLDAPExchangeServer or sLDAPhomeMDB. Check the values for these two variables again from the exchattrib.csv file. This error message is hard to find why this happens.

Here is one more change I made. The reason why I hard coded this was because again the concatenation of this line was not working write.

oNewUser.put "mail", lcase(SLogon) & "@example.com"

I really wanted to say thank you for creating this script it works great with a couple of tweaks but overall GREAT JOB!
The script looks for the "example.csv" file but I not really sure what to put in here?

Thanks
Look for section titled "' --------- Start creating user account
' Read variable information from the CSV file
' and build everything needed to create the account"

Then look that the value for each variable
item(0) = column A
item(1) = column B

and so on.
Anyone have an idea of what this could mean?
I would hard code the variables in section titled ?Build and write the users Exchange attributes?. One of those variables is incorrect. Either it is the concatenation of that string or string is incorrectly formatted. Did you get your values from your csv dump of you ldap?
0 Votes
+ -
The script works when you carrefully add the correct variables, but it creates the mailboxes as legacy mailbox and I even dont have any legacy exchange server. Is there anybody with the same problem?
I have been trying to set the user accounts so they did not have to change their passwords at next login. We are setting this up for DR purposes and if you login over Outlook Web Access it does not give you the option to change your password at login. Here is what you need to set in the script to not have them change password at next login.

oNewUser.Put "pwdLastSet", -1
Ensure you add the following at the end of the script so it reads the next line in the CSV file:

oRecordSet.MoveNext
loop
0 Votes
+ -
Is It also possible to add a user discription?
oNewUser.put "description","Your User Description"

You could also import it from the CSV file.
0 Votes
+ -
I keep getting "Exchange attributes error : -2147016657", any pointers on what that may be?
I would also like to create within a specific OU via the .csv file, actually nested OU. Is that possible?
And as it's only 362 days until Christmas; a pointer for how to create a home directory as well would be great.
I'm trying to teach myself via Don Jones' "Managing Windows with VBScript and WMI". Any suggestions for books that would be helpful?

Thanks all.
i am too getting the error 'Exchange attributes error : -2147016657' users are created, but mailboxes are not and neither are passwords.
I managed to get them in to an ou, you just change line 99 from :
Set oContainer = GetObject("LDAP://cn=Users," & _

to

Set oContainer = GetObject("LDAP://ou=your ou name," & _
I don't mind even if exclude Exchange Attributes.
0 Votes
+ -
never mind
itandweb@... Updated - 25th Sep 2007
never mind
Here is the script:
' ---------------------------------------------------
' Script: createusersfromcsv.vbs
' Author: Scott Lowe
' Input: CSV file with layout logonname,firstname,lastname,password
' Date: December 21, 2005
' Change log:
' no changes
' Rev: by: Hector Guerra 10-16-2007
' Help by and rewrite by: Sergio Mion
'----------------------------------------------------

Option Explicit

Dim sCSVFileLocation
Dim sCSVFile
Dim objFSO
Dim objFile
Dim strLine
Dim strItems
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="Autocadcc6.us"

' ----------Input file location----------
sCSVFileLocation = "C:\Scripts\"
' ----------Full path to input file----------
sCSVFile = sCSVFileLocation&"studentinfo45.csv"

' ----------Commands used to open the CSV file and select all of the records----------
set objFSO = CreateObject("Scripting.FileSystemObject")
set objFile = objFSO.OpenTextFile(sCSVFile, 1)

' ----------Create a connection to the Active Directory Users container.----------
' ----------My OU's are 2-3 period, 4-5 period, 7-8 period. My server name is CAD.----------
Set oContainer = GetObject("LDAP://cad/OU=4-5 Period,dc=Autocadcc6,dc=us")

' ----------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 objFile.AtEndOfStream ' 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
strLine = objFile.ReadLine
strItems = split(strLine,",")

sLogon = strItems(0)
sFirstName = strItems(1)
sLastName = strItems(2)
sDisplayName = sFirstName&" "&sLastName
sPassword = strItems(3)

' ----------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", -1

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

Loop
objFile.Close

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

' --------- End of user account creation----------
0 Votes
+ -
I have tried the script shown here, but always get an error when running it. The line with the error is Set oContainer = GetObject("LDAP://server1/OU=students,dc=server1,dc=local")

It says a referral was returned from the server 8007202b null.

Our domain controller is server1.delasalle.local


How can I adjust this line to meet our needs? I have tried a few different ways, but always get the error.

Thank you for any help you can give me,
0 Votes
+ -
your domain controller is server1.delasalle.local

you posted ("LDAP://server1/OU=students,dc=server1,dc=local")

It should read:
("LDAP://server1/OU=students,dc=delasalle,dc=local")

hope this helps as i am new to scripts myself.
I'm getting error "Exchange attributes error : -2147016657" has anyone ever resloved this issue?
This script will work pretty much out of the box if you like although there is a pretty major omission at the end of the "User Creation" loop.

Ensure you insert the following two lines at the end of the script

---
oRecordset.MoveNext
Loop
---

This will ensure that ADO moves to the next line in the csv file, otherwise the script will continually loop on the first entry in the file because it will create it once and then attempt to continually create the first account because it is not being told to move onto the next record.
what's the content of the csv file?
Keyboard Shortcuts:
Prev
Next
Toggle
Join the conversation
Formatting +
BB Codes - Note: HTML is not supported in forums
  • [b] Bold [/b]
  • [i] Italic [/i]
  • [u] Underline [/u]
  • [s] Strikethrough [/s]
  • [q] "Quote" [/q]
  • [ol][*] 1. Ordered List [/ol]
  • [ul][*] · Unordered List [/ul]
  • [pre] Preformat [/pre]
  • [quote] "Blockquote" [/quote]

Join the TechRepublic Community and join the conversation! Signing-up is free and quick, Do it now, we want to hear your opinion.