Remove all inactive User from groups and role
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-25-2023 05:59 AM
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();
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-25-2023 06:07 AM
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())
{
.
.
.
.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-25-2023 10:03 AM
@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!!