Windows Vista Forums

Exctracting OU and parent OU name from Active Directory
  1. #1


    Ib Schrader Guest

    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

  2. #2


    Ib Schrader Guest

    Re: Exctracting OU and parent OU name from Active Directory

    I made it work actually

    Disregard this post



      My System SpecsSystem Spec

  3. #3


    Richard Mueller [MVP] Guest

    Re: Exctracting OU and parent OU name from Active Directory


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

    >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

Exctracting OU and parent OU name from Active Directory problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
Active Directory Dyawlak Vista security 3 24 May 2007
Active Directory Vista networking & sharing 4 19 Dec 2006
Active Directory Lothar PowerShell 7 14 Dec 2006
Get parent directory Thomas Kofler PowerShell 3 23 Nov 2006
active directory Cameron Murray PowerShell 4 16 Nov 2006