-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathRemotePowerwash.gs
54 lines (51 loc) · 2.45 KB
/
RemotePowerwash.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
function powerWash() {
// 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: Remote Powerwash','Do you really want to powerwash these devices?', ui.ButtonSet.YES_NO);
// Process the user's response.
if (response == ui.Button.YES) {
Logger.log('Ok, powerwashing 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)
var serno = list[i][0];
// 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.Customer.Devices.Chromeos.issueCommand({"commandType": "REMOTE_POWERWASH"},'my_customer',id);
logsheet.appendRow([new Date(), userEmail, serno, "Device power washed"]);
// 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: 20220201-1806 Removed unnecessary log lines
*/