Search code examples
google-apps-scriptgoogle-admin-sdkgoogle-workspace

Exclude users from belonging to a particular group in G Suite


I am trying to write a script in Google apps script that will suspend the account of users whose account has been inactive for than 120 days. But I don't want the script to delete the accounts of users who are in a particular group. The following is the script that I came up with:

/**
 * Lists all the users in a domain sorted by first name.
 */
function listAllUsers() {

  var contents = [];
  var pageToken;
  var page;
  do {
    page = AdminDirectory.Users.list({
      customer: 'xyz12345',
      orderBy: 'givenName',
      maxResults: 100,
      pageToken: pageToken
    });
    var users = page.users;
    if (users) {
      for (var i = 0; i < users.length; i++) {
        var user = users[i];
        if(user.suspended==false){
          contents.push([user.name.fullName, user.primaryEmail, user.creationTime, user.lastLoginTime]);
        }
      }
    } else {
      Logger.log('No users found.');
    }
    pageToken = page.nextPageToken;
  } while (pageToken);
  checkUserLoginTime(contents);
}


/**
 * Check the user Log in Time of the users.
 * @param contents (The array containing the users list)
 */
function checkUserLoginTime(contents) {
  //Logger.log(contents);
  var usersToSuspend = [];
  var timeNow = new Date();
  var checkTime = new Date(); 
  checkTime.setDate(checkTime.getDate()-120);
  checkTime = new Date(checkTime);
  Logger.log('TimeNow:'+ timeNow);
  Logger.log('checktime:'+ checkTime);

  for (var i=0; i<contents.length; i++){
    var fullName = contents[i][0];
    var email = contents[i][1];
    var formattedCreationTime = new Date(contents[i][2]);
    var formattedLastLoginTime = new Date(contents[i][3]);
    Logger.log(fullName);
    Logger.log(email);
    Logger.log(formattedCreationTime);
    Logger.log(formattedLastLoginTime);
    if(formattedCreationTime <= checkTime && formattedLastLoginTime <= checkTime){
      usersToSuspend.push([fullName, email]); 
      Logger.log('The user account is Inactive')
      //suspendUsers(email);
    } else{
      Logger.log('The user account is Active')
    }
  }

}

This script will push all the user accounts the array usersToSuspend, whose last login time was greater than 120 days. Is there a way to filter out this array or stop pushing the emails to this array which belong to a particular group say for eg: [email protected]?


Solution

  • The script that finally worked for me without creating a separate function or complex for loops is as follows:

    /**
     * Lists all the users in a domain sorted by first name.
     */
    function listAllUsers() {
    
      var contents = [];
      var pageToken;
      var page;
      do {
        page = AdminDirectory.Users.list({
          customer: 'xyz12345',
          orderBy: 'givenName',
          maxResults: 100,
          pageToken: pageToken
        });
        var users = page.users;
        if (users) {
          for (var i = 0; i < users.length; i++) {
            var user = users[i];
            if(user.suspended==false){
              contents.push([user.name.fullName, user.primaryEmail, user.creationTime, user.lastLoginTime]);
            }
          }
        } else {
          Logger.log('No users found.');
        }
        pageToken = page.nextPageToken;
      } while (pageToken);
      checkUserLoginTime(contents);
    }
    
    
    /**
     * Check the user Log in Time of the users.
     * @param contents (The array containing the users list)
     */
    function checkUserLoginTime(contents) {
      //Logger.log(contents);
      var usersToSuspend = [];
      var timeNow = new Date();
      var checkTime = new Date();
      var group = GroupsApp.getGroupByEmail("[email protected]");     // gets the group
      var users = group.getUsers();     // gets the users in the group
      checkTime.setDate(checkTime.getDate()-120);
      checkTime = new Date(checkTime);
      Logger.log('TimeNow:'+ timeNow);
      Logger.log('checktime:'+ checkTime);
    
      for (var i=0; i<contents.length; i++){
        var fullName = contents[i][0];
        var email = contents[i][1];
        var formattedCreationTime = new Date(contents[i][2]);
        var formattedLastLoginTime = new Date(contents[i][3]);
        Logger.log(fullName);
        Logger.log(email);
        Logger.log(formattedCreationTime);
        Logger.log(formattedLastLoginTime);
        if(formattedCreationTime <= checkTime && formattedLastLoginTime <= checkTime){
          if (group.hasUser(email)) {     //checks if the user is part of the group
            Logger.log(email + ' belongs to [email protected] group');
          } else{
           usersToSuspend.push([fullName, email]); 
           Logger.log('The user account is Inactive')
           //suspendUsers(email);
          }
        } else{
          Logger.log('The user account is Active')
        }
      }
    
    }
    

    The group.hasUser(email) eliminates the need to run a for and if loop.