How To Export Associated Groups & Roles

MrDevanWright
Kilo Guru

I'm trying to obtain an excel spread sheet of all the groups, child groups, and their associated/inherited roles. Is there a way to export some file showing these relationships? 

17 REPLIES 17

LearnUseThrive
Mega Sage

If you're admin, go to System Security > Users and Groups > Groups, run a filter to select the desired groups, and right click the column header to export to xlsx, csv, etc

 

2023-05-13 01_14_22-Groups _ ServiceNow - Brave.png

This doesn't show ALL the roles assigned to each group, though. That's the issue. Please see attached screenshot of this list.

Go to System Security > Groups Roles. All roles are listed. Right-click on the context menu at top of column and Group By Group if you wish. Run a filter search, right click header at top, and export to xlsx, csv, etc.

"Groups Roles" isn't an option. Even if I go to System Security>Users and Groups>Groups, it doesn't show ALL the roles associated with that group. I have attached this before, but please see the attachment again.

Benjamin A
Mega Sage

Hello @MrDevanWright,

 

You could achieve this using a script. While probably not the most efficient way, it should work. The script below creates an array of group objects with the information you are asking for. You could then export to a csv using an adaptation of the script at this link.

 

function getRoles(groupSysId){
    
    // Create empty array of roles
    var roles = [];

    // Query all roles assigned to group
    var groupHasRoleQuery = new GlideRecord('sys_group_has_role');

    groupHasRoleQuery.addQuery('group', groupSysId);
    groupHasRoleQuery.query();

    // For each role
    while (groupHasRoleQuery.next()){

        // Create role object with name and sys_id of role
        var role = {'name': groupHasRoleQuery.role.name,'sys_id': groupHasRoleQuery.role.sys_id}

        // Add role object to array of roles
        roles.push(role);
    }
    
    // Return array of roles
    return roles;
}

// Create empty array of groups
var groups = [];

// Query all top-level groups
var groupQuery = new GlideRecord('sys_user_group');

groupQuery.addQuery('parent','');

groupQuery.query();

// For each group
while (groupQuery.next()){

    // Create group object 
    var group = {};

    // Add group name
    group.name = groupQuery.name;

    // Add group sys_id
    group.sys_id = groupQuery.sys_id;

    // Add array of roles associated with group
    groups.roles = getRoles(groupQuery.sys_id);

    // Create empty array of children
    group.children = [];

    // Query all children groups of the group
    var childrenGroups = new GlideRecord('sys_user_group');

    childrenGroups.addQuery('parent', groupQuery.sys_id);

    childrenGroups.query();

    // For each child group
    while (childrenGroups.next()){

        // Create a child object with the name, sys_id, and array of roles associated with the child group
        var child = {'name':childrenGroups.name,'sys_id':childrenGroups.sys_id,'roles':getRoles(childrenGroups.sys_id)};

        // Add child to array of children
        group.children.push(child);
    }

    // Add group to array of groups
    groups.push(group);
}