From 957d24cd32b2e9ecb2b692096d97483bce8b1c62 Mon Sep 17 00:00:00 2001 From: Shashank_Jain Date: Sun, 12 Oct 2025 12:55:49 +0530 Subject: [PATCH 1/4] Create checking for users with zero groups AND zero roles.js --- ...r users with zero groups AND zero roles.js | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 Server-Side Components/Background Scripts/User with no roles and groups/checking for users with zero groups AND zero roles.js diff --git a/Server-Side Components/Background Scripts/User with no roles and groups/checking for users with zero groups AND zero roles.js b/Server-Side Components/Background Scripts/User with no roles and groups/checking for users with zero groups AND zero roles.js new file mode 100644 index 0000000000..8f17222c55 --- /dev/null +++ b/Server-Side Components/Background Scripts/User with no roles and groups/checking for users with zero groups AND zero roles.js @@ -0,0 +1,21 @@ +var userRecord = new GlideRecord('sys_user'); +userRecord.addQuery('active', true); +userRecord.query(); + +var orphanedUsers = []; + +while(userRecord.next()) { + var userGroups = new GlideRecord('sys_user_grmember'); + userGroups.addQuery('user', userRecord.sys_id); + userGroups.query(); + + var userRoles = new GlideRecord('sys_user_has_role'); + userRoles.addQuery('user', userRecord.sys_id); + userRoles.query(); + + if(!userGroups.hasNext() && !userRoles.hasNext()) { + orphanedUsers.push(userRecord.user_name.toString()); + } +} + +gs.print('Orphaned Users: ' + orphanedUsers.join(', ')); From 537839e1ba1c4d7962f7dc265a3f11bb97835442 Mon Sep 17 00:00:00 2001 From: Shashank_Jain Date: Sun, 12 Oct 2025 12:56:55 +0530 Subject: [PATCH 2/4] README.md --- .../User with no roles and groups/README.md | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 Server-Side Components/Background Scripts/User with no roles and groups/README.md diff --git a/Server-Side Components/Background Scripts/User with no roles and groups/README.md b/Server-Side Components/Background Scripts/User with no roles and groups/README.md new file mode 100644 index 0000000000..8b5c31c600 --- /dev/null +++ b/Server-Side Components/Background Scripts/User with no roles and groups/README.md @@ -0,0 +1,4 @@ +This script identifies active users in ServiceNow who have no group memberships and no roles assigned. +It queries the sys_user table for all active users, then checks each user against the sys_user_grmember table (groups) and the sys_user_has_role table (roles). +If a user has no associated groups and no assigned roles, their username is added to a list called orphanedUsers. +Finally, the script prints the list, which can be used for user cleanup, security audits, or compliance purposes to ensure proper user management. From f7190f29daa1babe7c706c27d957cc1455f38e0f Mon Sep 17 00:00:00 2001 From: Shashank_Jain Date: Sun, 12 Oct 2025 13:12:25 +0530 Subject: [PATCH 3/4] Update checking for users with zero groups AND zero roles.js --- .../checking for users with zero groups AND zero roles.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Server-Side Components/Background Scripts/User with no roles and groups/checking for users with zero groups AND zero roles.js b/Server-Side Components/Background Scripts/User with no roles and groups/checking for users with zero groups AND zero roles.js index 8f17222c55..11da018f2f 100644 --- a/Server-Side Components/Background Scripts/User with no roles and groups/checking for users with zero groups AND zero roles.js +++ b/Server-Side Components/Background Scripts/User with no roles and groups/checking for users with zero groups AND zero roles.js @@ -14,8 +14,10 @@ while(userRecord.next()) { userRoles.query(); if(!userGroups.hasNext() && !userRoles.hasNext()) { - orphanedUsers.push(userRecord.user_name.toString()); + // Using getValue() instead of direct field access + orphanedUsers.push(userRecord.getValue('user_name')); } } gs.print('Orphaned Users: ' + orphanedUsers.join(', ')); + From 186a222b58f5c9dab20c80930d4ff2dedc97314c Mon Sep 17 00:00:00 2001 From: Shashank_Jain Date: Sun, 12 Oct 2025 13:17:37 +0530 Subject: [PATCH 4/4] Server-Side Components/Background Scripts/User with no roles and groups/checking for users with zero groups AND zero roles.js --- ...r users with zero groups AND zero roles.js | 23 ------------------- 1 file changed, 23 deletions(-) delete mode 100644 Server-Side Components/Background Scripts/User with no roles and groups/checking for users with zero groups AND zero roles.js diff --git a/Server-Side Components/Background Scripts/User with no roles and groups/checking for users with zero groups AND zero roles.js b/Server-Side Components/Background Scripts/User with no roles and groups/checking for users with zero groups AND zero roles.js deleted file mode 100644 index 11da018f2f..0000000000 --- a/Server-Side Components/Background Scripts/User with no roles and groups/checking for users with zero groups AND zero roles.js +++ /dev/null @@ -1,23 +0,0 @@ -var userRecord = new GlideRecord('sys_user'); -userRecord.addQuery('active', true); -userRecord.query(); - -var orphanedUsers = []; - -while(userRecord.next()) { - var userGroups = new GlideRecord('sys_user_grmember'); - userGroups.addQuery('user', userRecord.sys_id); - userGroups.query(); - - var userRoles = new GlideRecord('sys_user_has_role'); - userRoles.addQuery('user', userRecord.sys_id); - userRoles.query(); - - if(!userGroups.hasNext() && !userRoles.hasNext()) { - // Using getValue() instead of direct field access - orphanedUsers.push(userRecord.getValue('user_name')); - } -} - -gs.print('Orphaned Users: ' + orphanedUsers.join(', ')); -