Look! Another naming script post!

easyedc
Valued Contributor II

Over the years, with new OS's releasing, I've been tweaking a naming script that changes all the computer names AND changes the hard drive name (we use that as a quick reference for users for support issues). I can rename the computer all day long, but with APFS volumes out there, I'm looking for better solutions.

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

HDDName=`/usr/sbin/diskutil list | grep "Macintosh HD" | grep disk0`
HDDCSName=`/usr/sbin/diskutil list | grep "Macintosh HD" | grep disk1`
UntName=`/usr/sbin/diskutil list | grep "Untitled 1" | grep disk0`
Unt2Name=`/usr/sbin/diskutil list | grep "Untitled 1" | grep disk1`


if [ "$HDDName" != "" ]; then
    diskutil rename disk0s2 "${base}${serial}"
elif [ "$HDDCSName" != "" ]; then
    diskutil rename disk1 "${base}${serial}"
elif [ "$UntName" != "" ]; then
    diskutil rename disk0s2 "${base}${serial}"
elif [ "$Unt2Name" != "" ]; then
    diskutil rename disk1 "${base}${serial}"
fi

The script is based on 2 assumptions. Either a) the drive is brand new out of the box and named "Macintosh HD" or it was wiped and imaged and in the image the drive name was intentionally left as "Untitled 1" but with the addition of APFS vs JFHS+ a new wrinkle emerges. I assume that I could simplify this with something along the lines of a grep around

disk1s1

or

disk0s1

But before I go through that trial/error testing, I wanted to ask if anyone else has a simpler or prettier method to accomplish my goal or suggestions in general?

Thanks

0 REPLIES 0