I work at a school that issues Chromebooks to all students. I want to make an app to help manage and inventory devices in GSuite and automate the process of issuing new devices to students.
I want to be able to hand out the devices, ask the students to log in and either run a Chrome app or submit a Google Form that will run a script. The code needs to be able to find the serial number of the device the users are issued. Then it can do things like update the devices OU and annotated user in the device manager, email the student a receipt saying they have been issued device with a certain serial number etc.
The best I could do was write a script that goes through all the devices, ordered by lastsync, and checks if the most recent user of the device is the active user of the session. This assumes the device they most recently used is the one they were issued.
Alternatively I thought of creating a spreadsheet where the assigned serial numbers could be looked up for the user, but ideally this would be automated.
Is there any way to find the serial number of the device the student is logged into or running the script from. Any suggestions on how to automate device roll out and inventory?
Here is my code that finds the serial number of a user's most recently used device:
function main() {
var username = Session.getActiveUser().getEmail();
var deviceInfo = getUserDevice(username);
Logger.log("Device serial number: %s", deviceInfo[0]);
Logger.log("Device model number: %s", deviceInfo[1]);
}
// return serial and model number of user's most recently used device
function getUserDevice(username) {
var pageToken, page;
do {
page = AdminDirectory.Chromeosdevices.list(
'my_customer',
{
orderBy: 'lastSync',
maxResults: 100,
pageToken: pageToken
}
);
var found = false;
var devices = page.chromeosdevices;
if (devices) {
for (i in devices) {
var device = devices[i];
if (device.recentUsers && !found) {
var users = device.recentUsers;
var email = users[0].email;
if (email == username) {
var serial = device.serialNumber;
var model = device.model;
return [serial, model];
}
}
}
} else {
Logger.log('end of pages/no devices found.');
}
pageToken = page.nextPageToken;
} while (pageToken);
}