0

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);
}
jembe
  • 61
  • 7
  • I question your assumption that the Chromebook used most recently by the student is the one they are checking back in. What if there was a hardware issue and the student logged into their friend's device to send an email / complete a project, then brings the broken device back to the check-in location? Your code will bill them for a device they did not use. – tehhowch May 01 '18 at 15:24
  • I was aware of this flaw, that's why I mentioned the assumption and asked for suggestions of a better way. The program I want to create is to help automate the process of issuing hundreds of new devices to students. Ideally there would be a way of finding the serial number of the device the student is running the script from. So you would hand out the devices, ask the students to run the app and the serial number would be assigned in the inventory to the student who's logged in. It could also be used to audit the inventory of the Chromebooks that have been issued in years past. – jembe May 01 '18 at 15:43
  • Your stated question is not clear re: what you are seeking to implement in an alternative manner (it also doesn't use `found`). I think the first change you'd want to make is to parse all your devices, rather than stop once you get the first hit, and return an array of `[serial, model]` entries. Depending on how many devices you're dealing with, you may want to add a query to only page through certain devices (e.g. only provisioned ones, etc). https://support.google.com/chrome/a/answer/1698333?hl=en https://developers.google.com/admin-sdk/directory/v1/reference/chromeosdevices/list – tehhowch May 01 '18 at 15:55
  • Thanks @tehhowch, I have updated my question with a more fleshed out idea of the program I want to create. I realize my code example doesn't use `var found` and it sort of awkwardly returns `[serial, model]`. Consider it a quick sketch to show what I have done in an attempt to get close to the functionality I really want. I do like your suggestion of adding a query to only page through certain devices. If I end up making a Google Form with an embedded script I could query devices based on what the user fills out in the form. – jembe May 01 '18 at 17:30

0 Answers0