You could try something like this.
#!/bin/sh
export LOCALMACHINENAME=$(ls -l /dev/console | cut -d " " -f4)
/usr/sbin/networksetup -setcomputername "$LOCALMACHINENAME"
It should name the computer to match the username of whoever is logged in when it runs.
The username is my fall back. I found a script earlier that could do that. If we were using a directory for authentication that would be the easiest way to do it. With our roll-out though it is pretty much completely un-tethered, all local logins with no directory binding. So the students can technically create whatever they want for a username when they setup the devices. I'll have a printout for them with step by step instructions with their username and passwords so they can mirror their SIS and Canvas credentials but there is nothing stopping them from typing something different.
It looks like I can use the API, search by serial number, parse the json and return the username record.
@bbelew If you write something for it, I'd be interested in seeing it.
Completely untested but I think this will work. I'll test it when I get to a computer that is tied to JSS. It needs a conditional added to it. If real_name is blank, set computer name to serial number or something. Assuming the script runs before a user is associated with the computer in JSS it would return blank. Mac OS probably won't like a blank computer name and hostname.
#!/bin/sh
jssUser=apiusername
jssPass='apipassword'
jssHost=https://jss.address.org:8443/
serial=$(ioreg -c IOPlatformExpertDevice -d 2 | awk -F" '/IOPlatformSerialNumber/{print $(NF-1)}')
response=$(curl -k ${jssHost}/JSSResource/computers/serialnumber/${serial}/subset/location --user "${jssUser}:${jssPass}")
real_name=$(echo $response | /usr/bin/awk -F'<real_name>|</real_name>' '{print $2}');
/usr/sbin/scutil --set ComputerName $real_name
/usr/sbin/scutil --set HostName $real_name
@bbelew You might want to shorten the serial number that returns, especially if in the future the computer gets bound to AD as there is a limit in the number of characters that can be used in a name (15).
We use this to extract the last seven characters of the serial number:
lastCharsInSerialNum=$(system_profiler SPHardwareDataType | awk '/Serial/ {print $4}' | grep -o '.{7}$')
@dmw3 The retrieval of the serial is just used to query the API to return the Real_Name of the user record associated with the computer in JSS. Now Real_Name could be over 15 characters, so it might need to be trimmed. Or i'd just modify the script to pull username instead. Which at least in my environment i've never seen one 15 characters long. My reasoning for wanting the script is mostly for our content filter. So I can see who is searching without requiring them to sign into the filter or having to search JSS to find the username associated with a SN or Mac Address.
Working script.
#!/bin/sh
jssUser=apiuser
jssPass='apipassword'
serial=$(ioreg -c IOPlatformExpertDevice -d 2 | awk -F" '/IOPlatformSerialNumber/{print $(NF-1)}')
response=$(curl -k https://jss.domain.org:8443/JSSResource/computers/serialnumber/${serial}/subset/location --user "${jssUser}:${jssPass}")
real_name=$(echo $response | /usr/bin/awk -F'<real_name>|</real_name>' '{print $2}');
user_name=$(echo $response | /usr/bin/awk -F'<username>|</username>' '{print $2}');
/usr/sbin/scutil --set ComputerName "${real_name}"
/usr/sbin/scutil --set LocalHostName ${user_name}
dscacheutil -flushcache
Will this also work for mobile devices?
No. iOS devices running iOS8 you can name, and force name from JSS.
I get the following output as the computer name
?xml version="1.0" encoding="UTF-8"?><computer><location><username>mmurillo</username><real_name/><email_address/><position/><phone/><department/>
How do I just extract the user name?