Revoke itil role from users not logged in 60 days

SN Rookie
Giga Expert

Hi All,

The requirement is to remove ITIL role from users who haven't logged in 60 days from groups (which provide itil access) and also those users who were given itil role explicitly. Before removing it should also give row count so that the numbers can be matched. Any help is highly appreciated.

Thanks

1 ACCEPTED SOLUTION

Please try below script to delete user from support group

delete_user();
function delete_user()
{
var grp_role = new GlideRecord('sys_group_has_role');
grp_role.addQuery('role','282bf1fac6112285017366cb5f867469');
grp_role.query();
while(grp_role.next())
{
var gr_mem = new GlideRecord('sys_user_grmember');
gr_mem.addQuery('group',grp_role.group); gr_mem.addEncodedQuery("user.last_login_time<javascript:gs.daysAgoStart(60)^role=282bf1fac6112285017366cb5f867469^user.active=true");
gr_mem.query();
while(gr_mem.next())
{
gr_mem.deleteRecord();
gs.print("deleted users: " + gr_mem.user.getDisplayValue());

}


}
}

View solution in original post

24 REPLIES 24

Jayantha - this script doesn't remove inherited roles which is what I need. The script shall remove the itil role (which is generally inherited from a group). So ,the user shall be removed from the group that provided the itil role.

what are you actually planning to do, remove the user from the group or just remove the roles from the user.

 

btw, itil should not be an inherited role, unless an admin.

Shariq, generally 'itil' role is inherited from the group and not provided explicitly. Hence my requirement is to remove the user from the group that provides the 'itil' role

Can you try below code:

var count=0;
var uid =new GlideRecord('sys_user');
uid.addQuery('last_login_time', '<', gs.daysAgo(60));
uid.query();
while(uid.next()){
var urole = new GlideRecord('sys_user_has_role');
urole.addQuery('user',users.sys_id);
urole.addQuery('role', 'sys_id');// add sys_id of ITIL role
urole.query();
if (urole.next()) {

count++;
urole.deleteRecord();
gs.print('Username' + urole.user.getDisplayValue() + ' Count' + count);
}


}

Remove User from Group with ITIL role:

var count=0;
var ug =new GlideRecord('sys_group_has_role');
ug.addQuery('role', '282bf1fac6112285017366cb5f867469');
ug.query();
while(ug.next()){

gs.print('Group name' + ug.group.getDisplayValue());

var urole = new GlideRecord('sys_user_has_role');
// urole.addQuery('user',users.sys_id);
urole.addQuery('role',ug.sys_id);// add sys_id of ITIL role
urole.query();
if (urole.next()) {

count++;
urole.deleteRecord(); // remove roles from the role table
gs.print('Group name' + ug.group.getDisplayValue() + ' Username' + urole.user.getDisplayValue() + ' Count' + count);
}


}