Naming MacBooks in a 1-TO-1 environment

delbrown
New Contributor II

Hi Folks,

I cobbled together the following script to allow standard users to name their assigned device. The script needs to be applied to a Self Service Item for it to work properly. (i.e. it's run as root). When run, it queries the student for their A. Initials and B. their student number. It then builds the name by combining the data with the device serial number using the JAMF Binary.

#!/bin/bash

# must be run as root on selected devices through a Self Service Item
# Script to query student for Initials and ID numbers and create
# a string in the format XX-111111-SERIALNUMBER
# then set the computer name to that string

# Get information from user
keyPress=`
/usr/bin/osascript <<EOT
tell application "Self Service"
activate
display dialog "Please enter your Initials in UPPERCASE LETTERS" default answer ""
set myInitials to text returned of result
display dialog "Please enter your Student ID Number" default answer ""
set myID to text returned of result
set computerName to myInitials&"-"&myID&"-"
end tell
EOT`

# Set Computer Name
jamf setComputerName -target / -useSerialNumber -prefix $keyPress
1 REPLY 1

McLeanSchool
New Contributor III

I like your script! Here's what I currently use:

It pulls the username of the user a computer is assigned to from JSS. I found this helpful since AirDrop gets used a lot, and it lets users clearly know who they are sending files to. If a user is not assigned to a computer in JSS, it looks up the serial number and uses that instead.

Everything is prepended with a model identifier: MBA for MacBook Air, MBP for MacBook Pro, and IMAC for iMacs.

If devices are joined to AD, you may run into issues with the device name being longer than 15 characters. I have a line in the code that attempts to cut the string down to 15 characters, but I don't think it works correctly. Any ideas or help with that would be appreciated :)

Don't forget to set the JSS variables to work with your JSS instance. Since the password is stored in the script, I created a new JSS user with very limited rights just for running this script.

#!/bin/sh

# JSS Variables
jssUser = username
jssPass = password
jssURL = https://jss.example.com:8443

SERIAL=$(ioreg -c IOPlatformExpertDevice -d 2 | awk -F" '/IOPlatformSerialNumber/{print $(NF-1)}')

USERINFO=$(curl -k ${jssURL}/JSSResource/computers/serialnumber/${SERIAL}/subset/location -H "Accept: application/xml" --user "${jssUser}:${jssPass}")
USERNAME=$(echo $USERINFO | /usr/bin/awk -F'<username>|</username>' '{print $2}' | tr [a-z] [A-Z])

DEVICEINFO=$(curl -k ${jssURL}/JSSResource/computers/serialnumber/${SERIAL}/subset/hardware -H "Accept: application/xml" --user "${jssUser}:${jssPass}")
MODEL=$(echo $DEVICEINFO | /usr/bin/awk -F'<model_identifier>|</model_identifier>' '{print $2}')

if [ -z "$USERNAME" ]
then
    USERNAME="$SERIAL"
fi

if echo "$MODEL" | grep -q "MacBookAir"
then
    PREFIX="MBA"
elif echo "$MODEL" | grep -q "MacBookPro"
then
    PREFIX="MBP"
elif echo "$MODEL" | grep -q "iMac"
then
    PREFIX="IMAC"
else
    echo "No model identifier found."
    PREFIX="MBA"
fi

COMPUTERNAME="${PREFIX}-${USERNAME}"

# Attempt to cut string down to 15 characters to satisfy AD name limits.  Not sure if this works correctly.
COMPUTERNAME=`echo ${COMPUTERNAME:0:15}`

echo "Setting computer name to $COMPUTERNAME"
/usr/sbin/scutil --set ComputerName "$COMPUTERNAME"
/usr/sbin/scutil --set LocalHostName "$COMPUTERNAME"
/usr/sbin/scutil --set HostName "$COMPUTERNAME"
dscacheutil -flushcache