Code Script .co.uk

Home | Scripts | Tutorials | Disclaimer | Sitemap | Contact

   Create Ou Structure
 

Vbscript / Active Directory / Create Ou Structure

A script to create an OU structure based on a file containing tab separated values (TSV). This script discovers the domain name based on the user account running the script and is limited to a three level OU structure.

Example input file (save as OUStructure.txt in the script location):

Root      Root      Finance      Finance Objects 
Root      Finance      Users      Finance Users   
Root      Finance      Laptops      Finance Laptops 
Root      Finance      Desktops      Finance Desktops 
Root      Finance      Printers      Finance Printers
Root      Root      Marketing      Marketing Objects  
Root      Marketing      Users      Marketing Users    
Root      Marketing      Laptops      Marketing Laptops
Root      Marketing      Desktops      Marketing Desktops
Root      Marketing      Printers      Marketing Printers
Root      Root      Administrators      Administrators Objects
Root      Administrators      Users      Administrators Users
Root      Administrators      Laptops      Administrators Dektops
Root      Administrators      Desktops      Administrators Desktops

Note: If copying the text above ensure that tab is used instead of multiple spaces


strInputFile = "OUStructure.txt"

set objFSO = Wscript.CreateObject("scripting.filesystemobject")
Set objInputFile =  objFSO.OpenTextFile(strInputFile, 1)

Set objNetwork = WScript.CreateObject("WScript.Network")
strDomain = objNetwork.UserDomain

strRoot = "DC=" & strDomain & ",DC=local"

Do Until objInputFile.AtEndOfStream


 strLine = objInputFile.readline
 strLine = replace(strLine,chr(34),"")
 strLine = replace(strLine,",",chr(92) & ",")


 arrLine = split(strLine,vbTab)

 If (arrLine(1) = "Root") then

   strLDAP = "LDAP://" & strRoot
   set objLDAP = getobject(strLDAP)
   set objOU = objLDAP.create("organizationalUnit","OU=" & arrLine(2))
   objOU.Put "Description", arrLine(3)
   objOU.Setinfo
   
  Else If (arrLine(0) = "Root") then

    strLDAP = "LDAP://OU=" & arrLine (1) & "," & strRoot
    set objLDAP = getobject(strLDAP)
    set objOU = objLDAP.create("organizationalUnit","OU=" & arrLine(2))
    objOU.Put "Description", arrLine(3)
    objOU.Setinfo
   Else  

    strLDAP = "LDAP://OU=" & arrLine(1) & ",OU=" & arrLine(0) & "," & strRoot
    set objLDAP = getobject(strLDAP)
    set objOU = objLDAP.create("organizationalUnit","OU=" & arrLine(2))
    objOU.Put "Description", arrLine(3)
    objOU.Setinfo
   End If
  End If
  
Loop

wscript.echo "Complete"





Please note that a disclaimer applies to any code on this page.
 
   Actions
  Go Back
  Bookmark
  Print Page


   Menu
 
- Links
- Reference
- Script Editors
- Tutorials
- Vbscript
     - Active Directory
          - Ad User Information Query
          - Add Machine To Domain
          - Add User To Group
          - Create Group
          - Create Ou Structure
          - Enumerate Ad Users
          - Enumerate Group Members
          - Move Users And Groups Between Ous
          - Raise Domain Functional Level
     - Exchange
     - Files And Folders
     - General
     - Ms Office
     - Operating System
     - Processes And Services
     - Text Processing
     - User Interaction
     - Web Servers