General discussion

Locked

How to create a script that creates users and gives a login for each user

By polombo ·
I have a script that creates users from a cvs file, but at the same time I want this script to take the first character of the firstname and the 7 first characters of the lastname and store it in a variable. And then I want to populate ActiveDirectory with all the fields created on the cvs file, including the new variable that stores the "login". And I dont know how to do it. I am also a lit confuse with the attributes, thats why I comment out some of them. I am a java programmer with low experience in scripting. Any suggestion or any help will be appreciated.Here is the script:

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

This conversation is currently closed to new comments.

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

All Comments

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

Related Discussions

Related Forums