google-apps-scriptgoogle-workspacegoogle-admin-sdk

How can i automate export of all users and status (Active, Suspended, Archived) on GWS and then have the report sent to a list of users


Im trying to automate user reports on my Google workspace account showing their statuses and have the report sent via email to a list of recipients monthly.

I am trying to get all users with their statuses with the below script:

function generateUserReport() {
  const spreadsheet = SpreadsheetApp.getActive();
  const userReportSheet = spreadsheet.getSheetByName('User Report');
  const shareWithEmails = ['example1@example.com', 'example2@example.com']; // Replace with the email addresses you want to share the report with

  const userResult = [];

  const users = getAllUsers();

  users.forEach(user => {
    let status;
    if (user.suspended == true) {
      status = "suspended";
    } else if (user.archived == true) {
      status = "archived";
    } else {
      status = "active";
    }


    let userItem = {
      "primaryEmail" : user.primaryEmail,
      "status" : status,
      "archived" : user.archived,
      "suspended" : user.suspended,
      "isAdmin" : user.isAdmin
    }
    userResult.push(userItem);
  })
  
  jsonToSheet_(userResult, "User Report");
  //shareSpreadsheet(spreadsheet, shareWithEmails);
  //notifyUsers(spreadsheet, shareWithEmails);
}

function getAllUsers() {
  let users = [];
  let pageToken;
  do {
    const response = AdminDirectory.Users.list({
      customer: 'my_customer',
      maxResults: 500,
      orderBy: 'email',
      pageToken: pageToken
    });
    users = users.concat(response.users);
    pageToken = response.nextPageToken;
  } while (pageToken);
  return users;
}


function shareSpreadsheet(spreadsheet, shareWithEmails) {
  shareWithEmails.forEach(email => {
    spreadsheet.addEditor(email);
  });
}

function notifyUsers(spreadsheet, shareWithEmails) {
  const url = spreadsheet.getUrl();
  shareWithEmails.forEach(email => {
    MailApp.sendEmail({
      to: email,
      subject: 'Monthly User Account Status Report',
      body: `The user account status report has been generated and shared with you. You can access it here: ${url}`
    });
  });
}

function createMonthlyTrigger() {
  const triggers = ScriptApp.getProjectTriggers();
  triggers.forEach(trigger => {
    if (trigger.getHandlerFunction() === 'generateUserReport') {
      ScriptApp.deleteTrigger(trigger);
    }
  });

  ScriptApp.newTrigger('generateUserReport')
    .timeBased()
    .onMonthDay(1) // Adjust the day as needed
    .atHour(9) // Adjust the hour as needed
    .create();
}

However, I get this error message when i run or debug the script (also attached a screenshot of the error message

error message

Execution log 3:35:55 PM Notice Execution started 3:37:59 PM Error
ReferenceError: AdminDirectory is not defined getAllUsers @ Code.gs:40 generateUserReport @ Code.gs:8


Solution

  • It might be possible that you have not enabled yet the Directory API. Please check out the official quickstart -> https://developers.google.com/admin-sdk/directory/v1/quickstart/apps-script