VMware Cloud Community
deanvictrix
Enthusiast
Enthusiast
Jump to solution

Search for Child OUs

Hello,

Was wondering if a fresh set of eyes could help me out please. I am trying to search for the child OUs of a parent OU. I am getting the results but I am unable to filter the attributes I need:

var parentOU = "Departments"; (this would be an input but I am hard settings this for testing purposes)
var ous = ActiveDirectory.search('OrganizationalUnit',parentOU);

var arrchildOUs = [];

for each (ou in ous){
arrchildOUs.push(ou.organizationalUnits);
}
System.log (arrchildOUs)

The results I get are as follows:

[2017-09-27 16:27:03.603] [I] DynamicWrapper (Instance) : [AD_OrganizationalUnit]-[class ch.dunes.ad.object.OU] -- VALUE : #_v2_#,#OU#,#257bfca1-a439-44b7-a9a2-c5a4b593fdb7#,#OU=Finance,OU=Departments,OU=vRA Self Service,DC=demo,DC=co,DC=uk#,

DynamicWrapper (Instance) : [AD_OrganizationalUnit]-[class ch.dunes.ad.object.OU] -- VALUE : #_v2_#,#OU#,#257bfca1-a439-44b7-a9a2-c5a4b593fdb7#,#OU=Sales,OU=Departments,OU=vRA Self Service,DC=demo,DC=co,DC=uk#,

DynamicWrapper (Instance) : [AD_OrganizationalUnit]-[class ch.dunes.ad.object.OU] -- VALUE : #_v2_#,#OU#,#257bfca1-a439-44b7-a9a2-c5a4b593fdb7#,#OU=Support,OU=Departments,OU=vRA Self Service,DC=demo,DC=co,DC=uk#

I was hoping to just return the OU names. Any advice greatly appreciated.

Tags (1)
1 Solution

Accepted Solutions
Hejahida82
VMware Employee
VMware Employee
Jump to solution

Hi deanvictrix . You are nearly there.

The ActiveDirectory.search function returns a set of objects of type AD_OrganizationalUnit, and you are then capturing all sub OUs into a new array arrchildOUs. The method ou.organizationalUnits returns an array of AD_OrganizationalUnit objects though, so your variable arrchildOus is actually an array where each entry is itself an array of AD_OrganizationalUnit objects.

So I'm assuming that your initial search will only ever return one OU, if not then you need to include a stage to verify the OU you are running this on is the correct one first. I'm guessing you are searching for a particular OU and only want the names of the child OUs for that one particular parent OU though, so amending your code to the following:

var parentOU = "Departments";
var ous = ActiveDirectory.search('OrganizationalUnit',parentOU);

for each (ou in ous){
var childOUs = ou.organizationalUnits;
}

Will give you an array of child OU objects stored in the variable childOUs. You can now loop through these and get the name of each OU like so:

for each (var ouObject in childOUs){

     System.log("OU Name: " + ouObject.name);

}

Let me know if you need any more help with this.

View solution in original post

4 Replies
Hejahida82
VMware Employee
VMware Employee
Jump to solution

Hi deanvictrix . You are nearly there.

The ActiveDirectory.search function returns a set of objects of type AD_OrganizationalUnit, and you are then capturing all sub OUs into a new array arrchildOUs. The method ou.organizationalUnits returns an array of AD_OrganizationalUnit objects though, so your variable arrchildOus is actually an array where each entry is itself an array of AD_OrganizationalUnit objects.

So I'm assuming that your initial search will only ever return one OU, if not then you need to include a stage to verify the OU you are running this on is the correct one first. I'm guessing you are searching for a particular OU and only want the names of the child OUs for that one particular parent OU though, so amending your code to the following:

var parentOU = "Departments";
var ous = ActiveDirectory.search('OrganizationalUnit',parentOU);

for each (ou in ous){
var childOUs = ou.organizationalUnits;
}

Will give you an array of child OU objects stored in the variable childOUs. You can now loop through these and get the name of each OU like so:

for each (var ouObject in childOUs){

     System.log("OU Name: " + ouObject.name);

}

Let me know if you need any more help with this.

deanvictrix
Enthusiast
Enthusiast
Jump to solution

Perfect works a treat, thanks very much for this

0 Kudos
Hejahida82
VMware Employee
VMware Employee
Jump to solution

You're welcome, glad I could help.

BrettK1
Enthusiast
Enthusiast
Jump to solution

This is old, but still relevant!

As a non-javascript writer (and new to vRO), I'm wondering how best to do this 'recursively', and better yet, 'recursively, but only returning endpoints'?
I thought it might be simple, but so far no.  Caveat, some end points have the same name, but different paths, so .search isn't useful past the parent OU 😞

Edit: Was able to do this recursively after seeing that the function wants an array, so I had to take my individual OUs I was recursing and turn them into an array of 1 item, hah.
I don't write javascript, so it may be ugly, but got the job done (including filtering out 'non-terminal OUs by checking the organizationalUnits.length for each OU before pushing it into the return array.

Thanks again, wouldn't have made it here without the initial answer!
This 4 year old post got me started at least, and I'm returning my OUs direct children at least!

0 Kudos