Windows Scripting
  Home arrow Windows Scripting arrow Page 2 - Automate Creation of OU Structure
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
WINDOWS SCRIPTING

Automate Creation of OU Structure
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 3
    2005-07-14

    Table of Contents:
  • Automate Creation of OU Structure
  • The Code
  • Running the Hack

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Automate Creation of OU Structure - The Code


    (Page 2 of 3 )

    The following VBScript is a Windows script ( *.wsf) file, a text document that contains Extensible Markup Language (XML) code. Using a text editor such as Notepad (with Word Wrap turned off) type the following code and save it as CreateOU.wsf:

    <?xml version="1.0"?>
    <job id="CreateOUs">
    <script language="VBscript">
    <![CDATA[

    '**************************************************** '*** The script creates OU structure underneath top level OU
    '*** Second level: Accounts and Resources
    '*** Third level:
    '*** Accounts children OUs - Users, ServiceAccounts, Groups, Admins
    '*** Resources children OUs - Workstations, Servers
    '***
    '*** To execute, run cscript.exe //nologo CreateOUs.wsf OUName
    '*** where OUName is the name of the top level OU
    Option Explicit

    Dim strOU1 'the first level OU
    Dim strOU2 'the second level OU
    Dim strOU3 'the third level OU
    Dim arrOUTier2 'array of the second level OUs
    Dim arrOUTier3a 'first array of the third level OUs
    Dim arrOUTier3b 'second array of the third level OUs

    Dim strDomainDN 'name of the domain
    Dim strADsPath 'ADsPath of the first level OU
    Dim strADsSubPath 'ADsPath of the second level OU
    Dim adsRootDSE 'aDSRootDSE object
    Dim adsContainer, adsSubContainer, adsOU
    'variables representing AD container objects

    '**************************************************** '*** Connect to the current domain

    Set adsRootDSE = GetObject("LDAP://rootDSE")
    strDomainDN = adsRootDSE.Get("defaultNamingContext")

    '**************************************************** '*** Connect to the top level OU

    strOU1 = WScript.Arguments(0)
    strADsPath = "LDAP://OU=" & strOU1 & "," & strDomainDN
    Set adsContainer = GetObject(strADsPath)

    On Error Resume Next

    arrOUTier2 = Array("Accounts", "Resources")
    arrOUTier3a = Array("Users", "ServiceAccounts", "Groups", "Admins")
    arrOUTier3b = Array("Workstations", "Servers")

    '**************************************************** '*** Populate the OU structure

    For Each strOU2 in arrOUTier2

    Set adsOU = adsContainer.Create("OrganizationalUnit", "OU=" & strOU2)
    adsOU.SetInfo
    If ErrCheck(Err, strOU2) <> 2 Then

    strADsSubPath = "LDAP://OU=" & strOU2 & ",OU=" & strOU1 & "," & strDomainDN
    Set adsSubContainer = GetObject(strADsSubPath)

    Select Case strOU2
    Case "Accounts"

    For Each strOU3 in arrOUTier3a
    Set adsOU = adsSubContainer.Create("OrganizationalUnit", "OU=" & strOU3)
    adsOU.SetInfo
    Call ErrCheck(Err, strOU3)
    Next
    Case "Resources"
    For Each strOU3 in arrOUTier3b
    Set adsOU = adsSubContainer.Create("OrganizationalUnit", "OU=" & strOU3)
    adsOU.SetInfo
    Call ErrCheck(Err, strOU3)
    Next
    End Select

    End If

    Next

    On Error GoTo 0

    Set adsOU = Nothing
    Set adsContainer = Nothing

    '**************************************************** '*** Error checking function

    Function ErrCheck(objErr, strObj)

    If objErr.Number <> 0 Then
    'if the object already exists
    If objErr.Number = &H80071392 Then
    WScript.Echo "The OU " & strObj & " already exists"
    ErrCheck = 1
    Else
    WScript.Echo "Unexpected error " & objErr.Description
    ErrCheck = 2
    End If

    Else

    ErrCheck = 0

    End If

    objErr.Clear

    End Function

    ]]>
    <script>
    <job>

     

    More Windows Scripting Articles
    More By O'Reilly Media


     

    Buy this book now. This article is excerpted from Windows Server Hacks written by Mitch Tullock (O'Reilly, 2004; ISBN: 0596006470). Check it out at your favorite bookstore. Buy this book now.

    WINDOWS SCRIPTING ARTICLES

    - Introducing Two-Way Data Binding using Silve...
    - Silverlight 2.0 Application Development with...
    - Burning Multisession CDs with IMAPI2 in WSH
    - Creating a Silverlight 2.0 Application that ...
    - Burning CDs with the IMAPI2 Control
    - Burning CDs in Windows XP with WSH
    - Advanced Word Object Scripting
    - Reading and Printing Word Documents in WSH
    - Scripting Microsoft Word
    - Using WSH to Catalog MP3 Files
    - Reading MP3 ID3 Tags in WSH
    - A Brief Look at Menus in WPF
    - More Examples of Simplified Image Processing...
    - Completing a WPF To-Do List Application
    - Simplified Image Processing in GDI+





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
    Stay green...Green IT