Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > VB Script

Vista - Exctracting OU and parent OU name from Active Directory

Reply
 
Old 08-07-2008   #1 (permalink)
Ib Schrader


 
 

Exctracting OU and parent OU name from Active Directory

Hi there

I want to make a script that pulls out the names of all OUs and their parent
OUs to a text file. From various sources on the web, I managed to gather
enough clues to make this short script:

Filename = "parent.txt"

Const Writable = 2

Set FileObject = CreateObject("Scripting.FileSystemObject")
Set OutPutFile = FileObject.OpenTextFile(filename, Writable, True)

Set currentou = GetObject("ldap://ou=subtest, ou=test, dc=domain, dc=com")
Set ouParent = GetObject(currentou.Parent)
OutPutFile.write ouParent.ou & ";"
OutPutFile.write currentou.ou

This script creates the file "parent.txt" and writes the parent of the
subtest ou and the name of the subtest ou itself like this: test;subtest

However I need it to output this name;parent for all OUs within the test OU,
not just itself. So I gather I have to somehow loop through all OUs in the
structure and make the script return ouparent.ou and current.ou for all of
them so the file ends up something like this

test;subtest
subtest;west
subtest;east
subtest;north
north;marketing

You get the idea, I hope. However, I don't seem to be able to find out how I
loop through an OU structure. The closest I've gotten was to loop through
members of a group, but that wasn't exactly what I wanted.

Any suggestions?

Thanks in advance
Ib



My System SpecsSystem Spec
Old 08-07-2008   #2 (permalink)
Ib Schrader


 
 

Re: Exctracting OU and parent OU name from Active Directory

I made it work actually

Disregard this post


My System SpecsSystem Spec
Old 08-07-2008   #3 (permalink)
Richard Mueller [MVP]


 
 

Re: Exctracting OU and parent OU name from Active Directory


"Ib Schrader" <ibschrader@xxxxxx> wrote in message
news:%23uyBxDJ%23IHA.4816@xxxxxx
Quote:

>I made it work actually
>
> Disregard this post
>
Usually people start at the parent level (perhaps at the domain) and use a
recursive routine to enumerate the child OU's. You are starting at the
lowest level and working up to the domain (or top level OU). A recursive
routine is still the most straightforward method (to handle any level of
nesting), but you need to test the class of the parent object to determine
when you have reached the domain. For example:
=========
Option Explicit
Dim objOU

Set objOU = GetObject("ldap://ou=subtest,ou=test,dc=domain,dc=com")
Call EnumOUs(objOU)

Sub EnumOUs(objChild)
Dim objParent

Set objParent = GetObject(objChild.Parent)
If (objParent.Class = "organizationalUnit") Then
Wscript.Echo objChild.ou & ";" & objParent.ou
Call EnumOUs(objParent)
Else
Wscript.Echo objChild.ou & ";" & objParent.distinguishedName
End If
End Sub

--
Richard Mueller
MVP Directory Services
Hilltop Lab - http://www.rlmueller.net
--


My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
active directory PowerShell
Active Directory Vista General
Active Directory Vista networking & sharing
Active Directory PowerShell
Get parent directory PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46