Rename "MacBook"

New Contributor II

Our companies naming convention in JAMF is fairly simple, firstname.lastname. We do not have an active directory. Currently, I have to change the name manually in JAMF for each new computer. Since the user puts their name in during the Apple set up menu, Would it be possible to have this executed via script?


Legendary Contributor III

Is it a certainty that the name the user puts in during the setup will be in the format you need? Are they always entering proper First name and Last Name? If the answer is yes, then sure, this could be scripted fairly easily.
Since you said you don't have Active Directory, my assumption is you're using local accounts on the Macs.

You can use dscl to get the full name from a local account record. Something like this would get the logged in user's shortname, and then find the full name (called RealName in dscl), and format it for use as a Computer Name.


logged_in_user=$(/usr/sbin/scutil <<< "show State:/Users/ConsoleUser" | awk '/Name :/ && ! /loginwindow/ {print $3}')

computer_name=$(/usr/bin/dscl . read /Users/$logged_in_user RealName | awk -F':' '{print $NF}' | sed 's/^. *//;/^$/d;s/ /./g' | awk '{print tolower}')

If you want to have the name in all uppercase instead of lowercase, just change the last awk section to '{print toupper}' instead.

From there, you can add a line to the above script that would rename the Mac.

scutil --set ComputerName "$computer_name"
scutil --set LocalHostName "$computer_name"
scutil --set HostName "$computer_name"

Before running this on production machines, I would try the script out locally, or just have it echo back the computer name instead of doing the renaming, just to ensure you're getting the results you expect.