Import nested OrganizationalUnit from LDAP as tree with thier Object ID

I am making a program with Delphi 2007 to import Active Directory users along with their OrganizationalUnit .
When I import a user, I need to get his OrganizationalUnit and then build tree for OrganizationalUnit and I need teh ogjectGUID for each OrganizationalUnit  as well.

User "Kifah Najem" exists under OU Staff\Finance\A/C Payable as bellow distinguishedName
"CN=Kifah Najem,OU=A/C Payable,OU=Finance,OU=Staff,DC=kifah,DC=com,DC=sa"

So the result for OU should be as following

-Staff (OgjectGUID)
----Finance (OgjectGUID)
--------A/C Payable (OgjectGUID)

so this is a nested tree as it is in the Active directory, but for every entry I have the ObjectGUID.

I can run the query and get the distinguishedName, as well I can parse the distinguishedName and get all OUs from the distinguishedName, but how to get the ObjectGUID for every OU
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Will SzymkowskiSenior Solution ArchitectCommented:
The easiest way to accomplish this is using the Get-ADOrganizationalUnit cmdlet.

Import-module activedirectory
Get-ADOrganizationalUnit -Filter * | select name, ObjectGuid

Open in new window

This will get you the GUID for all of your Organizational Units in Active Directory.


Open in new window

infokifahAuthor Commented:
I am using Delphi, and ADO...
So I am using SQL query like

select distinguishedName, ObjectGUID, name
from 'LDAP://, dc=com, dc=sa'
where objectclass='OrganizationalUnit'
infokifahAuthor Commented:
I found the solution.

I have to parse the distinguishedName field, and extraxt all OU values and build the tree

Experts Exchange Solution brought to you by ConnectWise

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Geert GOracle dbaCommented:
have you fixed the workaround for 1000 entries ?

if you haven't ... then you'll only get 1000 users ... or ou's

the latter will probably not be a problem :)
infokifahAuthor Commented:
The answers was not correct at all, since I was using SQL query, and the solution submitted uses windows tools or some other ready made tool
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.