Script to remove all members of a specific group.

northfdl54937
Tera Contributor

Hello,

I have a group called 'test_impersonator'.  After doing upgrades, patches, builds, i put multiple members in this group just to allow them to test as other users.

 

Once all testing is complete, i would like a script that i could run, that would automagically show me all the members of the group, and then delete all those members from the group.

 

I appreciate the assistance on the way this could be done!

1 ACCEPTED SOLUTION

Deepak Shaerma
Kilo Sage

Hi @northfdl54937 

you will also directly remove the users from a group through Background - Scripts

var groupSysId = '0a52d3dcd7011200f2d224837e6103f2'; 

// Query the Group Members [sys_user_grmember] table to find members of the specified group
var grMember = new GlideRecord('sys_user_grmember');
grMember.addQuery('group', groupSysId);
grMember.query();

while (grMember.next()) {
    // Log the user’s Sys ID being removed - for audit purposes
    gs.info('Removing user with Sys ID: ' + grMember.user + ' from group: ' + groupSysId);
  
    // Delete the group member record
    grMember.deleteRecord();
}

gs.info('All members have been removed from the group with Sys ID: ' + groupSysId);

 

Note: Note: Please Mark this Helpful and Accepted Solution. If this Helps you to understand. This will help us a lot.
Thanks & Regards 
Deepak Sharma

View solution in original post

5 REPLIES 5

Ramesh_Naidu
Mega Guru

Hi @northfdl54937,

 

You can create a fix script and run it once the testing is done.

 

The below code will show you all the members of the group:

var grp_member_gr = new GlideRecord("sys_user_grmember");
grp_member_gr.addQuery("group","<sys id of the group that you have>");
grp_member_gr.query();
while(grp_member_gr.next())
{
	gs.info(grp_member_gr.user.name);
}

 

Later you can run another script that will delete all the users from the group:

var grp_member_gr = new GlideRecord("sys_user_grmember");
grp_member_gr.addQuery("group","<sys id of the group that you have>");
grp_member_gr.query();
grp_member_gr.deleteMultiple();

 

Thankyou.

That is simply Awesome, @Ramesh_Naidu !!   

Would there be a way to combine these?  to have the first one run, pause to show the users, then have the second one run?  Maybe a function that does both?

Hi @northfdl54937,

 

My suggestion would be to view the users on the Group by looking at the Group record in the table. Later you can run the 2nd script to delete the users.  As the 1st script just log the users in the logs table.

 

Thankyou.

Deepak Shaerma
Kilo Sage

Hi @northfdl54937 

you will also directly remove the users from a group through Background - Scripts

var groupSysId = '0a52d3dcd7011200f2d224837e6103f2'; 

// Query the Group Members [sys_user_grmember] table to find members of the specified group
var grMember = new GlideRecord('sys_user_grmember');
grMember.addQuery('group', groupSysId);
grMember.query();

while (grMember.next()) {
    // Log the user’s Sys ID being removed - for audit purposes
    gs.info('Removing user with Sys ID: ' + grMember.user + ' from group: ' + groupSysId);
  
    // Delete the group member record
    grMember.deleteRecord();
}

gs.info('All members have been removed from the group with Sys ID: ' + groupSysId);

 

Note: Note: Please Mark this Helpful and Accepted Solution. If this Helps you to understand. This will help us a lot.
Thanks & Regards 
Deepak Sharma