• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2156
  • Last Modified:

VBScript to add new user accounts from CSV to Active Directory

Hello,

I'm trying to use a VBScript I found on the internet to add new user accounts to our Active Directory by importing from a CSV file. I keep getting the following error in the VBScript trying to connect to our Test OU which I have confirmed does exist. The error is:
Line 54, Char 1 Error 0x80005000 Code: 80005000 Source: (null).

This is line 54:
Set oContainer = GetObject("LDAP://OU=Test" & _

Open in new window


Any help to get this working would be greatly appreiciated, as I am new to scripting. Thank you.

Here is the code:
 
'*********************************************************************
' Script: createUsersFromCSV.vbs									 *
' Creates new user accounts in Active Directory from a CSV file.     *
' Input: CSV file with layout logonname,firstname,lastname,password  *
'																	 *
'*********************************************************************
 
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 sLogon
Dim sFirstName
Dim sLastName
Dim sDisplayName
Dim sPassword
Dim nPwdLastSet
Dim nUserAccountControl ' Used to enable the account
Dim sDomain
Dim sCompany
Dim sPhone
Dim sEmail
Dim sDescription
 
Dim NumChar, Count, strRdm, intRdm
Dim fso, f, fso1, f1
 
'* Modify this to match your company's AD domain
sDomain="domain.name.org"
 
'* Input file location
sCSVFileLocation = "C:\Scripts\" 'KEEP TRAILING SLASH!
 
'* Full path to input file
sCSVFile = sCSVFileLocation&"newusers.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=yes;FMT=Delimited"""
oRecordSet.open "SELECT * FROM " & sCSVFile ,oConnection
 
' Create a connection to an Active Directory OU container.
Set oRootLDAP = GetObject("LDAP://RootDSE")
Set oContainer = GetObject("LDAP://OU=Test" & _
oRootLDAP.Get("defaultNamingContext")) 

 
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
   sLogon = oRecordSet.Fields.Item(0).value
   sFirstName = oRecordSet.Fields.Item(1).value
   sLastName = oRecordSet.Fields.Item(2).value
   sDisplayName = sFirstName&" "&sLastName
   sPassword = oRecordSet.Fields.Item(3).value
 
   ' 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", 0

' Enable the user account
oNewUser.Put "userAccountControl", 512
oNewUser.SetInfo


objFile.Close

'*******************
oRecordset.MoveNext
Loop
'*******************
' Used only for debugging
'if err.number = -2147019886 then
' msgbox "User logon " & sLogon & "already exists"
'End If
' *********End of user account creation

Open in new window

0
wcgmeit
Asked:
wcgmeit
  • 6
  • 4
4 Solutions
 
GundogTrainerCommented:
& _
This means this command continues on the next line and this looks like it has an extra close bracket that shouldnt be there.

try editing it so it reads like this:

Set oContainer = GetObject("LDAP://OU=Test" & oRootLDAP.Get("defaultNamingContext")
0
 
wcgmeitAuthor Commented:
That helped get rid of the error I was getting, but now I'm getting this:

Line 54 char 1 Error: An operations error occured, Code: 80072020 Source: (null)

Set oContainer = GetObject("LDAP://OU=Test") & oRootLDAP.Get("defaultNamingContext")

Open in new window


I looked up the error and a possible cause would b needing admin priviledges but I'm running this script as the domain administrator.....

thank you for your help so far!
0
 
GundogTrainerCommented:
if you can add the line just before line 54

My brains not in gear at the moment - the reason there was a extra close bracket on the second line before was because there shouldnt have been on on the first line - the Distinguised name of the OU should be somthing like: OU=Test,DC=domain,DC=com depending on what your domain is called.

Can you try this:
Set oContainer = GetObject("LDAP://OU=Test,"& oRootLDAP.Get("defaultNamingContext"))
and if you still get an error please add this just before the line and see if it looks OK.

wscript.echo "LDAP://OU=Test,"& oRootLDAP.Get("defaultNamingContext"
0
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

 
GundogTrainerCommented:
Looking at it again I think it was just missing a comma in your original listing ! (after test)
0
 
wcgmeitAuthor Commented:
I ran your changes with the wscript.echo.

the line containing the echo runs fine and displays the ou=test,dc=domina,dc=name,dc=org

however, the same line right after is failing with this error:

There is no such object on the server Code 80072030

I know the OU Tests exists, I can see it and I can manually create a new user within it in Active Directory. Thanks again!!
0
 
GundogTrainerCommented:
If your sure that the test OU is in the root of your active directory and not under another OU such as Users or groups then I dont know why its not finding it.

I would use the microsoft tool LDP.exe to browse your active directory with LDAP to confirm you can see it etc - it also usefull to see the other properties and attributes of the objects if you want to customise your script etc.

Please see:http://technet.microsoft.com/en-us/library/cc772839(WS.10).aspx?ppud=4
for details about LDP and a link to the support tools to install it.
http://go.microsoft.com/fwlink/?LinkId=100114
0
 
wcgmeitAuthor Commented:
Ok I so I went and used the LDP.exe tool, great tool by the way thank you so much. I noticed in our AD (I didn't originally set it up) that Test is an OU of a root OU named OU. Basically:

AD root/domain>organizational unit named OU>organizational unit named Test.

I tried this for the code but it gave the same error about not finding it:

Set oContainer = GetObject("LDAP://OU=OU,OU=Test,"& oRootLDAP.Get("defaultNamingContext"))

Open in new window

0
 
wcgmeitAuthor Commented:
I figured it out. The child OU must come before the parent OU. The Code should be :

Set oContainer = GetObject("LDAP://OU=Test,OU=OU,"& oRootLDAP.Get("defaultNamingContext"))

Open in new window



Thanks so much, wouldn't have figured it out without your help!
0
 
wcgmeitAuthor Commented:
Thanks so much, great advice.
0
 
wcgmeitAuthor Commented:
I added a new variable and inputbox so that you can prompt and enter which Child OU to create the account in, its working great. If anyone wants the code, here it is attached:
'*********************************************************************
' Script: createUsersFromCSV.vbs									 *
' Creates new user accounts in Active Directory from a CSV file.     *
' Input: CSV file with layout logonname,firstname,lastname,password  *
'																	 *
'*********************************************************************
 
Option Explicit
 
Dim sCSVFileLocation
Dim sCSVFile
Dim oConnection
Dim oRecordSet
Dim oNewUser
 
' Variables needed for LDAP connection
Dim oRootLDAP
Dim oContainer
Dim strChildOU
 
' Holding variables for information import from CSV file
Dim sLogon
Dim sFirstName
Dim sLastName
Dim sDisplayName
Dim sPassword
Dim nPwdLastSet
Dim nUserAccountControl ' Used to enable the account
Dim sDomain
Dim sCompany
Dim sPhone
Dim sEmail
Dim sDescription
 
Dim NumChar, Count, strRdm, intRdm
Dim fso, f, fso1, f1
 
'* Modify this to match your company's AD domain
sDomain="medres.strpweb.org"
 
'* Input file location
sCSVFileLocation = "C:\Scripts\" 'KEEP TRAILING SLASH!
 
'* Full path to input file
sCSVFile = sCSVFileLocation&"newusers.csv"

'Child OU To Add User To
strChildOU = InputBox("Enter name of OU to create new user in:")
 
' 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=yes;FMT=Delimited"""
oRecordSet.open "SELECT * FROM " & sCSVFile ,oConnection
 
' Create a connection to an Active Directory OU container.
Set oRootLDAP = GetObject("LDAP://RootDSE")
Set oContainer = GetObject("LDAP://OU=" & strChildOU & ", OU=OU,"& oRootLDAP.Get("defaultNamingContext"))

'Set oContainer = GetObject("LDAP://OU=Test") & oRootLDAP.Get("defaultNamingContext")


 
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
   sLogon = oRecordSet.Fields.Item(0).value
   sFirstName = oRecordSet.Fields.Item(1).value
   sLastName = oRecordSet.Fields.Item(2).value
   sDisplayName = sFirstName&" "&sLastName
   sPassword = oRecordSet.Fields.Item(3).value
 
   ' 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", 0

' Enable the user account
oNewUser.Put "userAccountControl", 512
oNewUser.SetInfo


objFile.Close

'*******************
oRecordset.MoveNext
Loop

WScript.Echo "Users have been added. Script complete."

'*******************
' Used only for debugging
'if err.number = -2147019886 then
' msgbox "User logon " & sLogon & "already exists"
'End If
' *********End of user account creation

Open in new window

0
 
Anuj75Commented:
Hi

I am new to vbscript and my requirement is the same, need to read a csv file via ADODB Connection. But I am recieving error "The connection cannot be used to perform this operation. It is either closed or invalid in this context.."

Please advice
Thanks
Anuj
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now