General discussion
Thread display: Collapse - |
All Comments
Start or search
Create a new discussion
If you're asking for technical help, please be sure to include all your system info, including operating system, model number, and any other specifics related to the problem. Also please exercise your best judgment when posting in the forums--revealing personal information such as your e-mail address, telephone number, and address is not recommended.
How to create a script that creates users and gives a login for each user
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 sUIN
Dim sLName
Dim sFName
Dim sMInitial
Dim sTitle
Dim sDepartment
Dim sFamis
Dim sWPhone
Dim sLogin
Dim nPwdLastSet
Dim nUserAccountControl ' Used to enable the account
Dim sDomain
' Modify this to match your company's AD domain
sDomain="xxxxx.edu"
' Input file location
sCSVFileLocation = "C:\temp\" 'KEEP TRAILING SLASH!
' Full path to input file
sCSVFile = sCSVFileLocation&"User list.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
sUIN = oRecordSet.Fields.Item(0).value
sLName = oRecordSet.Fields.Item(1).value
sFName = oRecordSet.Fields.Item(2).value
sMInit = oRecordSet.Fields.Item(3).value
sTitle = oRecordSet.Fields.Item(4).value
sDepartment = oRecordSet.Fields.Item(5).value
sFamis = oRecordSet.Fields.Item(6).value
sWPhone = oRecordSet.Fields.Item(7).value
sDisplayName = sLName&", "&sFName
'sPassword = oRecordSet.Fields.Item(3).value
' Create the sLogin
sLogin = (Left(sFName,1)) & (Left(sLName,1,7))
' Build the User account
Set oNewUser = oContainer.Create("User","cn="&sFName&" "&sLName)
'oNewUser.put "sAMAccountName",lcase(sUIN)
'oNewUser.put "givenName",sFName
'oNewUser.put "sn",sLName
'oNewUser.put "mn",sMInit
'oNewUser.put "title",STitle
'oNewUser.put "department",sDepartment
'oNewUser.put "famis",sFamis
'oNewUser.put "wphone",sWPhone
'oNewUser.put "login".sLogin
oNewUser.put "sAMAccountName",lcase(sLogin)
oNewUser.put "givenName",sFName
oNewUser.put "sn",sLName
oNewUser.put "UserPrincipalName",lcase(SLogin)&"@"&sDomain
oNewUser.put "DisplayName",sDisplayName
oNewUser.put "name",lcase(sLogin)
'oNewUser.put "UserPrincipalName",lcase(SLogin)&"@"&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