[Last Call] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 337
  • 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 & _
' 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
   ' Separate section to enable account with its password
   objUser.userAccountControl = 512
   objUser.pwdLastSet = 0
   objUser.SetPassword strPWD
intRow = intRow + 1

Open in new window

1 Solution
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;
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.
mcpp661Author Commented:
Thanks man, I know better than that. It's always something small that ends up missing.

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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