Skip to main content
Solved

Why isn't my Computer Naming script working


Forum|alt.badge.img+4

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

 

 

Best answer by karthikeyan_mac

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}'

 

View original
Did this topic help you find an answer to your question?

9 replies

dwbergstrom
Forum|alt.badge.img+4
  • New Contributor
  • 14 replies
  • January 12, 2022

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}'


Forum|alt.badge.img+15
  • Esteemed Contributor
  • 719 replies
  • January 12, 2022

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

 


Forum|alt.badge.img
  • New Contributor
  • 1 reply
  • January 13, 2022

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.


karthikeyan_mac
Forum|alt.badge.img+17
  • Honored Contributor
  • 281 replies
  • Answer
  • January 13, 2022

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}'

 


mm2270
Forum|alt.badge.img+16
  • Legendary Contributor
  • 7880 replies
  • January 13, 2022

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.


Tangentism
Forum|alt.badge.img+10
  • Honored Contributor
  • 144 replies
  • January 13, 2022
jasonz79 wrote:

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

 


Forum|alt.badge.img+4
  • Author
  • Contributor
  • 20 replies
  • January 13, 2022
karthikeyan_mac wrote:

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


Forum|alt.badge.img+4
  • Author
  • Contributor
  • 20 replies
  • January 13, 2022
cbrewer wrote:

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.


Forum|alt.badge.img+11
  • Valued Contributor
  • 194 replies
  • January 19, 2022

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


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings