Skip to main content

The prefix is showing up on the device, but it's not pulling down the serial. I've even tried a few commands I've seen on these forums, and it still won't grab the serial. I am learning scripting, and only know basic commands. I took over for the previous sys admin who left, and he could never get the script to work either. 


MODEL=$(sysctl hw.model)


if [[ $MODEL == *"Book"* ]]
then
PREFIX="TRML-"
else
PREFIX="TRM-"
fi


SERIAL=$(ioreg -rd1 -c IOPlatformExpertDevice | awk -F'"' '/IOPlatformSerialNumber/{print $7}')

COMPUTERNAME="${PREFIX}${SERIAL}"

/usr/sbin/scutil --set ComputerName $COMPUTERNAME
/usr/sbin/scutil --set LocalHostName $COMPUTERNAME
/usr/sbin/scutil --set HostName $COMPUTERNAME

 

 

Here's a couple alternatives to the ioreg command to get the serial number if you wanted to try them in its place:

system_profiler SPHardwareDataType | grep "Serial Number (system)" | awk '{ print $4 }'

or

system_profiler SPHardwareDataType | awk '/Serial/ {print $4}'


I would suggest adding a sleep 1 between your scutil commands. Otherwise you may see that your name change didn't fully work.

/usr/sbin/scutil --set ComputerName $COMPUTERNAME
sleep 1
/usr/sbin/scutil --set LocalHostName $COMPUTERNAME
sleep 1
/usr/sbin/scutil --set HostName $COMPUTERNAME
sleep 1

 


I think you need the command jamf recon for uploading the new computer name to Jamf Pro. Otherwise the computer name will be changed back after next check in.


ioreg in your script does not seem to be working. Change it as below. You can use system_profile command as well. Add update inventory in the policy to report back the new name. 

ioreg -rd1 -c IOPlatformExpertDevice | awk -F'"' '/IOPlatformSerialNumber/{print $4}'

 


As @karthikeyan_mac mentioned, you have the wrong column being printed in the awk section with your original command. Change it to print $4 like noted above and it should pull the serial number. The other alternative ways to grab the serial mentioned by others here should also work.


I think you need the command jamf recon for uploading the new computer name to Jamf Pro. Otherwise the computer name will be changed back after next check in.


or use:

 

jamf setComputerName -name $COMPUTERNAME

 


ioreg in your script does not seem to be working. Change it as below. You can use system_profile command as well. Add update inventory in the policy to report back the new name. 

ioreg -rd1 -c IOPlatformExpertDevice | awk -F'"' '/IOPlatformSerialNumber/{print $4}'

 


this fixed it. Thank you so much!!!! 


I would suggest adding a sleep 1 between your scutil commands. Otherwise you may see that your name change didn't fully work.

/usr/sbin/scutil --set ComputerName $COMPUTERNAME
sleep 1
/usr/sbin/scutil --set LocalHostName $COMPUTERNAME
sleep 1
/usr/sbin/scutil --set HostName $COMPUTERNAME
sleep 1

 


I got it working with a different ioreg command, but went ahead and added sleep between each command to avoid any possible issues. Thank you for the tip.


The jamf binary can do this for you without even performing a lookup.  "/usr/local/jamf/bin/jamf setComputerName -useSerialNumber"