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

Mrinmoy Koley
Kilo Expert

Create a scheduled job, compare last login time with current date time.



If its more than 30 days, set active true and lockout true.


If more than 90 days, you can delete the user record.



Mrinmoy


Hey Mrinmoy,



Can you please provide script for this


Nikhil,



        Make sure your conditions you want to apply before executing this, because this involves deleteMultiple();


Script:


var gr= new GlideRecord('sys_user');


gr.addEncodedQuery('active=true^last_loginRELATIVELT@dayofweek@ago@90'); // this gets all the users who are active and last logged in 3 months(90) ago


gr.query();


while(gr.next()){


var gr2= new GlideRecord('sys_user_grmember');


gr2.addQuery('user',gr.sys_id);


gr2.query();


gr2.deleteMultiple(); //deletes the user from the groups


var gr1= new GlideRecord('sys_user_has_role');


gr1.addQuery('user',gr.sys_id);


gr1.query();


gr1.deleteMultiple(); // deletes all the roles the user have


}




Thanks,


Abhinay




PS: Hit like, Helpful or Correct depending on the impact of the response


Hi mrinmoy,

 

Last login date and current date time comparing fine but how to write logic if its more than 30 days in Java coding.

 

Thanks 

Rohit