Skip to content

Commit 59df219

Browse files
authored
Merge branch 'main' into feature/new-code-snippet
2 parents 59c8a7f + e1e98f0 commit 59df219

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
This script identifies active users in ServiceNow who have no group memberships and no roles assigned.
2+
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).
3+
If a user has no associated groups and no assigned roles, their username is added to a list called orphanedUsers.
4+
Finally, the script prints the list, which can be used for user cleanup, security audits, or compliance purposes to ensure proper user management.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
var userRecord = new GlideRecord('sys_user');
2+
userRecord.addQuery('active', true);
3+
userRecord.query();
4+
5+
var orphanedUsers = [];
6+
7+
while(userRecord.next()) {
8+
var userSysId = userRecord.getValue('sys_id');
9+
10+
var userGroups = new GlideRecord('sys_user_grmember');
11+
userGroups.addQuery('user', userSysId);
12+
userGroups.query();
13+
14+
var userRoles = new GlideRecord('sys_user_has_role');
15+
userRoles.addQuery('user', userSysId);
16+
userRoles.query();
17+
18+
if(!userGroups.hasNext() && !userRoles.hasNext()) {
19+
orphanedUsers.push(userRecord.getValue('user_name'));
20+
}
21+
}
22+
23+
gs.print('Orphaned Users: ' + orphanedUsers.join(', '));

0 commit comments

Comments
 (0)