Skip to content

Rename Computer using Scripting and Google Sheets

Google has made some changes that made this script not work, but it was an easy fix, once I bothered to look at why it wasn’t working.

The page redirects to another page, and thus the cURL doesn’t work as usual. Instead you need to add the redirect flag to the cURL command so, instead of:

curl 'https://docs.google.com/spreadsheets/d/YOURGOOGLESHEETIDHERE/export?exportFormat=csv' -o $OUTPUT

You want:

curl -L 'https://docs.google.com/spreadsheets/d/YOURGOOGLESHEETIDHERE/export?exportFormat=csv' -o $OUTPUT

So the full script would be:

#!/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 -L '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 thought on “Rename Computer using Scripting and Google Sheets”

  1. Pingback: Rename Computer using Scripting and Google Sheets | Never Had To Fight

Leave a Reply