Trying to find the best way to rename computers after enrollment

MBlank
Contributor

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. 

3 ACCEPTED SOLUTIONS

ryan_ball
Valued Contributor

@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.

View solution in original post

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

View solution in original post

computerName=$internalNamingScheme-$modelName-$fname-$serial

View solution in original post

43 REPLIES 43

MBlank
Contributor

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

 

 

ChaseEndy
New Contributor III

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

mm2270
Legendary Contributor III

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? 

mm2270
Legendary Contributor III

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.

ryan_ball
Valued Contributor

@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.

MBlank
Contributor

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

MBlank
Contributor

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

 

MBlank_0-1631810507007.png

 

 

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

ryan_ball
Valued Contributor

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.

mm2270
Legendary Contributor III

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. 

mm2270
Legendary Contributor III

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.

jcarr
Release Candidate Programs Tester

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

MBlank
Contributor

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? 

 

MBlank_0-1631812913336.png

 

 

ChaseEndy
New Contributor III

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

jcarr
Release Candidate Programs Tester

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

robjschroeder
Contributor

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

jcarr
Release Candidate Programs Tester

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. 

MBlank
Contributor

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

 

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 

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 :😀

You can give this a look:

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

 

Appreciate the help.

Anytime! 👊

MBlank
Contributor

Currently getting this error when using a policy to run the script 😞

 

Script result: scutil: invalid option -- M
usage: scutil interactive access to the dynamic store.

 

Here's my current iteration. With the XXXX's in place of an internal abbreviation.

 

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

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!

AWESOME! I'm glad that worked! Anytime!

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.

 

Could you use: 

 

lastLoggedInUser=$(defaults read /Library/Preferences/com.apple.loginwindow lastUserName)

 

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 )?

 

Sorry, I am not perfect in scripts.

rguzman1
New Contributor II

This is what I currently have.

 

 

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

rguzman1
New Contributor II

Just want to update, it worked, so I changed ( computerName=$internalNamingScheme-$modelName-$fname-$serial ) to ( computerName=$lastLoggedInUser-$modelName-$fname-$serial )

 

And now it works.

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)