Skip to main content
Question

Script to name computers to SerialNumber


ryan_peterson
Forum|alt.badge.img+7

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

21 replies

Forum|alt.badge.img+13

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
Forum|alt.badge.img+16
  • Esteemed Contributor
  • 623 replies
  • February 6, 2018

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

Forum|alt.badge.img+10
  • Contributor
  • 126 replies
  • February 6, 2018

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"

Forum|alt.badge.img+1
  • New Contributor
  • 3 replies
  • February 6, 2018

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
Forum|alt.badge.img+36
  • Legendary Contributor
  • 4293 replies
  • October 4, 2018

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

Forum|alt.badge.img+6
  • Contributor
  • 49 replies
  • April 10, 2019

@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
Forum|alt.badge.img+14
  • Contributor
  • 33 replies
  • April 10, 2019

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


JasonAtCSUMB
Forum|alt.badge.img+6
  • Contributor
  • 53 replies
  • April 20, 2019

@easyedc

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

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

mickgrant
Forum|alt.badge.img+12
  • Contributor
  • 140 replies
  • April 21, 2019

Don't forget to run

jamf recon

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


Forum|alt.badge.img
  • New Contributor
  • 1 reply
  • September 13, 2019

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?


Forum|alt.badge.img+6
  • Contributor
  • 19 replies
  • September 13, 2019

@csksea check this link out.


Forum|alt.badge.img
  • New Contributor
  • 1 reply
  • September 23, 2019
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...


Forum|alt.badge.img+10
  • Valued Contributor
  • 108 replies
  • April 7, 2020

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


Forum|alt.badge.img+13

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

Forum|alt.badge.img+10
  • Valued Contributor
  • 108 replies
  • April 7, 2020

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


Forum|alt.badge.img+13

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


Forum|alt.badge.img+3
  • New Contributor
  • 4 replies
  • August 17, 2020

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

Forum|alt.badge.img
  • New Contributor
  • 1 reply
  • August 10, 2021
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 ?

 


Forum|alt.badge.img
  • New Contributor
  • 1 reply
  • June 24, 2022
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.

#!/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

I want set hostname like LP-machine serial no. 

any suggestion


JasonAtCSUMB
Forum|alt.badge.img+6
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}'

 


Forum|alt.badge.img

# 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


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings