Help me write a script to append "OLD" to the end of a Mac's name

howie_isaacks
Valued Contributor II

One of my remote clients has a Mac admin onsite whose job it is to enroll new or refreshed Macs into Jamf Pro, and to provide some of the end user support when needed. I want to simplify his job, and also ensure that proper naming is performed during enrollment. I have already deployed a script that will run right after enrollment that will prompt him to name the Mac. After that, the policy runs a recon to ensure that the new name is immediately submitted into the Mac's machine record. Since users are often still using the Mac that the new one is replacing, I want to setup a policy in Self Service that he can use to rename the existing Mac, and append "OLD" to the end of the name. I'm sure that the solution to this is to use basically the same commands, but I don't know how to add a line that adds "OLD" to the end of the Mac's name. I don't want the existing Mac to be completely renamed. I just want "OLD" to be added to the end. Something like "Mike-Smith-OLD". How do I make this happen?

The commands that my enrollment script runs are:

/usr/sbin/scutil --set ComputerName "${computerName}"
/usr/sbin/scutil --set LocalHostName "${computerName}"
/usr/sbin/scutil --set HostName "${computerName}"

During enrollment, I have the script setup so that the admin is prompted to give the Mac a name.

#!/bin/sh
computerName=$(osascript -e 'tell application "SystemUIServer"
set myComputerName to text returned of (display dialog "Please give this Mac a name then click Submit." default answer "" with title "Asset ID" buttons {"Submit"} with icon caution)
end tell')

/usr/sbin/scutil --set ComputerName "${computerName}"
/usr/sbin/scutil --set LocalHostName "${computerName}"
/usr/sbin/scutil --set HostName "${computerName}"

dscacheutil -flushcache

echo "Computer name has been set..."
echo "<result>`scutil --get ComputerName`</result>"


exit 0
2 REPLIES 2

mm2270
Legendary Contributor III

@howie_isaacks The general idea behind doing something like this is to capture the existing computer name in a variable, then use that variable to create a new variable for the new computer name by appending -OLD to the end of it. For example

CURRENT_NAME=$(/usr/sbin/scutil --get ComputerName)
NEW_NAME="${CURRENT_NAME}-OLD"

## Set the computer name to its new value
/usr/sbin/scutil --set ComputerName "$NEW_NAME"

In the above, if the existing computer name is something like "Mike-Smith", the $CURRENT_NAME variable will capture that first, then create a new name like Mike-Smith-OLD because $NEW_NAME is simply a string that is a combination of $CURRENT_NAME and -OLD

You might also want to or need to set the hostname to the same string with another scutil line, since I believe when name collisions occur on the network, they can happen with the hostname too.

howie_isaacks
Valued Contributor II

Thanks @mm2270 ! I will give this a try. I'm still learning some of the more advanced scripting, so this will help me later on when I need to tackle other types of issues. I really appreciate the response!