Posted on 07-09-2020 05:29 AM
Hi Guys!
Is there a simple, and effective way to change a hostname of a joined Mac via Jamf? We have a little bit too much "David's MacBook Air" going on :)
Solved! Go to Solution.
Posted on 07-09-2020 06:49 AM
Reset Computer Name renames the computer to the name in Jamf. You could just rename every computer in Jamf and then have a policy that runs regularly to Reset Computer Name. Not sure how easy that is. If you have a lot of computers, you could try MUT to update the names.
You could also run the script/policy to set the computer name and still use to the Reset Computer Name policy to fix it, if user's try and change it.
Posted on 07-09-2020 06:05 AM
You have a lot of options here. The "easiest" way is to write a script that using jamf setComputerName -name <<newComptuerName>>
and assign it to a policy.This will update the computer name. How do you want to name the computers?
Include an Inventory Update in your policy to ensure the new name is captured by Jamf.
Posted on 07-09-2020 06:43 AM
I just noticed something when going into the Inventory Update.
Would the "Reset Computer Names" under Maintenance not have the desired effect?
Posted on 07-09-2020 06:49 AM
Reset Computer Name renames the computer to the name in Jamf. You could just rename every computer in Jamf and then have a policy that runs regularly to Reset Computer Name. Not sure how easy that is. If you have a lot of computers, you could try MUT to update the names.
You could also run the script/policy to set the computer name and still use to the Reset Computer Name policy to fix it, if user's try and change it.
Posted on 07-09-2020 08:21 AM
I just force the hostname (all 3 of them) to the serial number on a daily basis since all our users are admins and with sudo
you can change whatever hostname you want.
Posted on 11-12-2021 02:37 PM
For my clients I use a naming scheme that uses the company's initials, "M" for Mac, and then the user's first and last initials... AC-M-HI... Acme Corp, Mac, Howie Isaacks... I did not want to have to manually rename Macs every time one is enrolled, so I wrote this script with some help from other Jamf Nation members. When ever I get around to it, I will make a version of the script that will allow us to define a variable for the first part of the script for the company so we don't have to manually modify this script every time it's added to a Jamf Pro server. We would just go into the policy containing the script and set the company initials in the field provided. I have this script run as the first policy that runs at check-in after the user account is setup and the desktop appears. It appears to run at some point either before or after DEPNotify is finished. This script was useful when I needed to mass rename a lot of Macs. There were some users with the same initials, so we dealt with those manually. There were only a few so it wasn't a bit deal.
#!/bin/sh
#Rename the Mac using the first and last initial of the user's name. Before using this script, replace "XX" in line 12 with the client company's initials
#Who is the current logged in user?
currentUser=`/bin/ls -l /dev/console | /usr/bin/awk '{ print $3 }'`
echo $currentUser
#Generate computer name - Replace "XX" with the client company's initials
firstInitial=$(finger -s $currentUser | head -2 | tail -n 1 | awk '{print toupper ($2)}' | cut -c 1)
lastInitial=$(finger -s $currentUser | head -2 | tail -n 1 | awk '{print toupper ($3)}' | cut -c 1)
computerName=$"XX-M-$firstInitial$lastInitial"
echo $computerName
#Send the computer name to inventory record
scutil --set ComputerName $computerName
scutil --set LocalHostName $computerName
scutil --set HostName $computerName
#Clear the directory service cache then run a current inventory to send the new Mac name to Jamf Pro
dscacheutil -flushcache
/usr/local/jamf/bin/jamf recon
#Howie Isaacks with help from Jamf Nation | F1 Information Technologies | 09/27/21
Posted on 08-29-2022 07:58 AM
Hello! This is pretty helpful, but I do not know enough about scripting to use this as a template (yet). I do basic Kaseya scripts here at work and am still coming up to speed on learning this stuff. :)
We are looking to have Jamf push the Macbook name based on this formula: first-last-serial#ofMacbook.
So Alex Bryant with Macbook serial FVFVG2G4Q6L7 should be named: ALEX-BRYANT-FVFVG2G4Q6L7
Would it be possible for you (or someone on this thread) to post how this script could be altered to produce that? Thank you very much!
09-07-2022 08:59 AM - edited 09-07-2022 09:13 AM
To get the serial number, this command should do it:
serial=$(system_profiler SPHardwareDataType | grep Serial | /usr/bin/awk '{ print $4 }')
The variable "serial" can be changed to what ever you want it to be. After this command is ran, typing echo $serial will produce the serial number. Later in the script when you use "$serial" in the script that will input the serial number into the line of script you need it to be in.
If you type this command in Terminal it should show your Mac's serial number.
system_profiler SPHardwareDataType | grep Serial | /usr/bin/awk '{ print $4 }'
Let me know if you would like a script that names the Mac the way you described above. When I have a moment today I can write it for you.
Posted on 09-07-2022 02:15 PM
You wanted the user's name to be in all caps. I am trying to figure out how to get AWK to output the first and last name as all caps but here's what I have so far. It outputs a computer name that includes the first name, last name, and serial number separated by dashes. It then runs the commands to rename the computer and updates the inventory. It may not be necessary to have the "sleep 5" commands but I found that sometimes waiting a few seconds after each scutil command used in a script makes the process more reliable.
#!/bin/sh
#What is the serial number of the computer?
serialNumber=$(system_profiler SPHardwareDataType | grep Serial | /usr/bin/awk '{ print $4 }')
#who is the current logged in user?
currentUser=`/bin/ls -l /dev/console | /usr/bin/awk '{ print $3 }'`
#What is the first name of the user?
firstName=$(dscl . -read /Users/$currentUser RealName | cut -d: -f2 | sed -e 's/^[ \t]*//' | grep -v "^$" | /usr/bin/awk '{ print $1 }')
#What is the last name of the user?
lastName=$(dscl . -read /Users/$currentUser RealName | cut -d: -f2 | sed -e 's/^[ \t]*//' | grep -v "^$" | /usr/bin/awk '{ print $2 }')
#Genrate computer name
computerName=$firstName-$lastName-$serialNumber
echo $computerName
#Set the computer name. Wait 5 seconds after each command.
scutil --set ComputerName $computerName
sleep 5
scutil --set LocalHostName $computerName
sleep 5
scutil --set HostName $computerName
sleep 5
#Clear the directory service cache then run a current inventory to send the new Mac name to Jamf Pro
dscacheutil -flushcache
/usr/local/jamf/bin/jamf recon
I ran this script through CodeRunner and the output was as expected. It was my first name, last name, and my Mac's serial number separated by dashes. I suggest NOT using a user's full name in a computer name. Initials or some shortened version will protect the user's identity when they're on a public WiFi network.
Posted on 09-07-2022 12:53 PM
This is a little harder than I thought it would be. Still working on it as I have time.
09-08-2022 08:39 AM - edited 03-09-2023 08:15 AM
I couldn't let this go. I wanted to create a script that would do exactly what @redfinjamfadmin asked for above. Here it is:
#!/bin/sh
#What is the serial number of the computer?
serialNumber=$(system_profiler SPHardwareDataType | grep Serial | /usr/bin/awk '{ print $4 }')
#who is the current logged in user?
currentUser=`/bin/ls -l /dev/console | /usr/bin/awk '{ print $3 }'`
#What is the first name of the user?
firstName=$(dscl . -read /Users/$currentUser RealName | cut -d: -f2 | sed -e 's/^[ \t]*//' | grep -v "^$" | /usr/bin/awk '{ print $1 }')
#What is the last name of the user?
lastName=$(dscl . -read /Users/$currentUser RealName | cut -d: -f2 | sed -e 's/^[ \t]*//' | grep -v "^$" | /usr/bin/awk '{ print $2 }')
#Convert firstName and lastName to all caps
firstNameCAP=$(echo $firstName | tr '[a-z]' '[A-Z]')
lastNameCAP=$(echo $lastName | tr '[a-z]' '[A-Z]')
#Genrate computer name
computerName=$firstNameCAP-$lastNameCAP-$serialNumber
echo $computerName
#Set the computer name.
scutil --set ComputerName $computerName
scutil --set LocalHostName $computerName
scutil --set HostName $computerName
#Clear the directory service cache then run a current inventory to send the new Mac name to Jamf Pro
dscacheutil -flushcache
/usr/local/jamf/bin/jamf recon
I was trying to get awk to convert the characters in the first and last name to all caps but I couldn't find a good way. I did some research and found the translate characters command (tr) and it has the ability to do this. If you reverse the a-z function and make it '[A-Z' '[a-z]' it will change all caps to lowercase.
I hope this helps someone! Everyone at Jamf Nation has always helped me when I need it so I'm happy to help when ever I can.
Posted on 08-29-2023 05:26 AM
Newer Jamf admin here and I wanted to say thanks for this script! It's super helpful as we begin to standardize hostnames.
Posted on 08-29-2023 11:38 AM
My pleasure! Practice makes perfect. I first posted to this thread almost 2 years ago. I have since learned a lot of different ways to name computers. There's always someone here with the knowledge and generosity you need to find the way to get something to work. Everyone here is awesome! They have saved me a lot of headaches for almost 10 years since I've been part of Jamf Nation.
Posted on 10-07-2024 07:25 AM
@howie_isaacks this crip is very nice however in our company we have separated the desktop and laptop so is there any way to check based on their model if the device is a laptop I can set a hostname like L - laptop, M- Mac, XX -Company name, and serial number (LMXXSerialnumber). and if it is iMAC or Studio so consider as D- Desktop, M - Mac, XX -Company name, and serial number (DMXXSerialnumber)
Please help to achieve this as I'm also new to scripting.
Thanks
Posted on 10-07-2024 11:38 AM
You could add in a variable that would check the model name. All of Apple's laptops begin with "MacBook". This command will output "MacBook" if ran on an Apple laptop.
system_profiler SPHardwareDataType | grep "Model Name:" | awk '{print $3}'
Your variable could be something like...
macModel=$(system_profiler SPHardwareDataType | grep "Model Name:" | awk '{print $3}')
You could use a case statement like this to get the output needed to buid the computer name. Note that in the second case in the statement I left a space after "Mac". This was done because there would normally be a space after "Mac" as in Mac Pro, Mac Studio, or Mac mini. The asterisk is a wildcard to account for anything that would come after "Mac" in the model name.
case $macModel in
"MacBook") macType="Laptop"
;;
"Mac "*) macType="Desktop"
;;
"iMac") macType="Desktop"
;;
esac
Above there is an earlier script that I posted that shows how to get the user's initials to include in the computer name. Once you have all the parts, you can just chain them all together in a new variable and use either "jamf setComputerName -name" or the scutil commands. These days I prefer using "jamf setComputerName". I'm happy to write this for you if you want. I just need to make sure I know all of the criteria for your naming scheme.
Posted on 10-08-2024 06:00 AM
@howie_isaacks Thank u so much for your response I'm trying to join the script parts not able to do it so far as I'm a beginner in scripting, if possible could you help me to write this for getting the hostname example: L, M/D, Company name, serial number, or StaffID.
Thanks once again
Posted on 10-08-2024 08:12 AM
I'm happy to help. Serial numbers are easy to add to computer names since they're easy to get in a script. Above I showed how the script will know if the Mac is a desktop or laptop. You want the company name. What name do you want in the script? I can also just write this with a fictional name and you can just change it later. If you have access to Jamf's training site, you should look for their course on scripting. It was very helpful to me a few years ago. After I went through it I started inventing problems to solve so I could get more practice. I would also encourage you to join the Mac Admins Slack. There are a lot of super smart people there who are happy to help a beginner. Of course you already know about Jamf Nation. There are a lot of super smart people here too. Find yourself a good app for writing scripts. My favorites are CodeRunner and BBEdit. I have been using CodeRunner a lot more lately. It is very helpful with writing scripts if you're a beginner. It will point out syntax errors as you write or you can just have it point out errors when you run the script. It annoys the crap out of me sometimes but I'm happy to know about the mistakes before I deploy the script.
Posted on 10-10-2024 06:32 AM
Sorry for the late reply, Thanks for the nice advice... I have joined admin slack also I will look for the training part for scripting.
10-08-2024 08:42 AM - edited 10-08-2024 08:47 AM
Try this. I didn't know what order you want everyting in but that's easy for you to switch around. I used a variable for company name so what ever you fill in for "companyName" will be in your computer names. I added a Jamf recon step at the end but if you put this script in a policy and include that payload you can comment out or remove that line from the script.
#!/bin/zsh
# Sets computer name using the Mac type (laptop or desktop), company name, and serial number
# 10/8/2024
companyName="NeXT"
serialNumber=$(system_profiler SPHardwareDataType | grep "Serial Number" | awk '{print $4}')
macModel=$(system_profiler SPHardwareDataType | grep "Model Name:" | awk '{print $3}')
jamfBinary="/usr/local/jamf/bin/jamf"
# Use the Mac model name to label the Mac as a laptop (L) or desktop (D)
case $macModel in
"MacBook") macType="L"
echo "This Mac is a laptop"
;;
"Mac "*) macType="D"
echo "This Mac is a desktop"
;;
"iMac") macType="D"
echo "This Mac is a desktop"
;;
esac
# Create the computer name using variable inputs
computerName=(${macType}-${companyName}-${serialNumber})
echo $computerName
# Run Jamf command to change computer name
"$jamfBinary" setComputerName -name "$computerName"
# Run inventory to update the Mac name with Jamf Pro. Comment out or remove this line if you have this step in your Jamf policy payload
"$jamfBinary" recon
Posted on 10-10-2024 06:36 AM
Tons of Thanks I will try this and update you.
Posted on 10-10-2024 07:57 AM
My pleasure! I tested it using CodeRunner. In my test, I commented out the setComputerName and recon commands because I didn't want to change the name of my own Mac but the script will create the computer name. Just replace what is between the quotes in
companyName="NeXT"
with your own company name or what ever you want to put there. You can also change the order of the variables in
computerName=(${macType}-${companyName}-${serialNumber})
to arrange them the way you want. I used brackets with the variables but that may not actually be necessary. It's a way of making sure that the data from the variables stays separate from the rest of the characters used.
Posted on 10-12-2024 09:23 AM
Did it work for you?
Posted on 10-13-2024 10:13 PM
I have not yet pushed this to production, however, I have tried it on bbedit (trial version) it is working as expected. both debug apps are not free so trying to learn some on visual studio code.
My plan to push this script after the enrollment and push the config file for set computer records. so this will be fixed and the user will not be able to change
Thanks once again I will sure update u once it is in production :)
Posted on 10-14-2024 07:16 AM
I highly recommend CodeRunner. BBEdit is a good choice too. I still use it for a lot of things. I recently made CodeRunner my default script editor. I like that I can run the script to test the commands and functions in my scripts. I make sure to comment out the lines that contain commands that would actually make a change on my Mac that I'm running it on. CodeRunner can be ran as a trial too.