- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-29-2024 09:32 AM
find the group who has itil role and group member is empty.
for this I have written a script , but it is not working.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-29-2024 11:47 AM
Hi, I could not quite understand the approach you were taking with your code, but I think a better solution would be to start by querying sys_group_has_role table, filtering for groups based on the role(s) you are checking for, then loop through the results. Perhaps something like this.
//An array of Roles to query or you can hard code the query.
var myRoles = ['itil', 'itil_admin'];
var myQuery = 'role.nameIN' + myRoles.toString();
//An arry for results
var groupWithNoMembers = [];
//Query Group Roles table
var groupRec = new GlideRecord('sys_group_has_role');
groupRec.addEncodedQuery(myQuery);
groupRec.query();
while (groupRec.next()) {
//Loop through results and check group member table
var myId = groupRec.group.sys_id;
var userRec = new GlideRecord('sys_user_grmember');
userRec.addQuery('group.sys_id', myId);
userRec.setLimit(1);
userRec.query();
//If no result then the group has no members, so push group into results array.
if(!userRec.next()) {
groupWithNoMembers.push(groupRec.group.name);
}
}
gs.info('Results ' + groupWithNoMembers.toString());
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-29-2024 11:47 AM
Hi, I could not quite understand the approach you were taking with your code, but I think a better solution would be to start by querying sys_group_has_role table, filtering for groups based on the role(s) you are checking for, then loop through the results. Perhaps something like this.
//An array of Roles to query or you can hard code the query.
var myRoles = ['itil', 'itil_admin'];
var myQuery = 'role.nameIN' + myRoles.toString();
//An arry for results
var groupWithNoMembers = [];
//Query Group Roles table
var groupRec = new GlideRecord('sys_group_has_role');
groupRec.addEncodedQuery(myQuery);
groupRec.query();
while (groupRec.next()) {
//Loop through results and check group member table
var myId = groupRec.group.sys_id;
var userRec = new GlideRecord('sys_user_grmember');
userRec.addQuery('group.sys_id', myId);
userRec.setLimit(1);
userRec.query();
//If no result then the group has no members, so push group into results array.
if(!userRec.next()) {
groupWithNoMembers.push(groupRec.group.name);
}
}
gs.info('Results ' + groupWithNoMembers.toString());