Script to name computers to SerialNumber

ryan_peterson
New Contributor II

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
18 REPLIES 18

andrew_nicholas
Valued Contributor

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.

easyedc
Valued Contributor II

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

ammonsc
Contributor II

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"

CostaDelSol
New Contributor II

Thank you for sharing this script.

I also use after the scutil command this line at the end:

dscacheutil -flushcache

Have a nice day

donmontalvo
Esteemed Contributor II

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
--
https://donmontalvo.com

Scotty
Contributor

@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"

bmortens115
New Contributor III
New Contributor III

@ScottSimmons You could just run the script at enrollment complete (if you are just trying to name the machine as the serial number).

JasonAtCSUMB
New Contributor III

@easyedc

The easiest way to rename the boot disk is with renameVolume. Target the root / volume.

/usr/sbin/diskutil renameVolume / "${COMPUTERNAME}"

mickgrant
Contributor

Don't forget to run

jamf recon

at the end of your rename script so the new name gets reflected jamf database

csksea
New Contributor

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?

rmarin
New Contributor

did you ever find how to do this ?

 

jason_d
New Contributor III

@csksea check this link out.

jfishersr
New Contributor
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...

joethedsa
Contributor II

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".

andrew_nicholas
Valued Contributor

@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"

joethedsa
Contributor II

@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.

andrew_nicholas
Valued Contributor

A case statement is exactly what you're looking for.

alexknelson
New Contributor

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