VMware Cloud Community
jlonsdale
Contributor
Contributor
Jump to solution

Adding one user group to another, taking strings as arguments

For something that should be very straight forward, this has had me tearing my hair out for hours now...

We're currently running version 2.0.3 of the Active Directory Plugin.

I found the included workflow "Add Groups to group members".

Upon opening it, I found the following line;

userGroup.addElements(groups);

Easy as pie I thought.  Takes an AD:UserGroup Object, along with an array of AD:UserGroup objects.

So, all I need to do is create a scriptable task that takes in a string and retrieves the associated Active Directory Object...

// SourceGroup is the included group

var srcgrp = ActiveDirectory.search("UserGroup",sourceGroup);

// Target Group (The one we're going to add the groups too)

var taggrp = ActiveDirectory.search("UserGroup",targetGroup);

But here's where the wheels come off...

The search method appears to return an array of "AD_UserGroup" type objects.

For the life of me, I CAN NOT figure out how to convert AD_UserGroup objects into the AD:UserGroup objects that the "Add Groups to group members" workflow requires.

Any help or advice on this would be hugely appreciated!

0 Kudos
1 Solution

Accepted Solutions
SeanKohler
Expert
Expert
Jump to solution

Well... maybe this will help as a primer...

// Get a one item array... it is a UserGroup Object (but in an array of 1)
var srcgrps = ActiveDirectory.search("UserGroup","GrpIG_smkLabTest-MGR"); //Use an S at the end for plural arrays - good practice
System.log("Warning... search returns an array always... even if there is only one.");
for each (srcgrp in srcgrps){
System.log(srcgrp.getAttribute("cn"));
}

// Get a two item array... they are UserGroup Objects (but in an array of 2)
var taggrps = ActiveDirectory.search("UserGroup","GrpIG_smkLabTest"); //Use an S at the end for plural arrays - good practice
System.log("Warning... search returns an array always... even if there is only one.");
for each (taggrp in taggrps){
System.log(taggrp.getAttribute("cn"));
  if (taggrp.getAttribute("cn") == "GrpIG_smkLabTest") {
   System.log("Setting the one I actually want from the array: " + taggrp.getAttribute("cn"));
   var singleTargetGroup = taggrp;
  }
}

System.log ("Do I have the right group?"); // should be "GrpIG_smkLabTest" in my Lab
System.log (singleTargetGroup.getAttribute("cn")); // yeppers

// ONE user group has a method for addElements that takes an ARRAY of UserGroup and/or User objects
singleTargetGroup.addElements(srcgrps);

System.log("Warning... groupMembers returns an array always... even if there is only one.");
singleTargetGroupMembers = singleTargetGroup.groupMembers; //Use an S at the end for plural arrays - good practice

//Loop through all the members of the UserGroup object and syslog the name
for each (singleTargetGroupMember in singleTargetGroupMembers){
System.log(singleTargetGroupMember.getAttribute("cn"));
}

ADUserGroup.jpg

View solution in original post

0 Kudos
3 Replies
SeanKohler
Expert
Expert
Jump to solution

And you are doing...

taggrp[0].addElements(srcgrp);

???

0 Kudos
SeanKohler
Expert
Expert
Jump to solution

Well... maybe this will help as a primer...

// Get a one item array... it is a UserGroup Object (but in an array of 1)
var srcgrps = ActiveDirectory.search("UserGroup","GrpIG_smkLabTest-MGR"); //Use an S at the end for plural arrays - good practice
System.log("Warning... search returns an array always... even if there is only one.");
for each (srcgrp in srcgrps){
System.log(srcgrp.getAttribute("cn"));
}

// Get a two item array... they are UserGroup Objects (but in an array of 2)
var taggrps = ActiveDirectory.search("UserGroup","GrpIG_smkLabTest"); //Use an S at the end for plural arrays - good practice
System.log("Warning... search returns an array always... even if there is only one.");
for each (taggrp in taggrps){
System.log(taggrp.getAttribute("cn"));
  if (taggrp.getAttribute("cn") == "GrpIG_smkLabTest") {
   System.log("Setting the one I actually want from the array: " + taggrp.getAttribute("cn"));
   var singleTargetGroup = taggrp;
  }
}

System.log ("Do I have the right group?"); // should be "GrpIG_smkLabTest" in my Lab
System.log (singleTargetGroup.getAttribute("cn")); // yeppers

// ONE user group has a method for addElements that takes an ARRAY of UserGroup and/or User objects
singleTargetGroup.addElements(srcgrps);

System.log("Warning... groupMembers returns an array always... even if there is only one.");
singleTargetGroupMembers = singleTargetGroup.groupMembers; //Use an S at the end for plural arrays - good practice

//Loop through all the members of the UserGroup object and syslog the name
for each (singleTargetGroupMember in singleTargetGroupMembers){
System.log(singleTargetGroupMember.getAttribute("cn"));
}

ADUserGroup.jpg

0 Kudos
jlonsdale
Contributor
Contributor
Jump to solution

Thank you for the excellent example code.

This resolved the issue nicely.

0 Kudos