Skip to main content

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. 

I think ideally I need to run the command "jamf SetComputername -name" but I'm not sure how to have it automatically pull the username and then add MacbookPro to it so then the computer name ends up being bsmith-Macbook Pro

 

 


If all you need is the name of the logged in user account, use something like this in a script.

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

Then you can combine that variable of logged_in_user with the rest of the computer name to make it into a complete name, like

comp_name="${logged_in_user}-MacBook Pro"

Finally, use that for naming the machine

/usr/local/bin/jamf SetComputername -name "$comp_name"

 


If all you need is the name of the logged in user account, use something like this in a script.

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

Then you can combine that variable of logged_in_user with the rest of the computer name to make it into a complete name, like

comp_name="${logged_in_user}-MacBook Pro"

Finally, use that for naming the machine

/usr/local/bin/jamf SetComputername -name "$comp_name"

 


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? 


I think ideally I need to run the command "jamf SetComputername -name" but I'm not sure how to have it automatically pull the username and then add MacbookPro to it so then the computer name ends up being bsmith-Macbook Pro

 

 


#!/bin/sh

# Name Your Computer.sh
#
#


#The user will choose their department from a drop down menu

#dept=$(/usr/bin/osascript << EOD
#tell application "System Events"
#activate
#set Department to (choose from list {"K", "1st", "2nd", "3rd", "4th","5th", "6th"} with title "Departments" with prompt "Please choose your department")
#end tell
#EOD
#)

#The user will choose their building from a drop down menu

#bldg=$(/usr/bin/osascript << EOD
#tell application "System Events"
#activate
#set Building to (choose from list {"HS", "MS", "IC", "PC"} with title "Buildings" with prompt "Please choose the building you work at")
#end tell
#EOD
#)

#The user will input the value of their asset tag

tag=$(/usr/bin/osascript << EOD
tell application "System Events"
activate
set AssetTag to text returned of (display dialog "Please enter your LastName followed by FirstInitial (ex.SpacklerC)" default answer "" buttons {"Continue"} default button 1)
end tell
EOD
)


#This takes the data collected from the user and sets it as the Computer Name and submits the name to the Jamf API

deviceName="DO-$tag"

#set all the name in all the places
/usr/local/bin/jamf setComputerName -name "$deviceName"
/usr/local/bin/jamf recon

 

 

This is the script I use at the School District I manage. You can tweak the script however you'd like. If you have any questions let me know. 


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? 


Yes of course. I was only adding in lines above to show how it can be done. But if you put them into a shell script in the above order, that should get you most of the way there. Something like

 

#!/bin/zsh

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

comp_name="${logged_in_user}-MacBook Pro"

/usr/local/bin/jamf SetComputername -name "$comp_name"

 

 This is quite simplified, as there's a lot of checks and other things such a script could include. But I'm purposely keeping it simple here for you so it makes sense.


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.


Thanks for simplifying it. Testing it now via a policy. 


I received this error in the logs. My script is below and I've attached it to a policy in Jamf.  What am I missing?

 

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

 

 

 

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


I received this error in the logs. My script is below and I've attached it to a policy in Jamf.  What am I missing?

 

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

 

 

 

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


Did you use TextEdit to create the sh file or a text editor like Visual Studio Code or Sublime Text? TextExit would have to be set in plain text mode and turning off text replacement and things like that. I suggest you use a real text editor used for creating code.

It also appears you also have the Maintenance > Reset Computer Names option added in the policy which will attempt to rename the local Mac to match the name of the Jamf Pro record. You'd want to turn this off.


I received this error in the logs. My script is below and I've attached it to a policy in Jamf.  What am I missing?

 

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

 

 

 

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


Well that's weird. There's an error in the awk syntax. There's a backslash "\\" where there should be a forward slash "/"I have no idea how it made it into my post like that, since I just copied and pasted that from a working script.

So anyway, change the "logged_in_user" line to look like this

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

That should fix it. 


Well that's weird. There's an error in the awk syntax. There's a backslash "\\" where there should be a forward slash "/"I have no idea how it made it into my post like that, since I just copied and pasted that from a working script.

So anyway, change the "logged_in_user" line to look like this

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

That should fix it. 


Ack!!! It's doing it again. Ok, so its this dang new forum format doing it. Ugh!! Jamf, please fix this. Your forum software is converting some forward slashes to backslashes. What the heck!? Why would it being doing that?

@MBlank Please make sure that the only \\ in the above shown is a / instead.


There are plenty of good ways to set the computer name via script on macOS.  I would like to point out however that using any personally identifiable information in the device name is a potential safety and security risk, especially if you are setting the device name for minor students.  Since a device name is broadcast in the clear on any network, you run the risk of allowing a bad actor to know if an individual is in a particular location.

The following can be used to give a Mac a unique name by executing a command via the Files and Processes payload in a policy:

/usr/local/bin/jamf setComputerName -name "Mac-$(ifconfig en0 | awk '/ether/{print $2}' | tr -d :’)”;/usr/local/bin/jamf recon

Just my $0.02


It worked as a local script on the machine and now I'm testing it via a policy in Jamf. Should I leave update inventory checked on Maintainence? 

 

 

 


If you change the name of the device, you’ll want to update inventory so that the device record name matches. 


It worked as a local script on the machine and now I'm testing it via a policy in Jamf. Should I leave update inventory checked on Maintainence? 

 

 

 


At the end of the script you can put 

/usr/local/bin/jamf recon

Which will automatically check into jamf after the other parts of the script were run. 


At the end of the script you can put 

