Posted on 02-05-2018 09:56 PM
This post is for people who are looking to name their computers to it's serial number. The script can be run with a policy at recurring check-in. Obviously there are multiple ways to set the computer name and the jamf binary can do it as well.
If anyone wants to add in the comments what they use for naming computers, I'm sure fellow Jamf Admin's would not mind.
#!/usr/bin/env bash
# Get the Serial Number of the Machine
sn=$(system_profiler SPHardwareDataType | awk '/Serial/ {print $4}')
# Set the ComputerName, HostName and LocalHostName
scutil --set ComputerName $sn
scutil --set HostName $sn
scutil --set LocalHostName $sn
Posted on 02-06-2018 05:37 AM
You can also use the Jamf binary to do this in one go:
#!/bin/sh
/usr/local/bin/jamf setComputerName -useSerialNumber
To add to the discussion, this is our post DEP method of prompting for the machine name during setup:
#!/bin/bash
currentUser=$(ls -l /dev/console | cut -d " " -f 4)
serialNumber=$(system_profiler SPHardwareDataType | awk '/Serial/ {print $4}')
headCriteria="THREELETTERACRONYMOFYOURCHOICEGOESHERE"
#Prompt for Tag and sanitize
assetTag=$(sudo -u $currentUser -H osascript -e 'display dialog "Please enter this machines asset tag:" default answer "" buttons {"Submit"} default button "Submit"')
assetTagSanitized=$(echo "$assetTag" | awk 'BEGIN { FS = ":" } ; {print toupper ($3)}' | tr -d '[:space:]' | cut -c1-15)
assetTagCheckHead=$(echo "$assetTagSanitized" | cut -c1-3)
assetTagCheckTail=$(echo "$assetTagSanitized" | cut -c4-15)
#If the provided name is not entered or it is not in our format, just use the machines Serial Number
if [[ "$assetTagSanitized" = "" || ! "$assetTagCheckHead" = "$headCriteria" || ! "$assetTagCheckTail" -gt 0 ]]; then
assetTagSanitized="$serialNumber"
fi
/usr/local/bin/jamf setComputerName -name "$assetTagSanitized"
#let it sleep so as to ensure the system has enough time to propagate the name change.
/bin/sleep 15
This isn't perfect but it gives us a decent base line and then we mop up any stragglers.
Posted on 02-06-2018 05:50 AM
We also change the name of the mounted Hard Drive. The MK is for Mac workstation (we call Windows OS WK...). It's a little ugly, I get, but it seems to handle where various file and storage formats, (fusion, APFS, JFHS, etc). It has started to fail on me recently with fusion drives, need to figure that out.
#!/bin/bash
base="MK"
serial=`/usr/sbin/system_profiler SPHardwareDataType | /usr/bin/awk '/Serial Number (system)/ {print $NF}'`
/usr/sbin/scutil --set ComputerName "${base}${serial}"
/usr/sbin/scutil --set LocalHostName "${base}${serial}"
/usr/sbin/scutil --set HostName "${base}${serial}"
APFS=`/usr/sbin/diskutil list | grep "APFS"`
Apple_HFS=`/usr/sbin/diskutil list | grep "Apple_HFS"`
Fusion=`/usr/sbin/diskutil list | grep "virtual"`
if [ "$APFS" != "" ]; then
diskutil rename disk1s1 "${base}${serial}"
elif [ "$Apple_HFS" != "" ]; then
diskutil rename disk0s2 "${base}${serial}"
elif [ "$Fusion" != "" ]; then
diskutil rename disk2 "${base}${serial}"
fi
Posted on 02-06-2018 05:58 AM
We had one to allow the IT department or user change the name from Self Service.
#!/bin/bash
NewComputerName="$(osascript -e 'Tell application "System Events" to display dialog "Enter the Asset ID Number:" default answer ""' -e 'text returned of result' 2>/dev/null)"
jamf setComputerName -name "$NewComputerName"
jamf recon -assetTag "$NewComputerName"
Posted on 02-06-2018 03:03 PM
Thank you for sharing this script.
I also use after the scutil command this line at the end:
dscacheutil -flushcache
Have a nice day
Posted on 10-03-2018 06:35 PM
This came up today, we're using ioreg
which is a bit faster than parsing system_profiler
:
#!/bin/sh
serialNumber=$( ioreg -c IOPlatformExpertDevice -d 2 | awk -F" '/IOPlatformSerialNumber/{print $(NF-1)}' )
if [[ ${serialNumber} != "" ]]; then
echo "Serial Number is $serialNumber, setting ComputerName/LocalHostName/Hostname."
/usr/sbin/scutil --set ComputerName ${serialNumber}
/usr/sbin/scutil --set LocalHostName ${serialNumber}
/usr/sbin/scutil --set HostName ${serialNumber}
else
echo "Serial Number is blank"
fi
exit 0
Posted on 04-10-2019 11:34 AM
@andrew.nicholas what trigger do you use? Id like to streamline the computer naming during DEP if possible. Right now the techs use a self service script to bind and set the name, but if I would set it to serial before all that it would make the "imaged and ready to use" machines not sit there with "somebody's macbook pro"
Posted on 04-10-2019 12:28 PM
@ScottSimmons You could just run the script at enrollment complete (if you are just trying to name the machine as the serial number).
Posted on 04-19-2019 07:02 PM
@easyedc
The easiest way to rename the boot disk is with renameVolume. Target the root / volume.
/usr/sbin/diskutil renameVolume / "${COMPUTERNAME}"
Posted on 04-21-2019 03:23 PM
Don't forget to run
jamf recon
at the end of your rename script so the new name gets reflected jamf database
Posted on 09-13-2019 06:14 AM
How would i go about scripting something with this logic?
if serial number is "123456" then change host name to "computer1"
this would be helpful for managing a mac lab where im constantly deploying system images to large numbers of computers. any suggestions?
Posted on 08-09-2021 07:12 PM
did you ever find how to do this ?
Posted on 09-13-2019 06:53 AM
Posted on 09-23-2019 12:33 PM
How would i go about scripting something with this logic? if serial number is "123456" then change host name to "computer1"
First, export the Jamf computer list in .csv
oh wait...
Posted on 04-07-2020 06:54 AM
Does anyone know what would have to change in a script if we wanted to append the serial number to a naming convention? We currently name our computers (Mac and PC's) with model-asset tag (for example "iMac-123456" or "mbp-123456"). Is there a way to keep the first part and append the serial number at the end? For example "iMac-SerialNumber".
Posted on 04-07-2020 08:01 AM
@joethedsa You could use something like the below.
#!/bin/bash
model=$(system_profiler SPHardwareDataType | grep "Model Name" | awk -F ":" '{ sub(" ",""); print $2}' | tr -d ' ')
sn=$(system_profiler SPHardwareDataType | awk '/Serial/ {print $4}')
assetTag="$model-$sn"
Posted on 04-07-2020 08:36 AM
@andrew.nicholas , thanks for the response. Since our environment has different models (iMacs, MacBook Pro, MacBook Air, MacMini), can you think of a way to add some smarts to the script where if it finds a particular text string like "iMac" it would then prefix the name with something predetermined? For example, if the command has identified an iMac, it would prefix it "im" so the rename of the computer would be something like "im-serialnumber" or if it identified a MacBook, it would name it "mb-serialnumber", etc.
Alternatively maybe there could be some interaction with the renaming of it so a policy would run, a dialogue window (oascript?) would display for text input. The person would type "im" or "mb" or "mini" etc. This text input would be the suffix to the rest of the computer name.
Posted on 04-07-2020 08:50 AM
A case statement is exactly what you're looking for.
Posted on 08-17-2020 09:41 AM
Below is what we're using...thanks to one of the fellow members here. L or D for laptop or desktop and then 8 characters from the serial number.
#!/bin/bash
## Laptop or Desktop
TYPE=$(system_profiler SPHardwareDataType | grep -i "MacBook" | wc -l)
[[ $TYPE -gt 0 ]] && T="L" || T="D"
## Get the Serial Number
SERIAL=$(system_profiler SPHardwareDataType | grep -i Serial | grep -i system | awk '{print $NF}' | cut -c5-)
## Put it all together now...
NEWNAME="${T}${SERIAL}"
## Name the computers
scutil --set ComputerName "$NEWNAME"
scutil --set HostName "$NEWNAME"
scutil --set LocalHostName "$NEWNAME"
## Create dummy receipt to mark complete
touch /Library/Receipts/com.company.renameComplete.bom
## Update Inventory
/usr/local/bin/jamf recon
Posted on 06-24-2022 11:37 AM
I want set hostname like LP-machine serial no.
any suggestion
Posted on 06-24-2022 03:04 PM
/usr/local/bin/jamf setComputerName "$NEWNAME"
The Jamf binary handles renaming the computer and localhostname in one command.
You can set your NEWNAME variable per the examples above.
For serial number, I tend to prefer
system_profiler SPHardwareDataType | awk '/Serial/ {print $4}'
Posted on 12-06-2022 02:35 AM
# Set basic variables
osversion=$(sw_vers -productVersion)
serial=$(ioreg -rd1 -c IOPlatformExpertDevice | awk -F'"' '/IOPlatformSerialNumber/{print $4}')
#Setup Computer name
base=SYS-
serial="$(ioreg -l | grep IOPlatformSerialNumber | sed -e 's/.*\"\(.*\)\"/\1/')"
/usr/sbin/scutil --set ComputerName $base$serial
/usr/sbin/scutil --set LocalHostName $base$serial
/usr/sbin/scutil --set HostName $base$serial
sudo jamf recon