-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgsheetname_postinstall.sh
executable file
·49 lines (37 loc) · 1.89 KB
/
gsheetname_postinstall.sh
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
#!/bin/sh
## postinstall
# Long Google Sheet ID taken from the url of the sheet
# Like for
# https://docs.google.com/spreadsheets/d/1-_KZMQ_xK1T9jenzEbElefxKFjZD08TrEyQQw3j5YaQ/edit#gid=0
# The sheet id is 1-_KZMQ_xK1T9jenzEbElefxKFjZD08TrEyQQw3j5YaQ
# The Google Sheet needs to be readable like Anyone One with Link Can View
# Replace _ with the id of the Google sheet you have created
# WARNING do not put text in row 1 of the sheet as the VIS api will see this as lables and return both the label and value causing this script to fail!!
sheetID="_"
# The column in the GSheet with the serial number in it
serialCol="A"
# The column in the GSheet with the device name in it.
# Note we use column C so that a CSV file downloaded
# from Apple School/Business Manager can be imported/appended as new devices are purchased
# The ASM/ABM CSV uses column A for serial number and column B for device type
nameCol="C"
# get the device serial number
serialNumber=$(ioreg -c IOPlatformExpertDevice -d 2 | awk -F\" '/IOPlatformSerialNumber/{print $(NF-1)}')
# Look up serial number from Google Sheet using the Google Visualization api returning simple csv
dname=$(curl --silent "https://docs.google.com/spreadsheets/d/$sheetID/gviz/tq?tqx=out:csv&tq=%20select%20$nameCol%20WHERE%20$serialCol%20%3D%27$serialNumber%27")
# Remove the header information and quotes
dname=$(echo $dname | cut -d '"' -f 4)
#Test to see if no value is returned and use serial number instead
if test -z $dname ; then
dname=$serialNumber
fi
# If a device naming convention is used, consider adding code here to test for conformation
# For testing. Comment out for production
echo $dname
# Name the device using best value
# Uncomment these 4 lines to actually name the device, commented for safety
# scutil --set LocalHostName "$dname"
# scutil --set ComputerName "$dname"
# scutil --set HostName "$dname"
# dscacheutil -flushcache
exit 0 ## Success