- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-04-2018 01:01 AM
Hi All,
This should be easy. I'm trying to find the ITIL users who haven't logged into system for past 30 days. The below background script gives me all the users. In addition to it , I also need the total count for these users. Can you please help here?
Script -
var users = new GlideRecord('sys_user');
users.addQuery('last_login_time', '<', gs.daysAgo(30));
users.query();
while(users.next()){
var hasRole = new GlideRecord('sys_user_has_role');
hasRole.addQuery('user', users.sys_id);
hasRole.addQuery('role.name', 'itil');
hasRole.query();
if (hasRole.next()) {
gs.print("The user" + users.name + "has role itil and it is going to be removed");
//hasRole.deleteRecord();
}
}
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-05-2018 10:50 PM
Hi ,
To get Count of these users you can simply take a counter as shown below:
var usersCount=0;
var users = new GlideRecord('sys_user');
users.addQuery('last_login_time', '<', gs.daysAgo(30));
users.query();
while(users.next()){
var hasRole = new GlideRecord('sys_user_has_role');
hasRole.addQuery('user', users.sys_id);
hasRole.addQuery('role.name', 'itil');
hasRole.query();
while (hasRole.next()) {
gs.print("The user " + users.name + " has role itil and it is going to be removed");
usersCount++;
}
}
gs.print("count : "+usersCount);
Hope it helps.
Regards,
Karishma Horo

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-04-2018 01:31 PM
It's typically recommended that User records be inactivated, rather than deleted. But the logic for doing either would be the same. I'd try something like this:
//Get the IDs for all Users that haven't logged in for over 30 days
var users = [];
var gr = new GlideRecord('sys_user');
gr.addQuery('last_login_time', '<', gs.daysAgo(30));
gr.query();
while(gr.next())
users.push(gr.sys_id.toString())
//Check for roles and get count
var counter = 0;
var currentUser = gs.getUser();
for(i = 0; i < users.length; i++){
currentUser = currentUser.getUserByID(users[i]);
if(currentUser.hasRole('itil')){
// do action (delete, inactive, lockout)
counter++;
}
}
Hope that helps
-Brian
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-05-2018 10:50 PM
Hi ,
To get Count of these users you can simply take a counter as shown below:
var usersCount=0;
var users = new GlideRecord('sys_user');
users.addQuery('last_login_time', '<', gs.daysAgo(30));
users.query();
while(users.next()){
var hasRole = new GlideRecord('sys_user_has_role');
hasRole.addQuery('user', users.sys_id);
hasRole.addQuery('role.name', 'itil');
hasRole.query();
while (hasRole.next()) {
gs.print("The user " + users.name + " has role itil and it is going to be removed");
usersCount++;
}
}
gs.print("count : "+usersCount);
Hope it helps.
Regards,
Karishma Horo