-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathUpdateDevices.gs
59 lines (56 loc) · 2.84 KB
/
UpdateDevices.gs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
function updateChromebook() {
// Display a dialog box with a message and "Yes" and "No" buttons. The user can also close the
// dialog by clicking the close button in its title bar.
var ui = SpreadsheetApp.getUi();
var response = ui.alert('Confirmation: Update Devices','Do you really want to update these devices?', ui.ButtonSet.YES_NO);
// Process the user's response.
if (response == ui.Button.YES) {
Logger.log('Ok, updating devices');
// Get User/Operator Info
var userEmail = Session.getActiveUser().getEmail()
// Get the current spreadsheet
var ss = SpreadsheetApp.getActiveSpreadsheet();
// Set the sheet called UpdateCBs as the sheet we're working in
var sheet = SpreadsheetApp.setActiveSheet(ss.getSheetByName("UpdateCBs"));
// Log actions to the sheet called Log
var logsheet = SpreadsheetApp.setActiveSheet(ss.getSheetByName("Log"));
// Get all data from the second row to the last row with data, and the last column with data
var lastrow = sheet.getLastRow();
var lastcolumn = sheet.getLastColumn();
var range = sheet.getRange(2,1,lastrow-1,lastcolumn);
var list = range.getValues();
for (var i=0; i<list.length; i++) {
// Grab serial number from the first column (0), then the rest from adjoing columns and set necessary variables
var serno = list[i][0];
var room = list[i][2].toString();
var asset = list[i][3].toString();
var user = list[i][4].toString();
var note = list[i][5].toString();
var ou = list[i][1].toString();
// Since we provided serial numbers, convert each to device-id
var sernoquery = "id:"+serno;
// Use AdminSDK API to check if the cros device exists. Else the update will fail
var chromebooklist = AdminDirectory.Chromeosdevices.list('my_customer', {query: sernoquery}).chromeosdevices;
if (!chromebooklist) {
logsheet.appendRow([serno, "not found"]);
} else if (chromebooklist.length !== 1) {
logsheet.appendRow([serno, chromebooklist.length+" found"]);
} else {
var id = chromebooklist[0].deviceId;
// For each line, try to update the device with given data, and log the result
try {
AdminDirectory.Chromeosdevices.update({orgUnitPath:ou, notes:note, annotatedUser:user, annotatedAssetId:asset, annotatedLocation:room},'my_customer',id);
logsheet.appendRow([new Date(), userEmail, serno, "Everything applied"+ " OU: "+ ou+ ", Note: "+ note+ ", User: "+ user+ ", Asset: "+ asset+ ", Location: "+ room]);
// If the update fails for some reason, log the error
} catch (err) {
logsheet.appendRow([serno, err]);
}
}
}
} else {
Logger.log('The user clicked "No" or the close button in the dialog\'s title bar.');
}
}
/**
Last edit: 20210418-1649 added confirmation prompt
*/