-
Notifications
You must be signed in to change notification settings - Fork 3
/
devices.gs
402 lines (317 loc) · 9.92 KB
/
devices.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
function syncEverything() {
// Get the current spreadsheet data
var sh = getDevicesSheet();
var devices = getJsonForSheet(sh);
for ( var serial in devices ) {
updateDevicesAndSettings(devices[serial]);
}
}
function updateDevicesAndSettings(data) {
var udid = data["UDID"] || data["udid"];
var serial = data["SerialNumber"] || data["serial_number"];
var sh = getDevicesSheet();
// Get Updated Device Info
var devices = getFullDeviceInfo([serial]);
devices[serial]["udid"] = udid;
var date = new Date();
devices[serial]["last_seen"] = date;
// Get the current spreadsheet data
var current = getJsonForSheet(sh);
var serials = Object.keys(current);
// Combine spreadsheet data with updated device info
devices = combineJsonData(current, devices);
// Set the header values
setHeader(sh, devices);
var mapped = getMappedHeader();
var header = mapped[1];
var range = sh.getRange(sh.getFrozenRows() - 1, 1, 2, header.length);
range.setValues(mapped);
range.setFontWeight("bold");
// Map the data to the spreadsheet
var rows = [];
for ( var device in devices ) {
// Map the row for the device and add it to the list
var row = header.map(function(key){return devices[device][key]});
for ( var j=0; j<row.length; j++ ) {
if ( !row[j] ) {
row[j] = "";
}
}
// Add new items to the top of the list
if ( serials.indexOf(device) == -1 ) {
rows.unshift(row);
} else {
rows.push(row);
}
}
// Set the spreadsheet values
var range = sh.getRange(sh.getFrozenRows() + 1, 1, rows.length, header.length);
range.setValues(rows);
// Delete Empty Rows If Needed
var maxRow = sh.getMaxRows();
var lastRow = sh.getLastRow();
if ( maxRow != lastRow ) {
sh.deleteRows(lastRow+1, maxRow-lastRow);
}
// Update column widths
formatColumnsForSheet(sh);
}
function getFullDeviceInfo(serials) {
// Get Full Device Info (combines data from MicroMDM with the data from Apple)
//var devs = getDevices(serials);
var devs = {};
var deps = getDEPDevices(serials);
var devices = combineJsonData(devs, deps);
for ( var device in devices ) {
for ( var item in devices[device] ) {
if ( devices[device][item] == "" ) {
delete devices[device][item];
}
}
// Add assigned profile information
devices[device] = addAssignedProfile(devices[device]);
}
return devices;
}
function addAssignedProfile(device) {
if ( device.profile_uuid ) {
var profile = sendGetDepProfiles(device.profile_uuid);
if ( profile ) {
device.assigned_profile = profile.profile_name;
}
} else {
device.assigned_profile = "";
}
return device;
}
function sendGetDepProfiles(uuid) {
var data = {
"uuid" : uuid
};
var url = SERVER_URL + '/v1/dep/profiles';
var headers = {
"Authorization" : "Basic " + Utilities.base64Encode(USER_NAME + ':' + API_PASSWORD)
};
var options = {
"method" : "POST",
"contentType" : "application/json",
"headers" : headers,
"payload" : JSON.stringify(data)
};
var response = JSON.parse(UrlFetchApp.fetch(url, options));
return response;
}
function formatColumnsForSheet(sheet) {
// Left Justify
var range = sheet.getRange(sheet.getFrozenRows() + 1, 1, sheet.getLastRow() - sheet.getFrozenRows(), sheet.getLastColumn());
range.setHorizontalAlignment("left");
// Update column widths
var header = sheet.getRange(sheet.getFrozenRows(), 1, 1, sheet.getLastColumn()).getValues()[0];
for ( var i=1; i<sheet.getLastColumn()+1; i++ ) {
sheet.autoResizeColumn(i);
if ( header[i-1] == "assigned_profile" ) {
sheet.setColumnWidth(i, sheet.getColumnWidth(i) + 20)
}
}
}
function getDevicesSheet() {
var ss = SpreadsheetApp.getActive();
var name = "Devices and Settings";
var sh = ss.getSheetByName(name);
if ( !sh ) {
// Create Devices and Settings Sheet
sh = ss.insertSheet(name, 0);
// Remove Empty Initial Sheet if Needed
var sheet1 = ss.getSheetByName('Sheet1');
if ( sheet1 ) {
if ( sheet1.getLastColumn() + sheet1.getLastRow() == 0 ) {
ss.deleteSheet(sheet1);
}
}
}
if ( !sh.getLastColumn() ) {
var header = getMappedHeader();
var range = sh.getRange(1, 1, header.length, header[0].length);
range.setValues(header);
sh.setFrozenRows(header.length);
sh.getRange(1, 1, header.length, sh.getLastColumn()).setFontWeight("bold");
// Hide Key Row
sh.hideRows(header.length);
}
return sh;
}
function setHeader(sh, devices) {
var keys = [];
for ( var device in devices ) {
keys = keys.concat(Object.keys(devices[device]));
}
keys = keys.filter( onlyUnique );
var header = sh.getRange(sh.getFrozenRows(), 1, 1, sh.getLastColumn()).getValues()[0];
header = header.concat(keys).filter ( onlyUnique );
var titles = sh.getRange(sh.getFrozenRows() - 1, 1, 1, sh.getLastColumn()).getValues()[0];
while ( header.length != titles.length ) {
if ( header.length < titles.length ) {
header.push("");
}
if ( titles.length < header.length ) {
titles.push("");
}
}
setMappedHeader(titles, header);
}
function onlyUnique(value, index, self) {
return self.indexOf(value) === index;
}
function setMappedHeader(titles, header) {
var map = {};
for ( var i=0; i<header.length; i++ ) {
map[header[i]] = titles[i];
}
var properties = PropertiesService.getScriptProperties();
properties.setProperty('HEADER', JSON.stringify(map));
}
function getMappedHeader() {
var properties = PropertiesService.getScriptProperties();
var map = properties.getProperty('HEADER');
if ( map ) {
map = JSON.parse(map);
} else {
map = {
"serial_number": "Serial",
"asset_tag": "Asset Tag",
"assigned_profile": "DEP Profile",
"model": "Model",
"profile_status": "Profile Status",
"udid": "Device UDID",
"last_seen": "Last Seen",
"profile_uuid": "Profile UUID",
"profile_assign_time": "Profile Assign Time",
"profile_push_time": "Profile Push Time",
"device_assigned_date": "Device Assigned Date",
"device_assigned_by": "Device Assigned By",
"description": "Device Description",
"color": "Device Color",
"os": "OS",
"device_family": "Device Family",
"op_date": "Purchase Date"
};
}
var titles = [];
for ( var item in map ) {
titles.push(map[item]);
}
var header = [titles,Object.keys(map)];
return header;
}
function combineJsonData(first, second) {
for ( var item in second ) {
if ( first[second[item].serial_number] ) {
var row = first[second[item].serial_number];
} else {
var row = second[item];
}
if ( row ) {
for ( var info in second[item] ) {
row[info] = second[item][info];
}
first[second[item].serial_number] = row;
}
}
return first;
}
function getJsonForSheet(sheet) {
var keys = sheet.getRange(sheet.getFrozenRows(), 1, 1, sheet.getLastColumn()).getValues()[0];
var data = sheet.getRange(sheet.getFrozenRows()+1, 1, sheet.getLastRow(), sheet.getLastColumn()).getValues();
var rows = {};
for ( var i=0; i<data.length; i++ ) {
var json = {};
for ( var j=0; j<keys.length; j++ ) {
json[keys[j]] = data[i][j];
}
if ( json.serial_number ) {
rows[json.serial_number] = json;
}
}
return rows;
}
function formatDate(string) {
var date = string.split(".")[0];
if ( string.split(".")[1] ) {
date = date + 'Z';
}
if ( date == "0001-01-01T00:00:00Z" ) {
return "";
} else {
date = new Date(date);
}
if ( Utilities.formatDate(date, 'Etc/GMT', 'yyyy-MM-dd\'T\'HH:mm:ss.SSS\'Z\'') != "1970-01-01T00:00:00.000Z" ) {
return Utilities.formatDate(date, 'America/Chicago', 'yyyy, dd MMMM - HH:mm:ss');
}
}
function getDEPDevices(serials) {
// Sync DEP Devices from Apple and Wait For Response
depSyncNow();
Utilities.sleep(5000);
// Get the Full List of Serials if None Specified
if ( !serials ) {
var serials = Object.keys(getDevices());
}
// Get DEP Device Information from Apple
var devices = sendGetDEPDevices(serials);
if ( !devices ) {
return {};
}
return formatDepResponse(devices);
}
function depSyncNow() {
var data = {};
var url = SERVER_URL + '/v1/dep/syncnow';
var headers = {
"Authorization" : "Basic " + Utilities.base64Encode(USER_NAME + ':' + API_PASSWORD)
};
var options = {
"method" : "POST",
"contentType" : "application/json",
"headers" : headers,
"payload" : JSON.stringify(data)
};
UrlFetchApp.fetch(url, options);
}
function testSendGetDEPDevices() {
Logger.log(sendGetDEPDevices(["C02JDFA1DTY3"]));
}
function sendGetDEPDevices(serials) {
var data = {
"serials" : serials
};
var url = SERVER_URL + '/v1/dep/devices';
var headers = {
"Authorization" : "Basic " + Utilities.base64Encode(USER_NAME + ':' + API_PASSWORD)
};
var options = {
"method" : "POST",
"contentType" : "application/json",
"headers" : headers,
"payload" : JSON.stringify(data)
};
var response = JSON.parse(UrlFetchApp.fetch(url, options));
var devices = response.devices;
return devices;
}
function formatDepResponse(devices) {
for ( var device in devices ) {
if ( devices[device].profile_status == "" ) {
devices[device].serial_number = device;
devices[device].profile_status = "DEP REMOVED";
devices[device].device_assigned_by = "";
}
devices[device].profile_assign_time = formatDate(devices[device].profile_assign_time);
devices[device].profile_push_time = formatDate(devices[device].profile_push_time);
devices[device].device_assigned_date = formatDate(devices[device].device_assigned_date);
devices[device].op_date = formatDate(devices[device].op_date);
if ( !devices[device].profile_uuid ) {
devices[device].profile_uuid = "";
}
}
return devices;
}