I'm currently testing Jamf and have everything working as I want with with the exception of figuring out the best way to rename computers after they are setup.
Currently I'm using Jamf with Jumpcloud pushing the user accounts for staff to an employee's computer however I'm stuck with the default computer name of Macbook Pro. I'm trying to figure out the best way to script this or use a policy that runs after the user is setup to that a machine can be renamed for username-Macbook Pro.
I'm wondering what the best way is to accomplish the renaming task so it can be automated and not have to be done manually each time a computer is deployed .
Thanks for any suggestions.
Best answer by ryan_ball
Is there a way to combine these all into one script that can run automatically after enrollment? Or does each step need to be done manually?
@mm2270 basically gave the entire answer. His code in a script would be:
#!/bin/bash
# Get the logged in username
logged_in_user=$( echo "show State:/Users/ConsoleUser" | /usr/sbin/scutil | /usr/bin/awk '/Name 😕😕 && ! /loginwindow/ { print $3 }' )
# Create a string
comp_name="${logged_in_user}-MacBook Pro"
# Rename the Mac using the jamf command line tool
/usr/local/bin/jamf SetComputername -name "$comp_name"
exit 0
Just take that, create a file named rename.sh using a Text Editor like Visual Studio Code, then upload that to your Jamf Pro and create a policy leveraging that script that runs once at check-in most likely as you want the user to actually be logged in when it runs.
#!/bin/bash
# Variables
internalNamingScheme=XXXX
lastLoggedInUser=`defaults read /Library/Preferences/com.apple.loginwindow lastUserName`
fname=`dscl . -read /Users/$lastLoggedInUser RealName | grep ^\\ | awk '{print $1}'`
modelName=`system_profiler SPHardwareDataType | grep "Model Identifier" | awk '{print $3}' | sed 's/[0-9]*//g' | tr -d ','`
serial1=`ioreg -l | awk '/IOPlatformSerialNumber/ { print $4;}'`
serial=`sed -e 's/^"//' -e 's/"$//' <<<"$serial1"`
# Get original name of computer
oldComputerName=`scutil --get ComputerName`
# Generate new computer name
computerName=$internalNamingScheme-$modelName-$fname
# Set new computer name locally
scutil --set ComputerName $computerName
scutil --set HostName $computerName
scutil --set LocalHostName $computerName
# Set the computer name in Jamf to reflect what is set locally on the computer
/usr/local/bin/jamf setComputerName -name $computerName
/usr/local/bin/jamf recon
echo "Computer name has been changed from $oldcomputername to $computerName"
exit 0
#!/bin/bash
# Variables
internalNamingScheme=XXXX
lastLoggedInUser=`defaults read /Library/Preferences/com.apple.loginwindow lastUserName`
fname=`dscl . -read /Users/$lastLoggedInUser RealName | grep ^\\ | awk '{print $1}'`
modelName=`system_profiler SPHardwareDataType | grep "Model Identifier" | awk '{print $3}' | sed 's/[0-9]*//g' | tr -d ','`
serial1=`ioreg -l | awk '/IOPlatformSerialNumber/ { print $4;}'`
serial=`sed -e 's/^"//' -e 's/"$//' <<<"$serial1"`
# Get original name of computer
oldComputerName=`scutil --get ComputerName`
# Generate new computer name
computerName=$internalNamingScheme-$modelName-$fname
# Set new computer name locally
scutil --set ComputerName $computerName
scutil --set HostName $computerName
scutil --set LocalHostName $computerName
# Set the computer name in Jamf to reflect what is set locally on the computer
/usr/local/bin/jamf setComputerName -name $computerName
/usr/local/bin/jamf recon
echo "Computer name has been changed from $oldcomputername to $computerName"
exit 0
Thanks. Giving it a try now. I might try and tweak it to run without a rename and just have it run to name the computer right after pre-stage is complete based on XXXX-Model-FirstName but I'll see how this works on my next pre-stage test.
Thanks. Giving it a try now. I might try and tweak it to run without a rename and just have it run to name the computer right after pre-stage is complete based on XXXX-Model-FirstName but I'll see how this works on my next pre-stage test.
# Get original name of computer #oldComputerName=`scutil --get ComputerName`
# Generate new computer name computerName=$XXXX-$modelName-$fname
# Set new computer name locally scutil --set ComputerName $computerName
scutil --set HostName $computerName
scutil --set LocalHostName $computerName
# Set the computer name in Jamf to reflect what is set locally on the computer /usr/local/bin/jamf setComputerName -name $computerName /usr/local/bin/jamf recon
# Get original name of computer #oldComputerName=`scutil --get ComputerName`
# Generate new computer name computerName=$XXXX-$modelName-$fname
# Set new computer name locally scutil --set ComputerName $computerName
scutil --set HostName $computerName
scutil --set LocalHostName $computerName
# Set the computer name in Jamf to reflect what is set locally on the computer /usr/local/bin/jamf setComputerName -name $computerName /usr/local/bin/jamf recon
exit 0
Maybe throwing those variables in the scutil command in quotes:
#!/bin/bash
# Variables
internalNamingScheme=XXXX
lastLoggedInUser=`defaults read /Library/Preferences/com.apple.loginwindow lastUserName`
fname=`dscl . -read /Users/$lastLoggedInUser RealName | grep ^\\ | awk '{print $1}'`
modelName=`system_profiler SPHardwareDataType | grep "Model Identifier" | awk '{print $3}' | sed 's/[0-9]*//g' | tr -d ','`
serial1=`ioreg -l | awk '/IOPlatformSerialNumber/ { print $4;}'`
serial=`sed -e 's/^"//' -e 's/"$//' <<<"$serial1"`
# Get original name of computer
oldComputerName=`scutil --get ComputerName`
# Generate new computer name
computerName=$internalNamingScheme-$modelName-$fname
# Set new computer name locally
scutil --set ComputerName "$computerName"
scutil --set HostName "$computerName"
scutil --set LocalHostName "$computerName"
# Set the computer name in Jamf to reflect what is set locally on the computer
/usr/local/bin/jamf setComputerName -name $computerName
/usr/local/bin/jamf recon
echo "Computer name has been changed from $oldComputerName to $computerName"
exit 0
Maybe throwing those variables in the scutil command in quotes:
#!/bin/bash
# Variables
internalNamingScheme=XXXX
lastLoggedInUser=`defaults read /Library/Preferences/com.apple.loginwindow lastUserName`
fname=`dscl . -read /Users/$lastLoggedInUser RealName | grep ^\\ | awk '{print $1}'`
modelName=`system_profiler SPHardwareDataType | grep "Model Identifier" | awk '{print $3}' | sed 's/[0-9]*//g' | tr -d ','`
serial1=`ioreg -l | awk '/IOPlatformSerialNumber/ { print $4;}'`
serial=`sed -e 's/^"//' -e 's/"$//' <<<"$serial1"`
# Get original name of computer
oldComputerName=`scutil --get ComputerName`
# Generate new computer name
computerName=$internalNamingScheme-$modelName-$fname
# Set new computer name locally
scutil --set ComputerName "$computerName"
scutil --set HostName "$computerName"
scutil --set LocalHostName "$computerName"
# Set the computer name in Jamf to reflect what is set locally on the computer
/usr/local/bin/jamf setComputerName -name $computerName
/usr/local/bin/jamf recon
echo "Computer name has been changed from $oldComputerName to $computerName"
exit 0
Holy **** that worked perfectly! I can't say thanks enough. That just accomplished what I've been trying to find a way to do for the past few weeks in other ways. Thanks again!
Holy **** that worked perfectly! I can't say thanks enough. That just accomplished what I've been trying to find a way to do for the past few weeks in other ways. Thanks again!
One thought occurred to me. It's unlikely but any ideas to avoid duplicate names besides appending the serial #? Which I assume would be this line modified:
One thought occurred to me. It's unlikely but any ideas to avoid duplicate names besides appending the serial #? Which I assume would be this line modified:
Maybe throwing those variables in the scutil command in quotes:
#!/bin/bash
# Variables
internalNamingScheme=XXXX
lastLoggedInUser=`defaults read /Library/Preferences/com.apple.loginwindow lastUserName`
fname=`dscl . -read /Users/$lastLoggedInUser RealName | grep ^\\ | awk '{print $1}'`
modelName=`system_profiler SPHardwareDataType | grep "Model Identifier" | awk '{print $3}' | sed 's/[0-9]*//g' | tr -d ','`
serial1=`ioreg -l | awk '/IOPlatformSerialNumber/ { print $4;}'`
serial=`sed -e 's/^"//' -e 's/"$//' <<<"$serial1"`
# Get original name of computer
oldComputerName=`scutil --get ComputerName`
# Generate new computer name
computerName=$internalNamingScheme-$modelName-$fname
# Set new computer name locally
scutil --set ComputerName "$computerName"
scutil --set HostName "$computerName"
scutil --set LocalHostName "$computerName"
# Set the computer name in Jamf to reflect what is set locally on the computer
/usr/local/bin/jamf setComputerName -name $computerName
/usr/local/bin/jamf recon
echo "Computer name has been changed from $oldComputerName to $computerName"
exit 0
Thank you for this! This worked for me too but I have a quick question. For this part of the script ( internalNamingScheme=XXXX ) it puts XXXX-MacBookPro etc etc on my computer name. How would I make it be the username of the user who is using the Computer?
Just for clarification, I am on SSO (Azure) on the machines as well.
Thank you for this! This worked for me too but I have a quick question. For this part of the script ( internalNamingScheme=XXXX ) it puts XXXX-MacBookPro etc etc on my computer name. How would I make it be the username of the user who is using the Computer?
Just for clarification, I am on SSO (Azure) on the machines as well.
To get the username of the last logged in user? You can then do something like
computerName=$lastLoggedInUser-$modelName
So, I would change ( internalNamingScheme=XXXX ) I would put $lastloggedInUser? and then in # Generate new computer name. I would do what you pasted which is ( computerName=$lastLoggedInUser-$modelName )?
So, I would change ( internalNamingScheme=XXXX ) I would put $lastloggedInUser? and then in # Generate new computer name. I would do what you pasted which is ( computerName=$lastLoggedInUser-$modelName )?
# Get original name of computer oldComputerName=`scutil --get ComputerName`
# Generate new computer name computerName=$internalNamingScheme-$modelName-$fname-$serial
# Set new computer name locally scutil --set ComputerName "$computerName"
scutil --set HostName "$computerName"
scutil --set LocalHostName "$computerName"
# Set the computer name in Jamf to reflect what is set locally on the computer /usr/local/bin/jamf setComputerName -name $computerName /usr/local/bin/jamf recon
echo "Computer name has been changed from $oldComputerName to $computerName"
# Get original name of computer oldComputerName=`scutil --get ComputerName`
# Generate new computer name computerName=$internalNamingScheme-$modelName-$fname-$serial
# Set new computer name locally scutil --set ComputerName "$computerName"
scutil --set HostName "$computerName"
scutil --set LocalHostName "$computerName"
# Set the computer name in Jamf to reflect what is set locally on the computer /usr/local/bin/jamf setComputerName -name $computerName /usr/local/bin/jamf recon
echo "Computer name has been changed from $oldComputerName to $computerName"
exit 0
I guess I would need to know what your desired naming scheme is, for example if you want `username-MacBookPro` then you would do:
#!/bin/bash
# Variables
lastLoggedInUser=$(defaults read /Library/Preferences/com.apple.loginwindow lastUserName)
modelName=$(system_profiler SPHardwareDataType | grep "Model Identifier" | awk '{print $3}' | sed 's/[0-9]*//g' | tr -d ',')
# Get original name of computer
oldComputerName=$(scutil --get ComputerName)
# Generate new computer name
computerName=$lastLoggedInUser-$modelName
# Set new computer name locally
scutil --set ComputerName "$computerName"
scutil --set HostName "$computerName"
scutil --set LocalHostName "$computerName"
# Set the computer name in Jamf to reflect what is set locally on the computer
/usr/local/bin/jamf setComputerName -name $computerName
/usr/local/bin/jamf recon
echo "Computer name has been changed from $oldComputerName to $computerName"
exit 0
# Get original name of computer oldComputerName=`scutil --get ComputerName`
# Generate new computer name computerName=$internalNamingScheme-$modelName-$fname-$serial
# Set new computer name locally scutil --set ComputerName "$computerName"
scutil --set HostName "$computerName"
scutil --set LocalHostName "$computerName"
# Set the computer name in Jamf to reflect what is set locally on the computer /usr/local/bin/jamf setComputerName -name $computerName /usr/local/bin/jamf recon
echo "Computer name has been changed from $oldComputerName to $computerName"
exit 0
Just want to update, it worked, so I changed ( computerName=$internalNamingScheme-$modelName-$fname-$serial ) to ( computerName=$lastLoggedInUser-$modelName-$fname-$serial )
Maybe throwing those variables in the scutil command in quotes:
#!/bin/bash
# Variables
internalNamingScheme=XXXX
lastLoggedInUser=`defaults read /Library/Preferences/com.apple.loginwindow lastUserName`
fname=`dscl . -read /Users/$lastLoggedInUser RealName | grep ^\\ | awk '{print $1}'`
modelName=`system_profiler SPHardwareDataType | grep "Model Identifier" | awk '{print $3}' | sed 's/[0-9]*//g' | tr -d ','`
serial1=`ioreg -l | awk '/IOPlatformSerialNumber/ { print $4;}'`
serial=`sed -e 's/^"//' -e 's/"$//' <<<"$serial1"`
# Get original name of computer
oldComputerName=`scutil --get ComputerName`
# Generate new computer name
computerName=$internalNamingScheme-$modelName-$fname
# Set new computer name locally
scutil --set ComputerName "$computerName"
scutil --set HostName "$computerName"
scutil --set LocalHostName "$computerName"
# Set the computer name in Jamf to reflect what is set locally on the computer
/usr/local/bin/jamf setComputerName -name $computerName
/usr/local/bin/jamf recon
echo "Computer name has been changed from $oldComputerName to $computerName"
exit 0
Hey, love the script works great but how would I add the $JSSID in the name? So the name may look like 123-iMac-Jane (the first 123 is the $JSSID)