- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-21-2023 01:50 AM
I have a script that is looking for users who have been granted an ITIL licence for more than 45 days, but have either not logged in for 45 days, or have never logged in. There are a couple of exceptions to cater for, but users satisfying these conditions are removed from the group membership table in order to recover ITIL licences that are not being used.
The script is doing what I want. But when I looked at the log statements it was not doing it in the way I expected and it was very inefficient.
Here is the script and below I have put the log output. If I comment out the 'updateUserGroups.deleteRecord();' line inside the for loop, and just write comments, the script works as expected and loops through each of the 3 users incrementally, and only once.
Thanks for any help
Ruth
______________________________________________________________________________________________
var users = [];
var userUnique = [];
// Look for users given the ITIL role more than 45 days ago
var itil = new GlideRecord('sys_user_has_role');
itil.addEncodedQuery('role.name=itil^sys_created_onRELATIVELT@dayofweek@ago@45^user.nameINUser1,User2,User3');
itil.query();
gs.log('Role records found: ' + itil.getRowCount()); // Remove
while (itil.next()) {
users.push(itil.user.toString());
}
userUnique = new ArrayUtil().unique(users);
gs.log('Users to check: ' + userUnique); // Remove
gs.log ('Array length: ' + userUnique.length);
// For each user, check last login and preserve flag
for (var i = 0; i < userUnique.length; i++) {
gs.log('What we are checking: i = ' + i + ' userUnique[i] = ' + userUnique[i]); // Remove
var usrItil = new GlideRecord('sys_user');
usrItil.get(userUnique[i]);
gs.log('User: ' + usrItil.name);
var newComment = '';
// Remove user from any group memberships
var updateUserGroups = new GlideRecord('sys_user_grmember');
updateUserGroups.addQuery('user', usrItil.sys_id);
updateUserGroups.query();
while (updateUserGroups.next()) {
newComment = newComment + "This user was removed from " + updateUserGroups.getDisplayValue() + " on " + gs.now() + " as they had not logged into ServicePortal for 45 days prior\n";
gs.log(usrItil.name + ': ' + newComment);
updateUserGroups.deleteRecord();
}
//usrItil.u_comments = usrItil.u_comments + "\n" + newComment;
//usrItil.update();
}
__________________________________________________________________________________________________
*** Script: Role records found: 3
*** Script: Users to check: 9eb3ab56dbdcc4d089f0fba66896199d,a4ebea22dbe0dc900929b29f299619a9,cb51d48edb7904540929b29f29961949
*** Script: Array length: 3
i=0
*** Script: What we are checking: i = 0 userUnique[i] = 9eb3ab56dbdcc4d089f0fba66896199d
*** Script: User: User1
*** Script: User1: This user was removed from Group1 on 21/02/2023 as they had not logged into ServicePortal for 45 days prior
*** Script: User1: This user was removed from Group1 on 21/02/2023 as they had not logged into ServicePortal for 45 days prior
This user was removed from Group2 on 21/02/2023 as they had not logged into ServicePortal for 45 days prior
*** Script: User1: This user was removed from Group1 on 21/02/2023 as they had not logged into ServicePortal for 45 days prior
This user was removed from Group2 on 21/02/2023 as they had not logged into ServicePortal for 45 days prior
This user was removed from Group3 on 21/02/2023 as they had not logged into ServicePortal for 45 days prior
i=1
*** Script: What we are checking: i = 1 userUnique[i] = a4ebea22dbe0dc900929b29f299619a9
*** Script: User: User2
*** Script: User2: This user was removed from Group1 on 21/02/2023 as they had not logged into ServicePortal for 45 days prior
*** Script: User2: This user was removed from Group1 on 21/02/2023 as they had not logged into ServicePortal for 45 days prior
This user was removed from Group2 on 21/02/2023 as they had not logged into ServicePortal for 45 days prior
*** Script: User2: This user was removed from Group1 on 21/02/2023 as they had not logged into ServicePortal for 45 days prior
This user was removed from Group2 on 21/02/2023 as they had not logged into ServicePortal for 45 days prior
This user was removed from Group3 on 21/02/2023 as they had not logged into ServicePortal for 45 days prior
*** Script: User2: This user was removed from Group1 on 21/02/2023 as they had not logged into ServicePortal for 45 days prior
This user was removed from Group2 on 21/02/2023 as they had not logged into ServicePortal for 45 days prior
This user was removed from Group3 on 21/02/2023 as they had not logged into ServicePortal for 45 days prior
This user was removed from Group4 on 21/02/2023 as they had not logged into ServicePortal for 45 days prior
*** Script: User2: This user was removed from Group1 on 21/02/2023 as they had not logged into ServicePortal for 45 days prior This user was removed from Group2 on 21/02/2023 as they had not logged into ServicePortal for 45 days prior This user was removed from Group3 on 21/02/2023 as they had not logged into ServicePortal for 45 days prior This user was removed from Group4 on 21/02/2023 as they had not logged into ServicePortal for 45 days prior This user was removed from Group5 on 21/02/2023 as they had not logged into ServicePortal for 45 days prior
i is 1 again (no groups to remove as already removed) and then it increments to 2
*** Script: What we are checking: i = 1 userUnique[i] = a4ebea22dbe0dc900929b29f299619a9
*** Script: User: User2
i=2
*** Script: What we are checking: i = 2 userUnique[i] = cb51d48edb7904540929b29f29961949
*** Script: User: User3
*** Script: User3: This user was removed from Group1 on 21/02/2023 as they had not logged into ServicePortal for 45 days prior
*** Script: User3: This user was removed from Group1 on 21/02/2023 as they had not logged into ServicePortal for 45 days prior
This user was removed from Group2 on 21/02/2023 as they had not logged into ServicePortal for 45 days prior
i is 1 again (no groups to remove as already removed), then it increments to 2 (no groups to remove as already gone)
*** Script: What we are checking: i = 1 userUnique[i] = a4ebea22dbe0dc900929b29f299619a9
*** Script: User: User2
*** Script: What we are checking: i = 2 userUnique[i] = cb51d48edb7904540929b29f29961949
*** Script: User: User3
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-21-2023 02:32 AM
Why don't you replace your entire for loop with the much more performant deleteMultiple() method?
var userGroupRemoval = new GlideRecord('sys_user_grmember');
userGroupRemoval.addEncodedQuery('user.sys_idIN' + userUnique.toString());
userGroupRemoval.deleteMultiple();
Otherwise, if you really need granular information like the group names the users gets removed from etc., you can keep your for loop without the record deletion part (just to collect the data), and either run the above code block afterwards, or create a new array outside your for loop, into which you would collect all the sys_ids of the Group Membership records you have looped through, and then do a similar deleteMultiple() method using those sys_ids. Hope it makes sense 🙂
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-21-2023 02:32 AM
Why don't you replace your entire for loop with the much more performant deleteMultiple() method?
var userGroupRemoval = new GlideRecord('sys_user_grmember');
userGroupRemoval.addEncodedQuery('user.sys_idIN' + userUnique.toString());
userGroupRemoval.deleteMultiple();
Otherwise, if you really need granular information like the group names the users gets removed from etc., you can keep your for loop without the record deletion part (just to collect the data), and either run the above code block afterwards, or create a new array outside your for loop, into which you would collect all the sys_ids of the Group Membership records you have looped through, and then do a similar deleteMultiple() method using those sys_ids. Hope it makes sense 🙂
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-22-2023 12:17 AM
Hi Laszlo
Thanks for your reply. I will take a look at that today, and let you know how it goes.
Regards
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-21-2023 02:34 AM
Hi @ruth3brown ,
In the below lines of code.
var updateUserGroups = new GlideRecord('sys_user_grmember');
updateUserGroups.addQuery('user', usrItil.sys_id);
updateUserGroups.query();
while (updateUserGroups.next()) {
newComment = newComment + "This user was removed from " + updateUserGroups.getDisplayValue() + " on " + gs.now() + " as they had not logged into ServicePortal for 45 days prior\n";
gs.log(usrItil.name + ': ' + newComment);
updateUserGroups.deleteRecord();
}
A user might be a part of many groups. That is the reason why this loop goes repeating.
Also, I do not see the script where you were checking whether the user had logged in last 45 days or not
Let me know if you need more help with this script
Mark helpful if it helps in solving your query.
Regards,
Johns
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-22-2023 12:23 AM
Hi Johns
Thanks for your reply. The bit of script that loops round each group the user belongs to is working. In the output I can see the comments with the group names for the user concatenating. It is the the way the 'for loop' is behaving that is giving me a problem.
And you are right, the code to check when the user last logged in is not there. It is in my original script but I had removed it to keep the code simple for testing.