Skip to main content
Solved

script to rename computer name (letter of first name and last name)

  • January 30, 2018
  • 27 replies
  • 177 views

Show first post

27 replies

markopolo
Forum|alt.badge.img+9
  • Contributor
  • April 28, 2025

This should work. I used the Jamf "setComputerName" command to set the computer name. Below it are the scutil commands to set the computer name. I commented out those lines. They are only needed if you're not using the Jamf command to name the computer. If you use scutil, you would want to comment out the line that uses the Jamf command.

#!/bin/zsh jamfBinary="/usr/local/jamf/bin/jamf" # Get last 7 of serial number serial=$(system_profiler SPHardwareDataType | grep Serial | /usr/bin/awk '{ print $4 }' | tail -c 8 ) # Current logged in user currentUser=$(/bin/ls -l /dev/console | /usr/bin/awk '{ print $3 }') # First Name firstName=$(dscl . -read /Users/$currentUser RealName | cut -d: -f2 | sed -e 's/^[ \\t]*//' | grep -v "^$" | /usr/bin/awk '{ print $1 }') # Last Name lastName=$(dscl . -read /Users/$currentUser RealName | cut -d: -f2 | sed -e 's/^[ \\t]*//' | grep -v "^$" | /usr/bin/awk '{ print $2 }') # Get first initial firstInitial=$(finger -s $currentUser | head -2 | tail -n 1 | awk '{print toupper ($2)}' | cut -c 1) # Get last initial lastInitial=$(finger -s $currentUser | head -2 | tail -n 1 | awk '{print toupper ($3)}' | cut -c 1) # Generate computer name computerName="${firstInitial}""${lastName}"-"${serial}" # Run Jamf command to name the computer "$jamfBinary" setComputerName "$computerName" # Use scutil to set computer name - comment out if using 'jamf setComputerName' #scutil --set ComputerName "$computerName" #scutil --set LocalHostName "$computerName" #scutil --set HostName "$computerName"

 

Thanks, will give this a try. How does your script know to skip middle names (e.g., full name is "Mary Elizabeth Winstead" and I want the computer name to be "mwinstead-XXXXXXX"; wouldn't "print $2" on the dscl command return elizabeth, not winstead)?


howie_isaacks
Forum|alt.badge.img+23
  • Esteemed Contributor
  • April 28, 2025

 

Thanks, will give this a try. How does your script know to skip middle names (e.g., full name is "Mary Elizabeth Winstead" and I want the computer name to be "mwinstead-XXXXXXX"; wouldn't "print $2" on the dscl command return elizabeth, not winstead)?


It doesn't know to do that. It's querying the first and last name that is in the user's local directory record. My script is looking at the "RealName" attribute. Here's a screenshot from my personal MacBook Pro. You could experiment with using awk or some other method of parsing output to remove a middle name from the output.