Script to Initialize and add multiple AD groups

Kaz9
Giga Expert

Hello community!

      I am trying to get a script working that queries a variable on my form and adds those string values as ad groups to the username requested by the parent workflow.

Here is my script

var gr = new GlideRecord('x_prole_onboarding_onboarding');

gr.addQuery('u_string_105', current.variables.position); //job title field

gr.query();

var groupList = "";

var group = []; // this will be an array of objects

while (gr.next()) {

groupList = gr.u_sharepoint_groups;

group = groupList.split(',');

}

workflow.scratchpad.count = group.length; // the overall count available

workflow.scratchpad.groupList = groupList; // the list of AD groups

workflow.scratchpad.group = group;

workflow.scratchpad.counter = 0;   // our counter

workflow.scratchpad.host = '10.100.33.100';

workflow.scratchpad.user = current.variables.username;

Here is a picture of the Activity Properties to add user to group

addadgroups.PNG

here is a picture of the variable it is trying to pull information from

picture of group for array.PNG

I am getting over the right server IP and the right username but I cannot get it to use that list as AD groups to add in the activity. Any help would be great! please let me know if you need additional information. Thank you!

1 ACCEPTED SOLUTION

Kaz9
Giga Expert

workflow.PNG



the Run Script gets all of the information from my onboarding from as far as the groups go and adds them to an array which runs through the add user to group function. Here is the script for the Run Script.





var gr = new GlideRecord('x_prole_emp_onb_employee_onboarding'); //set to the name of the table where the form is located


gr.addQuery('u_job_title', current.variables.position.getDisplayValue()); //add a sort(query) value for the job title


gr.addQuery('u_list_location', current.variables.location); //add a sort (query) value for the location


gr.query(); //run query



var groupList = "";


var group = []; // this will be an array of objects



while (gr.next()) {


groupList = gr.u_distribution_lists + ',' + gr.u_printers + ',' + gr.u_sharepoint_groups + ',' + gr.u_proxy; //these are the variables on the form I want to get a list from. I ended up creating my variables all in lists.


group = groupList.split(',');


}



workflow.scratchpad.count = group.length; // the overall count available


workflow.scratchpad.groupList = groupList; // the list of AD groups


workflow.scratchpad.group = group;


workflow.scratchpad.counter = 0;   // our counter


workflow.scratchpad.host = '10.100.33.100';


workflow.scratchpad.user = current.variables.username;








This is my add user to ad group activity


addusertoadgroupactivity.PNG


View solution in original post

18 REPLIES 18

drew25
Tera Contributor

This was extremely helpful.  I was able to recreate a similar workflow where we want our users to be able to subscribe to different AD distribution groups through a catalog item form.

Drew,

 

Can you please share the code for Run Script & If condition for adding multiple users to same group in the above subscription workflow?

Hey Sangita, I'll post the code below for each block, but my code is only for adding one user to multiple groups.  I could not make it so multiple users were added to multiple groups.

 Catalog Item:

On my Catalog Item I have a hidden string field and an onSubmit client script that adds all of the groups in the checkboxes on the form to that hidden string field in comma format (Group1,Group3,Group4).  This way I can get the user that needs the fields (in my case, it's the user that submitted the form, but you can specify the user on the form as a reference field).

 Workflow

Initialize Workflow Variables:

What this does is it takes the user that opened the request and the hidden field (add_groups) and splits the groups into an array.  (I'm just going to show the Add Groups code since the Remove groups code is exactly the same.)  Then I count the amount of groups in the array and save that number to a variable so I know how many times to loop through the add groups block.  I also set a counter so I know when to stop looping.

//Domain Controller (Insert your actual DC)
workflow.scratchpad.host = '10.x.x.x;

//User Being Modified
//workflow.scratchpad.user = "test.orchestration";
workflow.scratchpad.user = current.request.opened_by.user_name;


//Add Group Variables
if(current.variables.add_groups != ""){
workflow.scratchpad.addgroups = current.variables.add_groups.toString();
workflow.scratchpad.addgroup = workflow.scratchpad.addgroups.split(",");
workflow.scratchpad.addcount = workflow.scratchpad.addgroup.length; // the overall count available
workflow.scratchpad.addcounter = 0;   // our counter
gs.log("Add Group List: " + workflow.scratchpad.addgroups + " | User: " + workflow.scratchpad.user);
gs.log("Add Group Length: " + workflow.scratchpad.addcount + " | User: " + workflow.scratchpad.user);
}

If Adding User to Groups:

answer = ifScript();

function ifScript() {
   if (workflow.scratchpad.addcount >= 1) {
      return 'yes';
   }
   return 'no';
}

 

Add User to Group:

Domain Controller: ${workflow.scratchpad.host}
User name: ${workflow.scratchpad.user}
Group name: ${workflow.scratchpad.addgroup[workflow.scratchpad.addcounter]}

 

Check Add Group Counter:

This counter checks to see if we are done adding groups.  If we are then finish.

answer = ifScript();  
  
function ifScript() {  
   var check_add = 'no';  
   workflow.scratchpad.addcounter ++;  // increment the counter  
          
   // check the counter and see if we need to go again or stop  
   if (workflow.scratchpad.addcounter < workflow.scratchpad.addcount) {  
      check_add = 'yes';  
   }  
          
   return check_add;  
}  

  

I know it's a lot, but let me know if you have any questions.  The screenshot attachment in my previous comment should help match the workflow blocks to the scripts above.

Drew

Hi Drew,

 

Thanks for your quick response. I used your code to add users(as string to test first). I was able to add one user and then the "Add user to Group" failed and said "Object Already exist". It seems to not go to the "if" condition to increment counter. Did you encounter this problem?

 

My String for username

workflow.scratchpad.addgroups ="TMiller,KGayson";

 

Thanks

Oh, I'm doing it in reverse so what I have is the following

workflow.scratchpad.user = "TMiller";

workflow.scratchpad.addgroups = "Group1,Group2";

My workflow will add TMiller to Group1 and then loop through and add TMiller to Group2 and then complete.  Unfortunately, I did not setup my workflow to do multiple users, but I'm sure that it could be done, you would just need to add another if condition and loop back to the if condition and set the workflow.scratchpad.user variable to another user and loop through the groups again.