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 Mike Lewis 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 AD from csv in OU by 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----------
I get errorcode 0x80005000 (line with Set 0Container)
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
 
You are assigning values to sOU1 and sOU2 inside the loop, but the "Set oContainer = ..." line is before the loop starts, so sOU1 and sOU2 are empty.

Try moving the "Set oContainer = " to just before the "Set oNewUser = " line

Code:
sOU1 = strItems(7)
sOU2 = strItems(8)

[highlight]' ----------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")[/highlight]

' ----------Build the User account----------
Set oNewUser = oContainer.Create("User","cn="&sFirstName&" "&SLastName)
 
It's working!!
Thanks man, you saved me..:)
Cheers and Happy Eastern.

Office 2010
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top