Anda di halaman 1dari 4

Create users in Active Directory from a Version 1.

0
CSV file with this VBScript January 5, 2006

By Scott Lowe

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 script will create users with passwords in Active Directory in the Users container. You must make a few
minor modifications, explained below, for the script to work in your environment.

A few notes about this script


It’s best to execute these scripts from a domain controller while logged in with administrative credentials. Of
course, you can use the Windows Scheduler to handle this. Second, I’m assuming that you can get a comma-
separate value (CSV) extract from whatever ERP system you use. Finally, I save my scripts in the C:\Scripts
folder on my lab server and also placed the CSV file extract in that location.

Before you begin writing Windows scripts, create a file called schema.ini and place it in your scripts folder. The
contents of schema.ini should be:

[example.csv]
Format=CSVDelimited
ColNameHeader=False

Why do you need this? If your CSV file doesn’t have a header row, some versions of (Microsoft Data Access
Components) MDAC have problems processing the first row of data in your CSV extract file. Basically, your script
may complete ignore the first row of information. If you do use a header row (which I do not for these examples),
you don’t need to create this file.

Modifications you need to make


Make the following modification before using the script:

1. Change the value of the sDomain variable to match the name of your Active Directory environment. I've
used example.com for my sample script.
2. Change the value of the sCSVFileLocation variable. This is the directory in which you will store a CSV
file. Be sure to keep the trailing slash at the end of the variable.
3. Change the value of the sCSVFile variable. This is the name of your CSV file. I've used "example.csv" in
my script.

Using this script


One very important note: the passwords that you use in your CSV file to be imported must meet your system's
minimum complexity requirements. If they do not, the user will not be assigned a password.

You can use this script manually by executing cscript createusersfromcsv.vbs from the command line. No
command line parameters are required since everything is handled from within the script. You could also
schedule this script to be run on a periodic basis.

Page 1
Copyright ©2006 CNET Networks, Inc. All rights reserved.
For more downloads and a free TechRepublic membership, please visit http://techrepublic.com.com/2001-6240-0.html
Create users in Active Directory from a CSV file with this VBScript

' ---------------------------------------------------
' 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="example.com"

' Input file location


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

' Full path to input file


sCSVFile = sCSVFileLocation&"Book2.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

Page 2
Copyright ©2006 CNET Networks, Inc. All rights reserved.
For more downloads and a free TechRepublic membership, please visit http://techrepublic.com.com/2001-6240-0.html
Create users in Active Directory from a CSV file with this VBScript

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

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

Page 3
Copyright ©2006 CNET Networks, Inc. All rights reserved.
For more downloads and a free TechRepublic membership, please visit http://techrepublic.com.com/2001-6240-0.html
Create users in Active Directory from a CSV file with this VBScript

Additional resources
• TechRepublic's Downloads RSS Feed
• Sign up for TechRepublic's Downloads Weekly Update newsletter
• Sign up for TechRepublic's Network Administration NetNote
• Check out all of TechRepublic's free newsletters
• Add Global Groups to computers with VBScript
• Two more member-submitted scripts that automate network admin activities
• Automate mundane network admin tasks with four member-submitted scripts
• Three member-submitted admin scripts that will simplify your daily tasks
• Toggle Windows XP Pro IP address settings with this VB script
• Windows XP Pro File Sharing Interface Switcher VB script
• Quickly grab Event Log information in Windows with this script

Version history
Version: 1.0
Published: January 5, 2006

Tell us what you think


TechRepublic downloads are designed to help you get your job done as painlessly and effectively as possible.
Because we're continually looking for ways to improve the usefulness of these tools, we need your feedback.
Please take a minute to drop us a line and tell us how well this download worked for you and offer your
suggestions for improvement.

Thanks!

—The TechRepublic Downloads Team

Page 4
Copyright ©2006 CNET Networks, Inc. All rights reserved.
For more downloads and a free TechRepublic membership, please visit http://techrepublic.com.com/2001-6240-0.html

Anda mungkin juga menyukai