Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Create user in OU, set with variable

Status
Not open for further replies.

HVtech

IS-IT--Management
Jul 1, 2010
71
NL
Hi all,
I am trying to create users in a specific OU, which should be read from a csv file.
This is a line from the csv:
AAipuk1111,Aai,Puk,Welkom2012,HVServ.local,Users,Aai Puk,TopOU,SubOU

VBS script:
Code:
' ---------------------------------------------------
' Script: createusers2.vbs
' Input: It uses a CSV file with layout logonname,firstname,lastname,password


Option Explicit

Dim sCSVFileLocation
Dim sCSVFile
Dim objFSO
Dim objFile
Dim strLine
Dim strItems
Dim oNewUser

' ----------LDAP connection variables----------
Dim oRootLDAP
Dim oContainer
Dim sOU1
Dim sOU2

' ----------Other variables--------------------
Dim sLogon
Dim sFirstName
Dim sLastName
Dim sDisplayName
Dim sPassword
Dim nPwdLastSet
Dim nUserAccountControl ' Used to enable the account
Dim sDomain
Dim sEmailaddress

' ----------Modify this to match your company's AD domain----------
sDomain="HVServ.local"

' ----------Input file location----------
sCSVFileLocation = "C:\ServShare\Bulk\"
' ----------Full path to input file----------
sCSVFile = sCSVFileLocation&"test.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.----------
'Eerst domeinnaam, OU, top OU, domeinnaam gesplitst
Set oContainer = GetObject("LDAP://HVServ.local/OU=" & sOU2 & ",OU=" & sOU1 & ",OU=Examen,OU=leerling,dc=HVServ,dc=local")
'Set oContainer = GetObject("LDAP://HVServ.local/OU=BB-BTT,OU=BB,OU=Examen,OU=Leerling,dc=HVServ,dc=local")
'When I use this second line, it works fine

' ----------Allows processing to continue even if an error occurs (i.e. dup user)----------
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 = strItems(6)
sPassword = strItems(3)
sOU1 = strItems(7)
sOU2 = strItems(8)





' ----------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----------

It looks if there is a syntax error, but I cannot find it..
Anyone can help me with this?
I need ot import about 150 users, divided over 13 OU's
CSV file is ready, and I need this next week to work!
So please give me a clue..
Thanks

Office 2010
 
dilettante is correct. post in the VBScript forum, and also remove the "on error resume next", and post what the actual error message is (if there is one). "on error resume next" will just suppress runtime errors and continue, which does not help you
 
Ok thanks, posted in the correct Forum.

Office 2010
 
Just as a matter of interest, this code is a good example of one of the subtle differences (rather than the better known gross differences) between VBScript and VB

In VB the following line is an error:

sCSVFile = sCSVFileLocation&"test.csv"

whilst it is not an error in VBScript. I leave it as an exercise to the reader to figure out why.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top