Unable to rename laptop (Macbook Air) using a script on Policy on Jamf Pro

cmayo
New Contributor

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

26 REPLIES 26

howie_isaacks
Valued Contributor II

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?

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.

cmayo
New Contributor

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;

cmayo_0-1651684829536.png

And I have this weird name on the top right screen.

cmayo_1-1651684912285.png

Any other suggestion please? Thanks!

 

cmayo
New Contributor

Also, this is the name that registered on Jamf Pro

cmayo_2-1651685237550.png

 

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

cmayo
New Contributor

@rhoward 

Hi, 

    The script did not work too. It's still naming it as "Macbook Air"

 Any other suggestions?

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.

cmayo
New Contributor

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!

cmayo
New Contributor

Okay, I will try to add the parenthesis (). Thanks again!

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

cmayo
New Contributor

@howie_isaacks 

Okay, thanks again. I will try that script. 

Hi @howie_isaacks    

   Do I still need to add this command on the script?

$ sudo jamf setComputerName -name $M1-$serialNumber

 

Thanks again!

No you do not. Also you won't ever need sudo as JAMF runs the script as sudo by default.

cmayo
New Contributor

Okay, Thanks again @howie_isaacks !

howie_isaacks
Valued Contributor II

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

rhoward
Contributor

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

howie_isaacks
Valued Contributor II

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!

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. 

howie_isaacks
Valued Contributor II

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 🤯

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) 

cmayo
New Contributor

@jpeters21 

Still did not work. Hopefully it's not related with the New OS Monterey. I'll keep digging. Thank you!

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

howie_isaacks
Valued Contributor II

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.

howie_isaacks
Valued Contributor II

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.

Honestly haven't seen that issue myself but like I said it is a separate policy that runs during enrollment.

 


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.