Last active
May 13, 2020 01:27
-
-
Save gjyoung1974/777483a97ee7f2d165212542dac128aa to your computer and use it in GitHub Desktop.
Get GSuite users
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Gets users in a GSuite directory and add to a Gsheet | |
*/ | |
//Expose this as a Webhook ? | |
//function doGet(e) { | |
// GetGsuiteUsers(); | |
// return HtmlService.createHtmlOutput("GET request received"); | |
//} | |
//function doPost(e) { | |
// GetGsuiteUsers(); | |
// return HtmlService.createHtmlOutput("POST request received"); | |
//} | |
function GetGsuiteUsers() { | |
//Set a range for recent logins | |
const sixyDaysAgo = new Date(); | |
loginRange = new Date(sixyDaysAgo.setDate(sixyDaysAgo.getDate() - 60)); | |
// Creating a new sheet - TODO append a running sheet | |
var now = new Date(); | |
var sheet = SpreadsheetApp.create('gsuite_postmates_users_' + now).getActiveSheet(); | |
// Array of OU's to exclude | |
var excluded_ous = ["/Service Accounts", "/Former/On Hold", "/Former/Suspended", "/Temps"]; | |
// array to store sheet row values | |
var data = []; | |
var archive; | |
var pageToken, page; | |
var sheetrange = sheet.getRange("A:F") | |
sheetrange.clear() | |
// Add the required CSV headers | |
data.push(['Email', 'First Name', 'Last Name', 'Archive', 'Last Login Time', 'Creation', 'Org Unit Path', 'Aliases']); | |
do { | |
page = AdminDirectory.Users.list({ | |
domain: 'example.com', | |
pageToken: pageToken | |
}); | |
var users = page.users; | |
if (users) { | |
for (var i = 0; i < users.length; i++) { | |
var user = users[i]; | |
//If user isn't within an excluded OU | |
if (!excluded_ous.includes(user.orgUnitPath)) { | |
//User has logged on to GSuite within the period | |
if (Date.parse(user.lastLoginTime) >= loginRange) { | |
//Add the row to the sheet | |
data.push([user.primaryEmail, user.name.givenName, user.name.familyName, archive, user.lastLoginTime, user.creationTime, user.orgUnitPath, user.aliases]); | |
} | |
} | |
} | |
} else { | |
Logger.log('No users found.'); | |
} | |
pageToken = page.nextPageToken; | |
} while (pageToken); | |
sheet.getRange(1, 1, data.length, data[0].length).setValues(data); | |
var dated = sheet.getRange("P1") | |
dated.setValue(Utilities.formatDate(new Date(), Session.getScriptTimeZone(), 'dd-MMM-yyy')); | |
} | |
// GSuite User Object's properties | |
//addresses : Object | |
//agreedToTerms : Boolean | |
//aliases : String[] | |
//archived : Boolean | |
//changePasswordAtNextLogin : Boolean | |
//creationTime : String | |
//customSchemas : Object | |
//customerId : String | |
//deletionTime : String | |
//emails : Object | |
//etag : String | |
//externalIds : Object | |
//gender : Object | |
//hashFunction : String | |
//id : String | |
//ims : Object | |
//includeInGlobalAddressList : Boolean | |
//ipWhitelisted : Boolean | |
//isAdmin : Boolean | |
//isDelegatedAdmin : Boolean | |
//isEnforcedIn2Sv : Boolean | |
//isEnrolledIn2Sv : Boolean | |
//isMailboxSetup : Boolean | |
//keywords : Object | |
//kind : String | |
//languages : Object | |
//lastLoginTime : String | |
//locations : Object | |
//name : UserName | |
//nonEditableAliases : String[] | |
//notes : Object | |
//orgUnitPath : String | |
//organizations : Object | |
//password : String | |
//phones : Object | |
//posixAccounts : Object | |
//primaryEmail : String | |
//recoveryEmail : String | |
//recoveryPhone : String | |
//relations : Object | |
//sshPublicKeys : Object | |
//suspended : Boolean | |
//suspensionReason : String | |
//thumbnailPhotoEtag : String | |
//thumbnailPhotoUrl : String | |
//websites : Object |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment