Skip to content

Rename Computer using Scripting and Google Sheets

UPDATE: You need to allow redirects in your cURL by adding -L. See this post.

With Apple’s Device Enrollment program, when a organization-owned device first turns on, it checks in with Device Enrollment and gets the information to know what MDM is managing it and how to contact it. That MDM system can then install what’s known as a bootstrapping package.

In our old bootstrapping package, which was developed by an employee no longer here, was a giant if statement. If SerialNumber = x then set ComputerName to Bob, elseif SerialNumber = y then set ComputerName to Frank, elseif SerialNumber = z then set ComputerName to Jane. Pretty simple and straight forward, but a long list that is static and cannot be updated, without getting the package, rewriting the script to include new computers, repackage it and redeploy it. UGH!

I can’t find the original package. It’s in AirWatch, but sadly, I can’t find a “Download your package” button anywhere in there.

So I was going to rewrite it. Then I came across a page that talked about doing it from a spreadsheet. So I thought, well I can host a CSV file on a server somewhere and the computer can use the curl command to download it. Then I discovered that you can curl command to download a Google Sheet from the internet. At that point then I never have to change the package again1, I just have to update the Google Sheet.

I got into some trouble, and as usual, the killer community in the #toronto channel of the MacAdmins Slack are amazing and came to the rescue.

Specifically @Gerk, I had this weird awk command that wasn’t working. I also didn’t quite understand the command I had found in my Googling. He told me how he would do it, I quickly changed the line and tested it and it worked. I think googled how to parse $LINE variable to only have the value prior to the ,.

I think this works well. If anyone wants to improve on it, let me know. This is why we share knowledge in the MacAdmins community.

#!/bin/sh

# Get the current device's serial number
SERIAL="$(ioreg -l | grep IOPlatformSerialNumber | sed -e 's/.*\"\(.*\)\"/\1/')"

# Where the file will be saved using today's date. On date of writing would be /tmp/serials20190802.csv
OUTPUT=/tmp/serials$(date +%Y%m%d).csv

# Download the CSV from Google Drive, file must be set to Shared With Anyone with Link (or Shared with Anyone)
curl 'https://docs.google.com/spreadsheets/d/YOURGOOGLESHEETIDHERE/export?exportFormat=csv' -o $OUTPUT

# With much thanks to @Gerk and the rest of the crew on the MacAdmins #toronto channel, this now grabs the entire line from the CSV file 
LINE=$(grep $SERIAL $OUTPUT)

# This will grab all the text before the ,
ASSETTAG="$( cut -d ',' -f 1 <<< "$LINE" )"

# Set the ComputerName, HostName and LocalHostName
scutil --set ComputerName $ASSETTAG
scutil --set HostName $ASSETTAG
scutil --set LocalHostName $ASSETTAG
  1. Well, never say never []

2 thoughts on “Rename Computer using Scripting and Google Sheets”

  1. Pingback: Weekly News Summary for Admins — 2019-08 -30 – Cebu Scripts

  2. Pingback: MDM Migration for macOS | Never Had To Fight

Leave a Reply