remove users that are not logged in since 90 days

nik281092
Kilo Explorer

I have requirement where in I have to remove all users role and group if they haven't logged in since 90 days or inactive for 30 days.

I am not able to get approach and script for this needed help?

7 REPLIES 7

Suresh1
Tera Guru

Nikhil,



Use the below script, same we had a requirement and implemented.



var gR=new GlideRecord('sys_user_has_role'); // go to user role table


gR.addEncodedQuery('user.last_login_timeISEMPTY^ORuser.last_login_timeRELATIVELE@dayofweek@ago@30^user.active=true^user.locked_out=false');//check the conditions like user record is active, last login time etc...


gR.query();


while(gR.next())


{


  var SID=gR.user;       // get the user details


  var gR1=new GlideRecord('sys_user_grmember');


  gR1.addQuery('sys_id',SID);


  gR1.query();


  if(gR1.next())


  {


    gR1.deleteMultiple();   // remove the user from the group


    }


gR.deleteMultiple(); // delete the roles of those users.


}




Regards:
Suresh. D


tony_barratt
ServiceNow Employee
ServiceNow Employee

Hi Nihil,



When you say   ".. remove all users role and group" do you mean remove all the roles and groups associated with user who has not logged in in the last 90 days?



Deleting users, for example, is not recommended.


Deleting users can lead to Incidents without defined callers and other odd and undesirable outcomes.



Best Regards



Tony




polugari epapra
Tera Contributor

How to get that encoded query