Report on the users with no roles and groups

VojjalaS
Tera Contributor

I need to pull a report of users with no roles and no groups.

9 REPLIES 9

Robbie
Kilo Patron
Kilo Patron

Hi @VojjalaS,

 

Here's a background script that I run from time to time to pull exactly this information.

 

var usersWithoutRolesOrGroups = [];

var userGR = new GlideRecord('sys_user');
userGR.addActiveQuery(); // Checking active users only.
userGR.query();

while (userGR.next()) {
    // Check for roles associated to the user
    var roleGR = new GlideRecord('sys_user_has_role');
    roleGR.addQuery('user', userGR.sys_id);
    roleGR.query();

    // Check to see if user associated to any groups 
    var groupGR = new GlideRecord('sys_user_grmember');
    groupGR.addQuery('user', userGR.sys_id);
    groupGR.query();

    if (!roleGR.hasNext() && !groupGR.hasNext()) {
        // Push relevant user data to the array
        usersWithoutRolesOrGroups.push({
            name: userGR.getValue('name'),
            username: userGR.getValue('user_name'),
            email: userGR.getValue('email'),
            sys_id: userGR.getValue('sys_id')
        });
    }
}

// Results (This could be adapted if you want to separate out role and/or group membership)
gs.print('Users with no roles and not a member of any groups: ' + usersWithoutRolesOrGroups.length);
gs.print(JSON.stringify(usersWithoutRolesOrGroups, null, 2));

 

To help others (and for me to gain recognition for my efforts), please mark this response correct by clicking on Accept as Solution and/or Kudos.



Thanks, Robbie

prsudhakar123
Tera Contributor

Dear @VojjalaS 

 

Good day ! Hope you are doing great ..

 

You may try the below method to get the list of active users who dont have role. Explore and see if this fulfills your needs. 

 

You can enter this " javascript:getRoledUsers()" under sysid

 

prsudhakar123_0-1745397435123.png

 

 

 

Dear @VojjalaS Would like to know if you achieved what you looking for .. Have a great day ! 

Yes. Thank you Sudhakar.