Ureader.com  
Microsoft software help and Community
   home   |   control panel login   |   archive   |  
 
scripts
hosting
jscript
remote
scripting.wsh
scriptlets
vbscript
virus.discussion
  
 
date: Wed, 23 Jan 2008 15:36:53 -0800 (PST),    group: microsoft.public.scripting.wsh        back       


Recurse through OU's   
I have a top level OU with 4 nested OU's and would like my script to
search all of the OU's when running the script.  I've seen ADO being
suggested but that seems like overkill for what I'm trying to do.  How
can I make the following recursive (I want all OU's under site1 to be
included)?

-------------------------------------------------------------------------------------------------
' Bind to OU, using the Distinguished Name of the OU.
Set objOU = GetObject("LDAP://ou=site1,dc=company,dc=com")

' Filter on user objects.
objOU.Filter = Array("user")

' Enumerate users.
	For Each objUser In objOU
Do this and that and the other thing.....
-------------------------------------------------------------------------------------------------

Thanks,
D
date: Wed, 23 Jan 2008 15:36:53 -0800 (PST)   author:   unknown

Re: Recurse through OU's   
wrote in message 
news:54aba935-6ead-4d5f-abcc-3480a6a59154@i29g2000prf.googlegroups.com...
>I have a top level OU with 4 nested OU's and would like my script to
> search all of the OU's when running the script.  I've seen ADO being
> suggested but that seems like overkill for what I'm trying to do.  How
> can I make the following recursive (I want all OU's under site1 to be
> included)?
>
> -------------------------------------------------------------------------------------------------
> ' Bind to OU, using the Distinguished Name of the OU.
> Set objOU = GetObject("LDAP://ou=site1,dc=company,dc=com")
>
> ' Filter on user objects.
> objOU.Filter = Array("user")
>
> ' Enumerate users.
> For Each objUser In objOU
> Do this and that and the other thing.....
> -------------------------------------------------------------------------------------------------

A recursive subroutine should work:
==========
Dim objOU

' Bind to parent OU object.
Set objOU = GetObject("LDAP://ou=site1,dc=company,dc=com")

Call EnumUsers(objOU)

Sub EnumUsers(objParent)
    ' Recursive sub to enumerate users.
    Dim objChild

    ' Filter on user objects in OU.
    objParent.Filter = Array("user")

    ' Enumerate users.
    For Each objUser In objParent
        Wscript.Echo objUser.sAMAccountName
    Next

    ' Filter on child OU's.
    objParent.Filter = Array("organizationalUnit")
    ' Enumerate child OU's.
    For Each objChild In objParent
        Call EnumUsers(objChild)
    Next
End Sub

-- 
Richard Mueller
Microsoft MVP Scripting and ADSI
Hilltop Lab - http://www.rlmueller.net
--
date: Wed, 23 Jan 2008 19:25:59 -0600   author:   Richard Mueller [MVP]

Google
 
Web ureader.com


    COPYRIGHT 2007, YARDI TECHNOLOGY LIMITED, ALL RIGHT RESERVE  |   contact us