The createOU.bat script requires a list of organizational units as input. To know which Organizational Units are required, we may use a script such as the following :

Windows 2003 Commands

		Dsquery ou > ou.txt

results in a ou.txt that looks like this :

	"OU=Domain Controllers,DC=KICKS,DC=LOCAL"


This file can be used to re-create the same OU's on an other machinbe / in an other domain :

		For /L %%n in (ou.txt) do dsadd ou %%n


ADSI script

On pre-Windows 2003 operating systems, we can use ADSI to script against Active Directory. Organizational Units can be retrieved from the user distinguished names like this :

	'first, create a recordset by quering the A.D., 
	're.  getusers script 
	'then : 

	strDN = objRecordSet.Fields("distinguishedName").Value
    	arrPath = Split(strDN, ",")
    	intLength = Len(arrPath(1))
    	intNameLength = intLength - 3
	strOU = Right(arrPath(1), intNameLength)


However, when we think about reproducing the OU structure, it may make more sense to list all OU's by ther distinguished name, as this preserves the active Directory's hierarchical structure. Here's a sample script :

	Set oRootDSE = GetObject("LDAP://RootDSE")
	Set oDomain = GetObject("LDAP://" & oRootDSE.Get("DefaultNamingContext"))

	Call EnumOUs(oDomain.ADsPath)

	' -------------------------------------------------------------------------

	Sub EnumOUs(sADsPath)
		Set oContainer = GetObject(sADsPath)
		oContainer.Filter = Array("OrganizationalUnit")

		For Each oOU in oContainer
			WScript.Echo oOU.ADsPath
			'recurse : call the sub again for nested OU's

	End Sub


Koen Noens
june 7, 2005