Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

how do i determine the OU structure on the fly ?

6 views
Skip to first unread message

IT Staff

unread,
Nov 5, 2009, 9:43:48 PM11/5/09
to
Each time i script, i have to hard-code the OU. Is there a method to
determine the AD OU structure on the fly ?

Let me give a scenario:

# generic way to determine ANY ORGANISATION LDAP PATH
$objOU=[ADSI]"LDAP://ROOTDSE"
$OBJOU.defaultNamingContext


Is there similar way like above to determine OU structure ?


Martin Zugec

unread,
Nov 6, 2009, 5:08:03 AM11/6/09
to

Sorry, could you be more specific??

"Determine the AD OU structure" - of which OU exactly, how? :)

Martin

IT STAFF

unread,
Nov 6, 2009, 9:36:59 AM11/6/09
to
Let me be more specific.

In company A, their OU structure could be like this:

OU Country1
OU Computers
OU Users.

In company B, their OU structure could be like this:

OU Dept1
OU Users_Computers

If i write a script for Company A, i hAve to use a regular expression to
detect Company A OU structure.
Similarly if i write a script for Company B, the regular expression used in
Company A cannot be applied to Company B. This means i have to write a
regular expression for Company B.

Is there a "generic" way to determine the OU structure of Two companies,
rather than *hard code" a regular express for Company A and Company B
respectively ?

Martin Zugec

unread,
Nov 8, 2009, 8:51:20 AM11/8/09
to

Still don't know what exactly you want to achieve - OUs are container
objects for OU, therefore you will need to do equivalent of Get-ChildItem
(which can be done) and then have some logic to determine which OU is used
for what - and this is suddenly almost impossible task :(

You could of course have a logic that will match OU based on some kind of
mask (for example returning first OU that contains work Users or Computers),
however don't think such code would be too useful.

Martin

"IT STAFF" <jkk...@hotmail.com> wrote in message
news:ep7915uX...@TK2MSFTNGP02.phx.gbl...

jj

unread,
Nov 8, 2009, 2:11:59 PM11/8/09
to
> > respectively ?- Hide quoted text -
>
> - Show quoted text -

Martin - I am definitely concerned about this question as well. You
say that you need to do the "equivalent of Get-ChildItem (which can be
done)" Yes, understood, but HOW is it done for OU structure?

jj

Martin Zugec

unread,
Nov 8, 2009, 2:53:22 PM11/8/09
to

Hi jj,

ah, now I understand the problem :) Having a look at
http://msdn.microsoft.com/en-us/library/system.directoryservices.directoryentry_properties.aspx, I
can see that DirectoryEntry class contains property Children.

Therefore you retrieve reference to any directory entry and then you can
have a look at $DirectoryEntry.Childen. If you are interested, I can give
you code how to do that.

Martin

jj

unread,
Nov 8, 2009, 4:28:44 PM11/8/09
to
On Nov 9, 8:53 am, "Martin Zugec" <martin.zu...@gmail.com> wrote:
> Hi jj,
>
> ah, now I understand the problem :) Having a look athttp://msdn.microsoft.com/en-us/library/system.directoryservices.dire..., I

> can see that DirectoryEntry class contains property Children.
>
> Therefore you retrieve reference to any directory entry and then you can
> have a look at $DirectoryEntry.Childen. If you are interested, I can give
> you code how to do that.
>
> Martin

Excellent. Thanks, Martin. So you get the top level, see if it has
children, etc??

Yeah, if you were able to post code, that would be great.

Martin Zugec

unread,
Nov 11, 2009, 10:36:40 AM11/11/09
to
Hi jj,

you can try below code (don't have AD here, so I could try):

Function AD\Get-OU ([string]$Path = $([ADSI]"LDAP://
ROOTDSE").defaultNamingContext) {
$DS = New-Object DirectoryServices.DirectorySearcher($Path,
"(objectclass=organizationalUnit)", $Null,
[DirectoryServices.SearchScope]::OneLevel)
[array]$OUs = $DS.FindAll()
ForEach ($OU in $OUs) {
$OU.GetDirectoryEntry()
}

}

It should work (or require just little bit testing).

0 new messages