- 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-22-2023 02:11 AM
Hi @ruth3brown ,
I cannot see any error with the for loop you have created. I still don't know why the last 5 lines is getting printed.
I had run it in the background in my PDI and it worked fine without any issues.
Can you run your script 1 more time and see if the last 5 lines are still getting added to the logs again?
Regards,
Johns
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-23-2023 01:07 AM
Thanks for all contributions. I have got the script working now by using deletemultiple() outside the 'for' loop. I have attached the final script and output. The final script includes more than was in my original question. Once I got the principle working I could apply it to the full script