/usr/local/bin/jamf recon

Which will automatically check into jamf after the other parts of the script were run. 


Like so:

 

/usr/local/bin/jamf SetComputername -name "$comp_name"

/usr/local/bin/jamf recon

 

exit 0


I was tinkering around a bit thinking about this script, this is what I came up with:

#!/bin/bash

# Set variables

# Get the last logged in user
lastLoggedInUser=`defaults read /Library/Preferences/com.apple.loginwindow lastUserName`

# Get Model Name of computer
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

I was tinkering around a bit thinking about this script, this is what I came up with:

#!/bin/bash

# Set variables

# Get the last logged in user
lastLoggedInUser=`defaults read /Library/Preferences/com.apple.loginwindow lastUserName`

# Get Model Name of computer
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

This script works well for adult users, but due to the PII in the device name, should not be used for K12 student devices. One could make the argument against PII in any device name, but for minors there is a safety and security risk. 

Just my $0.02


This script works well for adult users, but due to the PII in the device name, should not be used for K12 student devices. One could make the argument against PII in any device name, but for minors there is a safety and security risk. 

Just my $0.02


Totally agree! PII is a big issue! I’ve always used an inventory asset number as my computer names or a location based naming scheme (Bldg-Room-Station). That seemed to work out well. 


Are there any variables that can pull the first part of NAME from when the computer is configured instead of the username that {logged_in_user} generates? So the script pulls "Bob" from Bob Smith?


Are there any variables that can pull the first part of NAME from when the computer is configured instead of the username that {logged_in_user} generates? So the script pulls "Bob" from Bob Smith?


You can get the last logged in user using: 

lastLoggedInUser=`defaults read /Library/Preferences/com.apple.loginwindow lastUserName`

Then you can get the first name by 

fname=`dscl . -read /Users/$lastLoggedInUser RealName | grep ^\\ | awk '{print $1}'`

Then echo that to make sure

echo $fname

 


You can get the last logged in user using: 

lastLoggedInUser=`defaults read /Library/Preferences/com.apple.loginwindow lastUserName`

Then you can get the first name by 

fname=`dscl . -read /Users/$lastLoggedInUser RealName | grep ^\\ | awk '{print $1}'`

Then echo that to make sure

echo $fname

 


Thanks! Would I be able to use this to rename a computer using the first name instead of the logged in user name by modifying my existing script? This runs right after pre-stage. Or would it not be possible since the user was just created ie during initial setup. Thanks for any additional suggestions. 

XXXX=Internal Naming Scheme

# Get the logged in username
logged_in_user=$(/usr/sbin/scutil <<< "show State:/Users/ConsoleUser" | /usr/bin/awk '/Name 😕😕 && ! /loginwindow/ {print $3}')

#get computer serial number
serialNumber=`system_profiler SPHardwareDataType | awk '/Serial/ {print $4}'`

# Create a string
comp_name="XXXX-MBP-${logged_in_user}-$serialNumber"

# Rename the Mac using the jamf command line tool
jamf SetComputername -name "$comp_name"

jamf recon

exit 0


Thanks! Would I be able to use this to rename a computer using the first name instead of the logged in user name by modifying my existing script? This runs right after pre-stage. Or would it not be possible since the user was just created ie during initial setup. Thanks for any additional suggestions. 

XXXX=Internal Naming Scheme

# Get the logged in username
logged_in_user=$(/usr/sbin/scutil <<< "show State:/Users/ConsoleUser" | /usr/bin/awk '/Name 😕😕 && ! /loginwindow/ {print $3}')

#get computer serial number
serialNumber=`system_profiler SPHardwareDataType | awk '/Serial/ {print $4}'`

# Create a string
comp_name="XXXX-MBP-${logged_in_user}-$serialNumber"

# Rename the Mac using the jamf command line tool
jamf SetComputername -name "$comp_name"

jamf recon

exit 0


Yes, if you get your first name variable, you will just plug that into where you are setting the computer name variable 


Yes, if you get your first name variable, you will just plug that into where you are setting the computer name variable 


Any chance you can check my handy work and let me know's missing? Scripting is absolutely not my strength 😞 

I dropped the serial field so ideally the script should rename the computer right after prestage to XXXX-MBP-Firstname 

 

 

# Get the logged in username
logged_in_user=$lastLoggedInUser=`defaults read /Library/Preferences/com.apple.loginwindow lastUserName` 😕😕 && ! /loginwindow/ {print $3}')

$fname=`dscl . -read /Users/$lastLoggedInUser RealName | grep ^\\ | awk '{print $1}'`

echo $fname

# Create a string
comp_name="XXXX-MBP-${$fname}-$”

# Rename the Mac using the jamf command line tool
jamf SetComputername -name "$comp_name"

jamf recon

exit 0


Any chance you can check my handy work and let me know's missing? Scripting is absolutely not my strength 😞 

I dropped the serial field so ideally the script should rename the computer right after prestage to XXXX-MBP-Firstname 

 

 

# Get the logged in username
logged_in_user=$lastLoggedInUser=`defaults read /Library/Preferences/com.apple.loginwindow lastUserName` 😕😕 && ! /loginwindow/ {print $3}')

$fname=`dscl . -read /Users/$lastLoggedInUser RealName | grep ^\\ | awk '{print $1}'`

echo $fname

# Create a string
comp_name="XXXX-MBP-${$fname}-$”

# Rename the Mac using the jamf command line tool
jamf SetComputername -name "$comp_name"

jamf recon

exit 0


Huge thanks for any more help you can provide : 😀


Reply