Question

Locked

Need add user VBS script from csv file OU member of

By jamesolds ·
I downloaded this VBS script from techrepublic and it works great, but I'd like to know if one of you hot-shot VBS experts can tell me what to add & where so that I can assign these new users to an OU (pulled from the CSV) thru the script. Oh, and if you have some extra time could you please provide the instructions for "member of" and "description" also. (I need to add a bunch of teachers and would like them to be in a "teachers" OU and with a description "teacher", if possible)

Thanks.

' ---------------------------------------------------
' 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="fwafa.org"
' Input file location
sCSVFileLocation = "\\postoffice\installs\restricted\scripts\ad_user_import\" 'KEEP TRAILING SLASH!
' Full path to input file
sCSVFile = sCSVFileLocation&"new_users.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

This conversation is currently closed to new comments.

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

All Answers

Collapse -

The OU in which to place your user(s)

by neilb@uk In reply to Need add user VBS script ...

is set in the line (split for clarity)

Set oContainer = GetObject("LDAP://cn=Users," & _
oRootLDAP.Get("defaultNamingContext"))

So, replace "cn=users" with the required OU's FQDN (without the domain bits because that's what defaultNamingContext gets you. Because it will have commas,wrap it in quotes in your CSV, stick it on the end of the user details line and access it by:

sContainer = oRecordSet.Fields.Item(4).value

Description, the same way although it will be sDescription = oRecordSet.Fields.Item(5).value and

oNewUser.Put "Description", sDescription before the oNewUSer.Setinfo

OK?

Back to Software Forum
2 total posts (Page 1 of 1)  

Related Discussions

Related Forums