Remove all inactive User from groups and role

Mark Wood
Tera Contributor

Hello Experts,

I have written below script to remove inactive users from all roles and groups.when i pass manual sys_id of inactive my code is working properly but when i pass dynamic value its not working .please guide me.

thank you.

var gr=new GlideRecord('sys_user');
gr.addQuery('active',false);
gr.setLimit(1);
gr.query();
while(gr.next())
{
var gr1=new GlideRecord('sys_user_grmember');

gr1.addQuery('user',gr.sys_id);
gs.print(gr.sys_id);
gr1.query();
while(gr1.next())
{
gr1.deleteMultiple();
gs.print(gr1.user+"removeed successfully");

}
var gr2=new GlideRecord('sys_user_has_role');
gr2.addQuery('user',gr.sys_id);
gr2.query();
while(gr2.next())

{
gr2.deleteMultiple();

}
}

2 REPLIES 2

Sohithanjan G
Kilo Sage
Kilo Sage

Hi @Mark Wood ,

 

Use this code below to delete dynamically with help of sys_id. 

var gr=new GlideRecord('sys_user');
//gr.addQuery('active',false);
//gr.setLimit(1); 
gr.addQuery('sys_id','sys_id_of_user');  // if you want to pass dynamic sysid
gr.query();
while(gr.next())
{
.
.
.
.

 

Please mark as Accepted Solution if this solves your query and HIT Helpful if you find my answer helped you. This will help other community mates too..:)

@Mark Wood  The code in your original post seems to be perfectly fine where it queries one inactive user in the ServiceNow and removes the groups and roles that are associated to the inactive user.

 

Please tell us from where and how are you calling the script and what are the parameters that are passed the script, that will help us in suggesting the correct solution.

 

Please mark the appropriate response as correct answer and helpful

Thanks!!