Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,32 @@ function deactivateDormantUsers(daysInactive) {
var count = 0;
while (gr.next()) {
gr.active = false;
removeUserFromGroups(gr.sys_id.toString()); //calling the function to remove the user from enrolled groups
gr.update();
count++;
}

gs.info('✅ Deactivated ' + count + ' users inactive for over ' + daysInactive + ' days (before ' + cutoff.getDisplayValue() + ').');
}
// below function is used to remove the user from the groups which the user is already a part of
function removeUserFromGroups(userSysId) {
var gm = new GlideRecord('sys_user_grmember');
gm.addQuery('user', userSysId);
gm.query();

var removedCount = 0;
while (gm.next()) {
var groupName = gm.group.name.toString();
gm.deleteRecord();
removedCount++;
gs.info('Removed from group: ' + groupName);
}

if (removedCount === 0)
gs.info('No group memberships found for user: ' + userSysId);
else
gs.info('Removed ' + removedCount + ' group memberships for user: ' + userSysId);
}

// Example run
// deactivateDormantUsers(90);
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@
2. Paste the script and execute:
```javascript
deactivateDormantUsers(90);

**Script Explanation**
This script helps the admin team to identify the users who are inactive since the last "x" amount of days and make their profiles inactive. Along with the deactivation of user accounts, the group memberships of the user profiles also will be removed to prevent any mishappen.
Loading