Find users who has roles but not in any groups

Avinash72
Tera Contributor

Hi All,

 

I want to find out the users who has roles in their profile but with No groups. 

 

We are planning to remove that roles from that users and we will assign them the groups.

 

There are around 1 lakh active users in our user table.

1 ACCEPTED SOLUTION

swathisarang98
Giga Sage
Giga Sage

Hi @Avinash72 ,

 

You can create a fix script or you can run the below script in "script -background" it will give you the list of user name who has role but not group,

 

var user = new GlideRecord('sys_user');
user.addActiveQuery();
user.query();


while(user.next()){
    var grRole = new GlideRecord ('sys_user_has_role');
    grRole.addQuery('user', user.sys_id);
    grRole.query();
    if(grRole.hasNext()){

        var grmember = new GlideRecord('sys_user_grmember');
        grmember.addQuery('user', user.sys_id);
        grmember.query();
        if(!grmember.hasNext()){
            gs.print(user.getDisplayValue());
        }


    }

       


}

 

 

Please mark this comment as Correct Answer/Helpful if it helped you.

Regards,

Swathi Sarang

View solution in original post

2 REPLIES 2

swathisarang98
Giga Sage
Giga Sage

Hi @Avinash72 ,

 

You can create a fix script or you can run the below script in "script -background" it will give you the list of user name who has role but not group,

 

var user = new GlideRecord('sys_user');
user.addActiveQuery();
user.query();


while(user.next()){
    var grRole = new GlideRecord ('sys_user_has_role');
    grRole.addQuery('user', user.sys_id);
    grRole.query();
    if(grRole.hasNext()){

        var grmember = new GlideRecord('sys_user_grmember');
        grmember.addQuery('user', user.sys_id);
        grmember.query();
        if(!grmember.hasNext()){
            gs.print(user.getDisplayValue());
        }


    }

       


}

 

 

Please mark this comment as Correct Answer/Helpful if it helped you.

Regards,

Swathi Sarang

marekwerner
Tera Contributor

Go to sys_user_has_role , select "Inherited" from Personalized List. Filter out "true" from Inherited. Now you have a list of users who didn't get roles from groups.