Why isn't my Computer Naming script working

braillle
New Contributor III

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

 

 

1 ACCEPTED SOLUTION

karthikeyan_mac
Valued Contributor

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 solution in original post

9 REPLIES 9

dwbergstrom
New Contributor III

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

cbrewer
Valued Contributor II

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

 

braillle
New Contributor III

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.

jasonz79
New Contributor

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

 

karthikeyan_mac
Valued Contributor

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

mm2270
Legendary Contributor III

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.

daniel_behan
Contributor III

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