Change all machine names to serial number

rpayne
Contributor II

We have a need to rename all managed Macs to their serial number. Is there an easy way to automate this? I'm not finding anything.

4 REPLIES 4

bmortens115
New Contributor III
New Contributor III

Give this a shot. I have this running at enrollment complete for my devices, but you could just run this once per computer.

#!/bin/sh

#get serial number
serial=$(system_profiler SPHardwareDataType | awk '/Serial/ {print $4}')

# Set Hostname using variable created above
echo "Setting computer name to $serial locally..."
scutil --set HostName "$serial"
scutil --set LocalHostName "$serial"
scutil --set ComputerName "$serial"


exit 0

rderewianko
Valued Contributor II

What you want todo is run a script that does this:

#!/bin/bash

##get serial number
computerName=$(ioreg -c IOPlatformExpertDevice -d 2 | awk -F" '/IOPlatformSerialNumber/{print $(NF-1)}')

#set the host name on several places

/usr/local/bin/jamf setComputerName -name "$computerName"
/usr/sbin/scutil --set LocalHostName "$computerName"
/usr/sbin/scutil --set HostName "$computerName"
/usr/sbin/scutil --set ComputerName "$computerName"
/usr/bin/defaults write /Library/Preferences/SystemConfiguration/com.apple.smb.server NetBIOSName "$computerName"

/usr/local/bin/jamf recon
exit 0

rpayne
Contributor II

Thanks guys. Is there a way to do in Jamf Pro and then utilize the "enforce machine name in JAMF?"

rderewianko
Valued Contributor II

You would use the scripts above as part of your policy in Jamf.