vbs Script to create users from excel

I have a script that I've used before to add users from an excel spreadsheet. I'm trying to add more LDAP fields to the script but it's failing. I think I may be using the wrong syntax. The code errors on Line 36 Char 4 "Variable is undefined: "strdisplay" is not defined.
' UserSpreadsheet .vbs
' VBScript to create User accounts from a spreadsheet
' RE: http://computerperformance.co.uk/
' Version 1.0 - April 2009
' ------------------------------------------------------' 
Option Explicit
Dim objRootLDAP, objContainer, objUser, objShell
Dim objExcel, objSpread, intRow
Dim strUser, strOU, strSheet
Dim strCN, strSam, strFirst, strLast, strPWD
 
' -------------------------------------------------------------'
' Important change OU= and strSheet to reflect your domain
' -------------------------------------------------------------'
 
strOU = "OU Path ," ' Note the comma
strSheet = "Excel Path"
 
' Bind to Active Directory, Users container.
Set objRootLDAP = GetObject("LDAP://rootDSE")
Set objContainer = GetObject("LDAP://" & strOU & _
objRootLDAP.Get("defaultNamingContext")) 
 
' Open the Excel spreadsheet
Set objExcel = CreateObject("Excel.Application")
Set objSpread = objExcel.Workbooks.Open(strSheet)
intRow = 3 'Row 1 often contains headings
 
' Here is the 'DO...Loop' that cycles through the cells
' Note intRow, x must correspond to the column in strSheet
Do Until objExcel.Cells(intRow,1).Value = ""
   strSam = Trim(objExcel.Cells(intRow, 1).Value)
   strCN = Trim(objExcel.Cells(intRow, 2).Value) 
   strFirst = Trim(objExcel.Cells(intRow, 3).Value)
   strLast = Trim(objExcel.Cells(intRow, 4).Value)
   strdisplay = Trim(objExcel.Cells(intRow, 6).Value)
   strUPN = Trim(objExcel.Cells(intRow, 7).Value)    
   strCtry = Trim(objExcel.Cells(intRow, 8).Value)
   strcom = Trim(objExcel.Cells(intRow, 9).Value)
   strdep = Trim(objExcel.Cells(intRow, 10).Value)
   strtel = Trim(objExcel.Cells(intRow, 11).Value) 
   strtil = Trim(objExcel.Cells(intRow, 12).Value)
   strPWD = Trim(objExcel.Cells(intRow, 5).Value)
 
   ' Build the actual User from data in strSheet.
   Set objUser = objContainer.Create("User", "cn=" & strCN)
   objUser.sAMAccountName = strSam
   objUser.givenName = strFirst
   objUser.sn = strLast
   objuser.displayName = strdisplay
   objUser.userPrincipleName = strUPN
   objUser.c = strCtry
   objUser.company = strcom
   objUser.department = strdep
   objUser.telephoneNumber = strtel
   obluser.title = strtil
   objUser.SetInfo
 
   ' Separate section to enable account with its password
   objUser.userAccountControl = 512
   objUser.pwdLastSet = 0
   objUser.SetPassword strPWD
   objUser.SetInfo
 
intRow = intRow + 1
Loop
objExcel.Quit 
 
WScript.Quit 
 
' End of free example UserSpreadsheet VBScript.

Open in new window

leche671Asked:
Who is Participating?
 
yehudahaConnect With a Mentor Commented:
change line 56

obluser.title = strtil

it's need to be

objuser.title = strtil
0
 
Ken FayalCTOCommented:
You don't have the variable dim'd out in lines 7-11.  You would need to add strdisplay to the dims as I've shown below.
Dim strCN, strSam, strFirst, strLast, strPWD, strdisplay

Open in new window

0
 
yehudahaCommented:
try to remove line 6
0
Improved Protection from Phishing Attacks

WatchGuard DNSWatch reduces malware infections by detecting and blocking malicious DNS requests, improving your ability to protect employees from phishing attacks. Learn more about our newest service included in Total Security Suite today!

 
Ken FayalCTOCommented:
You will also have the same problems with lines 39 - 42.
0
 
leche671Author Commented:
I added the DIM fields but now I get an error saying that the Object doesn't support this property or method: "userPrincipleName" Can this just not be scripted?
0
 
Ken FayalCTOCommented:
No, I do it all the time.  You just have it spelled wrong.  It should be userPrincipalName, not userPrincipleName
0
 
leche671Author Commented:
Ok I think we're real close. I know get an error on line 57 char 4 "a constraint violation occured" I don't know what this command does. It was just in the original script so I didn't take it out.
0
 
leche671Author Commented:
excuse me not line 54. it errors on line 57 "objUser.SetInfo"
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.