SCRIPT | Management-Rename-Mac

ByDan Ashley

SCRIPT | Management-Rename-Mac

Rename Macs

Summary

This script will enforce naming Macs based upon their serial numbers.

In addition to using the serial number to form the bulk of the naming convention, the script will also determine wether a laptop or desktop Mac is being renamed. If the computer being renamed is a laptop then the serial number will be prefixed with “ML-” representing “Mac Laptop“. If the computer is determined to be a desktop computer then the serial number will be prefixed with “MD-” to signify “Mac Desktop“.
And finally, if the computer is determined to be a virtual machine then the serial number will be prefixed with “MV-” to signify “Mac Virtual Machine“.


Script Content

#!/bin/bash

isLaptop=`/usr/sbin/system_profiler SPHardwareDataType | grep "Model Identifier" | grep "Book"`
isVM=`/usr/sbin/system_profiler SPHardwareDataType | grep "Model Identifier" | grep "Virtual"`

if [ "$isLaptop" != "" ]; then
	NewMacName=`system_profiler SPHardwareDataType | grep "Serial Number (system)" | sed 's/Serial Number (system): /ML-/'`
	elif [ "$isVM" != "" ]; then
		NewMacName=`system_profiler SPHardwareDataType | grep "Serial Number (system)" | sed 's/Serial Number (system): /MV-/'`
	else
		NewMacName=`system_profiler SPHardwareDataType | grep "Serial Number (system)" | sed 's/Serial Number (system): /MD-/'`
fi

echo "Setting computer name as: $NewMacName"
scutil --set ComputerName $NewMacName
scutil --set HostName $NewMacName
scutil --set LocalHostName $NewMacName
defaults write /Library/Preferences/SystemConfiguration/com.apple.smb.server NetBIOSName $NewMacName
rm -f "$isLaptop"
rm -f "$isVM"
rm -f "$NewMacName"

Concluding Comments

By implementing this naming convention when working with a large number of Macs will ensure that all computer names are unique. In addition it will allow for easy identification of wether a Mac is a laptop or desktop model.

About the author

Dan Ashley administrator