deanvictrix
Contributor
Contributor

Search for Child OUs

Jump to solution

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

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

0 Kudos
3 Replies
Hejahida82
VMware Employee
VMware Employee

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

0 Kudos
deanvictrix
Contributor
Contributor

Perfect works a treat, thanks very much for this

0 Kudos
Hejahida82
VMware Employee
VMware Employee

You're welcome, glad I could help.