2

I have a member in each group on our domain and I want to list all the members and the groups that are members of each group. I am getting errors when there are banned members in a group as it appears to list some of those members and when it iterates through the groups that are members it fails if there are banned members with an access error.

function listGroups() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();

  var groups = GroupsApp.getGroups();
  var results = [];

  for (var i = 0; i < groups.length; i++){
    if(groups[i] != ""){
      Utilities.sleep(1000);
      results.push([groups[i].getEmail(),groups[i].getUsers().length +" Members in Group"]);
      if(groups[i].getUsers().length > 0){
        Utilities.sleep(1000);
        var members = groups[i].getUsers();
        for (var j = 0; j < members.length; j++){
          if (members[j] != ""){
            results.push([groups[i].getEmail(), members[j].getEmail()]);
          }
        }
      } // end members check
      if(groups[i].getGroups().length > 0){
        results.push([groups[i].getEmail(),groups[i].getGroups().length + "Member Groups"]);
        Utilities.sleep(1000);
        var groupInGroup = groups[i].getGroups();
        for (var k = 0; k < groupInGroup.length; k++){
          if (groupInGroup[k] != ""){
            results.push([groups[i].getEmail(),groupInGroup[k].getEmail()]);
          }
        }
      } // end Groups in Groups check.
    }
  }



  var outputSheetName = 'Group Listing '+ new Date();
  ss.insertSheet().setName(outputSheetName);
  var outputSheet = ss.getSheetByName(outputSheetName);

  outputSheet.clear();
  outputSheet.getRange(outputSheet.getLastRow()+1,1,results.length,results[0].length).setValues(results);
}

Possibly only listing Group Members by role would get past these errors but it seems to be a problem with .getMembers() and .getGroups() Am I right in thinking the only way round it is listing the members by role?

MαπμQμαπkγVπ.0
  • 5,887
  • 1
  • 27
  • 65
  • Could you share to the community what error(s) you are getting? – MαπμQμαπkγVπ.0 Dec 04 '18 at 11:06
  • There is no error when just listing the members but it is showing banned members in the output or at least some of the banned members(possibly only banned members with google accounts). When listing the groups it fails if all domain users are a member. Also fails if there are banned members with a "cannot find a group named: /cu/domain/username (line 21, file"code")" – Tom Easterbrook Dec 05 '18 at 12:10

0 Answers0