Posted on 05-04-2022 08:02 AM
Hi All,
Our District is aiming to use Macbook Air for our next laptop refresh. We have Jamf Pro and on our way to get a subscription on Jamf Connect. Anyhow, when I created a script under Policies, the name is not changing. The naming convention that I wanted to use for all the M1's should be M1-"Serial Number". The script that I've created is below;
#!/bin/bash
$ sudo jamf setComputerName -name $M1-$serialNumber
scutil --set ComputerName $M1-$serialNumber
scutil --set LocalHostName $M1-$serialNumber
scutil --set HostName $M1-$serialNumber
exit
Please help?
Thank you and have a great one!
Sincerely,
Cesar
Posted on 05-04-2022 09:42 AM
First you need to define the variable "serialNumber" If you don't, the script doesn't know what "serialNumber" represents. Try this after #!/bin/bash:
serialNumber=$(system_profiler SPHardwareDataType | grep -i "Serial Number (system):" | /usr/bin/awk ' {print $4 }')
If you type the above in Terminal and hit return then type echo $serialNumber and hit return again, you will be given the serial number.
Why are you using "$M1" before the serial number? Is that another variable that you defined in the script?
Posted on 05-04-2022 09:45 AM
Thanks @howie_isaacks! M1 is an additional name that I wanted to add on the naming convention.
I will try to add your instructions and let you know if that works. Thanks again.
Posted on 05-04-2022 10:22 AM
Hi @howie_isaacks ,
The script did not work.
After the enrollment, Iwent to system preferences, sharing, and it named the computer with; (User)Macbook Air. It look like this;
And I have this weird name on the top right screen.
Any other suggestion please? Thanks!
Posted on 05-04-2022 10:27 AM
Also, this is the name that registered on Jamf Pro
Posted on 05-04-2022 10:30 AM
@cmayo I ran the following script and it worked for me:
#!/bin/bash
serialNumber=$(system_profiler SPHardwareDataType | grep -i "Serial Number (system):" | /usr/bin/awk ' {print $4 }')
scutil --set ComputerName $serialNumber
scutil --set LocalHostName $serialNumber
scutil --set HostName $serialNumber
jamf recon
exit
You will need to add a recon for it to update in JAMF Pro since the GUI updates the name on inventory update.
Posted on 05-04-2022 10:50 AM
Hi,
The script did not work too. It's still naming it as "Macbook Air"
Any other suggestions?
Posted on 05-04-2022 11:37 AM
Are you running this from Self Service or as a policy? Polices are run as root. The two instances when we use the management user are Casper Remote and Self Service. Casper Remote uses that account to SSH into the computer. Self Service also uses that account to escalate the current user to an admin user in order to install software.
Make a .sh file and run in manually in Terminal using sudo /path/to/script.sh
If that works then something in your JAMF instance is not working properly. Check your jamf.log to see what the issue is. I ran the exact script I shared and had no issues. If you are getting permissions error chances are you will need sudo in front of all those commands.
Posted on 05-04-2022 11:49 AM
Hi rhoward,
I am running it as a Policy, but I made it available too on Self Service. I will check our Jamf Log. Thanks again for your help. Really appreciate it. Have a great one!
Posted on 05-04-2022 11:58 AM
Okay, I will try to add the parenthesis (). Thanks again!
Posted on 05-04-2022 11:56 AM
honestly when working on a script its usually easier to first do manually, line by line checking the variable outputs, then put into a .sh (as mentioned) and run it locally, then copy out to jamf. Speaking generally of course, the grep command given worked flawlessly for me as well. Although for your desired out come the set lines I think should look like this: scutil --set ComputerName "M1-$serialNumber" .. that may not be exact.. I have spent to much time in powershell lately were I would have "M1-$($serialnumber)"
Posted on 05-04-2022 10:36 AM
Okay, thanks again. I will try that script.
Posted on 05-04-2022 09:54 AM
Do I still need to add this command on the script?
$ sudo jamf setComputerName -name $M1-$serialNumber
Thanks again!
Posted on 05-04-2022 09:56 AM
No you do not. Also you won't ever need sudo as JAMF runs the script as sudo by default.
Posted on 05-04-2022 10:02 AM
Okay, Thanks again @howie_isaacks !
Posted on 05-04-2022 09:48 AM
My pleasure. Check this script that I use to name computers while DEPNotify is running. This might help. I use a naming scheme that uses my client company's initials and the user's first and last initial in the computer name.
#!/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. 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
Posted on 05-04-2022 09:51 AM
Yep, you need to define your variables here. Something like this should do:
#!/bin/bash
serialNumber=$(system_profiler SPHardwareDataType | grep -i "Serial Number (system):" | /usr/bin/awk ' {print $4 }')
scutil --set ComputerName $serialNumber
scutil --set LocalHostName $serialNumber
scutil --set HostName $serialNumber
exit
Posted on 05-04-2022 09:58 AM
This is why I love Jamf Nation! I have never had to query a serial number before to use it in a script so I took some time to learn how before I replied. So I have a new skill now and we're helping someone else gain a new skill too!
Posted on 05-04-2022 10:37 AM
I think the jamf nation apple scripting training video has a few things with Variables (were it seems you misunderstanding was) you should check it out.
Posted on 05-04-2022 12:51 PM
The scripting series training in Jamf's training site is very helpful. I recommend it for anyone who needs to learn scripting. I have an enormous library of scripts that I pull code from so I don't have to memorize everything. I just remember that I solved certain problems with a script and I can recycle that code over and over again.... unless Apple changes something in macOS that screws that up 🤯
Posted on 05-04-2022 12:58 PM
I feel like I am relearning certain things once a year (things I do not do every day) .. recently not only had to relearn power shell since i had been so focused on building out the apple side of the house for the last two years, then had to add some vmware power cli into the mix for our vm environment.. its the smallest character that can just break a for each loop and have one burning an entire day (or two)
Posted on 05-04-2022 01:49 PM
Still did not work. Hopefully it's not related with the New OS Monterey. I'll keep digging. Thank you!
Posted on 05-05-2022 05:59 AM
This is the exact script I use to name my MacBooks by SN. It works every time. I have it set to run after enrollment. You should be able to edit it add M1 at the begining as others have stated. We are 100% Big Sur and newer in our environment but only tested on Monterey. I'd start with what jpeters21 said with "M1-$serialNumber" in the scutil --set entries.
#!/usr/bin/env bash
# Get the Serial Number of the Machine
sn=$(system_profiler SPHardwareDataType | awk '/Serial/ {print $4}')
# Set the ComputerName, HostName and LocalHostName
scutil --set ComputerName $sn
scutil --set HostName $sn
scutil --set LocalHostName $sn
Posted on 05-05-2022 06:39 AM
It shouldn't be. The commands to change computer name, host name, and local host name have been the same for a long time. They still work with macOS Monterey.
Posted on 05-05-2022 06:44 AM
I have noticed that sometimes the local host name does not get changed. I thought maybe adding in a "sleep 5" after each command might help but it doesn't appear so. I haven't had time to focus on figuring out why. My script is used in a policy that runs from the DEPNotify script. If I add the script to a policy that runs at check-in all three name settings get changed. I don't see how the trigger would matter. The policy that runs during the enrollment process is a custom trigger that is added to the policy array.
Posted on 05-05-2022 06:47 AM
Honestly haven't seen that issue myself but like I said it is a separate policy that runs during enrollment.
Posted on 05-05-2022 06:49 AM
It doesn't do any harm for me to run the policy again. I'm sure I will figure it out. It doesn't happen often enough to light a fire under me to get it solved.