You can also use the Jamf binary to do this in one go:
1#!/bin/sh
2/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:
1#!/bin/bash
2currentUser=$(ls -l /dev/console | cut -d " " -f 4)
3serialNumber=$(system_profiler SPHardwareDataType | awk '/Serial/ {print $4}')
4headCriteria="THREELETTERACRONYMOFYOURCHOICEGOESHERE"
5#Prompt for Tag and sanitize
6assetTag=$(sudo -u $currentUser -H osascript -e 'display dialog "Please enter this machines asset tag:" default answer "" buttons {"Submit"} default button "Submit"')
7assetTagSanitized=$(echo "$assetTag" | awk 'BEGIN { FS = ":" } ; {print toupper ($3)}' | tr -d '[:space:]' | cut -c1-15)
8assetTagCheckHead=$(echo "$assetTagSanitized" | cut -c1-3)
9assetTagCheckTail=$(echo "$assetTagSanitized" | cut -c4-15)
10
11#If the provided name is not entered or it is not in our format, just use the machines Serial Number
12if [[ "$assetTagSanitized" = "" || ! "$assetTagCheckHead" = "$headCriteria" || ! "$assetTagCheckTail" -gt 0 ]]; then
13 assetTagSanitized="$serialNumber"
14fi
15
16/usr/local/bin/jamf setComputerName -name "$assetTagSanitized"
17
18#let it sleep so as to ensure the system has enough time to propagate the name change.
19/bin/sleep 15
This isn't perfect but it gives us a decent base line and then we mop up any stragglers.
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.
1#!/bin/bash
2
3base="MK"
4serial=`/usr/sbin/system_profiler SPHardwareDataType | /usr/bin/awk '/Serial Number (system)/ {print $NF}'`
5/usr/sbin/scutil --set ComputerName "${base}${serial}"
6/usr/sbin/scutil --set LocalHostName "${base}${serial}"
7/usr/sbin/scutil --set HostName "${base}${serial}"
8
9APFS=`/usr/sbin/diskutil list | grep "APFS"`
10Apple_HFS=`/usr/sbin/diskutil list | grep "Apple_HFS"`
11Fusion=`/usr/sbin/diskutil list | grep "virtual"`
12
13if [ "$APFS" != "" ]; then
14 diskutil rename disk1s1 "${base}${serial}"
15elif [ "$Apple_HFS" != "" ]; then
16diskutil rename disk0s2 "${base}${serial}"
17elif [ "$Fusion" != "" ]; then
18diskutil rename disk2 "${base}${serial}"
19fi
We had one to allow the IT department or user change the name from Self Service.
1#!/bin/bash
2
3NewComputerName="$(osascript -e 'Tell application "System Events" to display dialog "Enter the Asset ID Number:" default answer ""' -e 'text returned of result' 2>/dev/null)"
4
5jamf setComputerName -name "$NewComputerName"
6
7jamf recon -assetTag "$NewComputerName"
Thank you for sharing this script.
I also use after the scutil command this line at the end:
1dscacheutil -flushcache
Have a nice day
This came up today, we're using ioreg
which is a bit faster than parsing system_profiler
:
1#!/bin/sh
2
3serialNumber=$( ioreg -c IOPlatformExpertDevice -d 2 | awk -F" '/IOPlatformSerialNumber/{print $(NF-1)}' )
4
5if [[ ${serialNumber} != "" ]]; then
6 echo "Serial Number is $serialNumber, setting ComputerName/LocalHostName/Hostname."
7 /usr/sbin/scutil --set ComputerName ${serialNumber}
8 /usr/sbin/scutil --set LocalHostName ${serialNumber}
9 /usr/sbin/scutil --set HostName ${serialNumber}
10else
11 echo "Serial Number is blank"
12fi
13
14exit 0
@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"
@ScottSimmons You could just run the script at enrollment complete (if you are just trying to name the machine as the serial number).
@easyedc
The easiest way to rename the boot disk is with renameVolume. Target the root / volume.
1/usr/sbin/diskutil renameVolume / "${COMPUTERNAME}"
Don't forget to run
1jamf recon
at the end of your rename script so the new name gets reflected jamf database
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?
@csksea check this link out.
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...
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".
@joethedsa You could use something like the below.
1#!/bin/bash
2model=$(system_profiler SPHardwareDataType | grep "Model Name" | awk -F ":" '{ sub(" ",""); print $2}' | tr -d ' ')
3sn=$(system_profiler SPHardwareDataType | awk '/Serial/ {print $4}')
4assetTag="$model-$sn"
@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.
A case statement is exactly what you're looking for.
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.
1#!/bin/bash
2
3## Laptop or Desktop
4TYPE=$(system_profiler SPHardwareDataType | grep -i "MacBook" | wc -l)
5[[ $TYPE -gt 0 ]] && T="L" || T="D"
6
7## Get the Serial Number
8SERIAL=$(system_profiler SPHardwareDataType | grep -i Serial | grep -i system | awk '{print $NF}' | cut -c5-)
9
10## Put it all together now...
11NEWNAME="${T}${SERIAL}"
12
13## Name the computers
14scutil --set ComputerName "$NEWNAME"
15scutil --set HostName "$NEWNAME"
16scutil --set LocalHostName "$NEWNAME"
17
18## Create dummy receipt to mark complete
19touch /Library/Receipts/com.company.renameComplete.bom
20
21## Update Inventory
22/usr/local/bin/jamf recon
csksea wrote:
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?
did you ever find how to do this ?
alexknelson wrote:
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.
1#!/bin/bash
2
3## Laptop or Desktop
4TYPE=$(system_profiler SPHardwareDataType | grep -i "MacBook" | wc -l)
5[[ $TYPE -gt 0 ]] && T="L" || T="D"
6
7## Get the Serial Number
8SERIAL=$(system_profiler SPHardwareDataType | grep -i Serial | grep -i system | awk '{print $NF}' | cut -c5-)
9
10## Put it all together now...
11NEWNAME="${T}${SERIAL}"
12
13## Name the computers
14scutil --set ComputerName "$NEWNAME"
15scutil --set HostName "$NEWNAME"
16scutil --set LocalHostName "$NEWNAME"
17
18## Create dummy receipt to mark complete
19touch /Library/Receipts/com.company.renameComplete.bom
20
21## Update Inventory
22/usr/local/bin/jamf recon
I want set hostname like LP-machine serial no.
any suggestion
sgrewal wrote:
I want set hostname like LP-machine serial no.
any suggestion
/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}'
# 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