diff --git a/Server-Side Components/Background Scripts/Auto-Deactivate Users Not Logged In for X Days/Auto_Deactivate_Users_Not_Logged_In_for_X_Days.js b/Server-Side Components/Background Scripts/Auto-Deactivate Users Not Logged In for X Days/Auto_Deactivate_Users_Not_Logged_In_for_X_Days.js index 5f0c36300f..a3a949cd68 100644 --- a/Server-Side Components/Background Scripts/Auto-Deactivate Users Not Logged In for X Days/Auto_Deactivate_Users_Not_Logged_In_for_X_Days.js +++ b/Server-Side Components/Background Scripts/Auto-Deactivate Users Not Logged In for X Days/Auto_Deactivate_Users_Not_Logged_In_for_X_Days.js @@ -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); diff --git a/Server-Side Components/Background Scripts/Auto-Deactivate Users Not Logged In for X Days/README.md b/Server-Side Components/Background Scripts/Auto-Deactivate Users Not Logged In for X Days/README.md index 8d987462ea..0e88a796a6 100644 --- a/Server-Side Components/Background Scripts/Auto-Deactivate Users Not Logged In for X Days/README.md +++ b/Server-Side Components/Background Scripts/Auto-Deactivate Users Not Logged In for X Days/README.md @@ -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.