Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Created by:
- Michael Stone (michael.stone@bettercloud.com)
- BetterCloud, 2017
- BetterIT Slack Community
- Updated by:
- Dan Gualtieri (daniel.gualtieri@bettercloud.com)
- BetterCloud, 2019
- BetterIT Slack Community
- DISCLAIMER: Sample scripts in this guide are not supported under any BetterCloud, Inc. (“BetterCloud”) support program or service and shall be deemed “Beta Services” under any Master Subscription Agreement or any other services agreement entered into with BetterCloud. The sample scripts are provided AS IS without representation or warranty of any kind, whether express or implied. BetterCloud disclaims all implied warranties including, without limitation, any implied warranties of merchantability or of fitness for a particular purpose. By using a sample script, you acknowledge and agree: (a) that the entire risk arising out of the use or performance of the sample scripts and documentation remains with you: and (b) that in no event shall BetterCloud, its employees, officers, directors, contractors or agents, or anyone else involved in the creation, production, or delivery of the scripts, be liable for any loss, costs, or damages whatsoever (including, without limitation, damages for loss of business profits, business interruption, loss of business information, or other pecuniary loss) arising out of or relating to the performance, use of, or inability to use the sample scripts or documentation, even if BetterCloud has been advised of the possibility of such damages.
- */
- //Global Variables - Edit lines 17, 19 and 20
- var sheetID = "ENTER YOUR SHEET ID HERE" //Found in the address bar of the sheet
- var sheet = SpreadsheetApp.openById(sheetID)
- var userSheet = sheet.getSheetByName("Offboarding") //Ensure you've updated the tabbed name of your sheet
- var nameOfOu = "/ENTER YOUR OFFBOARDING OU HERE" //The OU that triggers offboarding in BetterCloud
- var logColumn = "I"
- var userMovedColumn = "H"
- var daysUntilMoveColumn = "G"
- var userManagerColumn = "D"
- // MAIN/CRON FUNCTIONS
- function dailyUserOffboardWork(){
- dailyFormulaUpdates() //Update the 'Days Until Offboarded Column'
- updateManager()
- var users = getUsers() //get the users from the Sheet
- runUserOffboardCheck(users) //check all users to see if end date is 0 or less days and hasn't been created. If so, offboard the user. If not, do nothing
- }
- function dailyFormulaUpdates(){
- //UPDATES COLUMNS THE CORRECT FORMULA
- var lastRow = userSheet.getLastRow() //Find the last row
- for(var i = 0 ; i < (lastRow-1); i ++){ //Go through every row in the sheet
- userSheet.getRange(daysUntilMoveColumn+(i+2)).setFormula("=F"+ (i+2) +"-Now()") //reset the formula so the date difference is always correct
- }
- }
- function runUserOffboardCheck(users){
- var users = getUsers() //get users from the sheet
- for (var i = 0; i < users.length; i++){ //Check all users
- if(users[i].daysUntilOffboard <= 0){ //If offboard date is less than or equal to 0
- if(users[i].userMovedToOU != "YES"){ //If the user has not already been removed
- moveToOU(users[i], nameOfOu, i) //move User to OU
- }
- }
- }
- }
- function getUsers(){
- var rawUsers = userSheet.getRange(2,1,sheet.getLastRow(),sheet.getLastColumn()).getValues() //Get users from sheet
- var userArray = [] //set blank User array
- for (var i = 0; i < rawUsers.length - 1; i++){ //push User Object into an array
- //Logger.log(rawUsers[i][2])
- userArray.push(
- {
- "primaryEmail" : rawUsers[i][2],
- "managerEmail" : rawUsers[i][3],
- "offboardingDate" : rawUsers[i][5],
- "submittedBy" : rawUsers[i][1],
- "daysUntilOffboard" : rawUsers[i][6],
- "userMovedToOU" : rawUsers[i][7]
- }
- )
- }
- return userArray
- }
- function moveToOU(user, ou, row){
- var users = getUsers() //get the users from the Sheet
- var row = row + 2 //Set first Row
- //Logger.log(user)
- try{
- //Logger.log(user.primaryEmail)
- AdminDirectory.Users.update({orgUnitPath: ou, primaryEmail: user.primaryEmail}, user.primaryEmail)
- userSheet.getRange(logColumn+row).setValue("User " + user.primaryEmail + " was moved to " + nameOfOu + ": " + Date()) //Write 'user moved' log to Sheet
- userSheet.getRange(userMovedColumn + row).setValue("YES") //Set 'Offboarded’ value to YES
- } catch(e){ //If the user creation fails...
- //Logger.log(logColumn+row)
- userSheet.getRange(logColumn+row).setValue("User was not moved. Failure reason: " + e) //Write failure reason to Sheet
- userSheet.getRange(userMovedColumn + row).setValue("NO") //Set 'user created' to NO
- }
- }
- function updateManager (user, managerEmail, row){
- var users = getUsers() //get the users from the Sheet
- for (var i = 0; i < users.length; i++){ //Check all users
- //Logger.log("MANAGER: " + users[i].managerEmail)
- //Logger.log(users[i].primaryEmail)
- try{
- AdminDirectory.Users.update({"relations": [{"value": users[i].managerEmail, "type": "manager"}]}, users[i].primaryEmail)
- }catch(e){Logger.log(e)}
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement