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

Creating User Accounts in a subOU with VBScript

Okay, this is a stupid question. I have this VBScript to create user accounts and in it is a line that reads "strOU = "OU=Disabled User Accounts" ' Note the comma". I need to edit this line so that I can have the accounts created in a subOU. The way it's written now it will only find a top-level OU. For example, let's say I have an OU structure such that I have an OU named OU3 that's in OU2 which is in OU1 (OU1 is a top-level OU). I need for the accounts to be created in OU3. How do I edit that one line so that the accounts are in OU3? I thought I would just list the OUs in reverse order like this:

strOU = "OU=OU3,OU=OU2,OU=OU1"

I thought that's how you would edit that line but it's not working. Please help. Thanks.

Option Explicit
Dim objRootLDAP, objContainer, objUser, objShell
Dim objExcel, objSpread, intRow
Dim strUser, strOU, strSheet, strUPN, strDispName
Dim strCN, strSam, strFirst, strLast, strPWD, strDesc
 
strOU = "OU=UserAccounts" ' Note the comma
strSheet = "c:\useraccounts.xls"
 
' 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
' Trim function eliminates blank spaces
Do Until objExcel.Cells(intRow,1).Value = ""
   strSam = Trim(objExcel.Cells(intRow, 1).Value)
   strCN = Trim(objExcel.Cells(intRow, 2).Value) 
   strLast = Trim(objExcel.Cells(intRow, 3).Value)
   strFirst = Trim(objExcel.Cells(intRow, 4).Value)
   strPWD = Trim(objExcel.Cells(intRow, 5).Value)
   strUPN = Trim(objExcel.Cells(intRow, 6).Value)
   strDispName = Trim(objExcel.Cells(intRow, 7).Value)
   strDesc = Trim(objExcel.Cells(intRow, 8).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.userPrincipalName = strUPN
   objUser.displayName = strDispName
   objUser.description = strDesc
   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

Open in new window

0
mcpp661
Asked:
mcpp661
1 Solution
 
rejoinderCommented:
Try using this ...
strOU = "OU=OU3,OU=OU2,OU=OU1,"
Make sure there is a comma at the end of the string.

I think that the current script is merging the strings so that it looks like this;
LDAP://OU=OU3,OU=OU2,OU=OU1DC=domain,DC=com
As you can see, there whould be a comma between the OU1 and DC=

Give that a try and let me know if that works for you.
0
 
mcpp661Author Commented:
Thanks man, I know better than that. It's always something small that ends up missing.
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.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

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