Posted on 07-03-2017 12:06 PM
Good afternoon everyone!
I'm looking through the forums and i'm wondering if there's an easy way to pull just the last six digits of the serial number for the computer name. Below is what I have for creating the name with the full serial.
Thanks for any help!
#!/bin/sh
/usr/sbin/jamf setComputerName -useSerialNumber -prefix 'BSM' -suffix 'AIR'
Solved! Go to Solution.
Posted on 07-03-2017 01:30 PM
@bcheney Almost.
To make serial
into a valid variable, you have to enclose the command in either backticks (not as preferable) or the $(command)
syntax. So that line should look like this instead
serial=$(ioreg -rd1 -c IOPlatformExpertDevice | awk -F'"' '/IOPlatformSerialNumber/{print $4}' | tail -c 7)
Posted on 07-03-2017 12:20 PM
This command will get you the last 6 digits of the serial:
set serial to (do shell script "system_profiler SPHardwareDataType|grep 'Serial Number'|awk '{print $4}'|cut -c 7-12")
Posted on 07-03-2017 12:27 PM
another variation that should do it:
#!/bin/sh
serial=`system_profiler SPHardwareDataType | awk '/Serial/ {print $4}' | grep -o '......$'`
/usr/sbin/jamf setComputerName -prefix 'BSM' -suffix 'AIR' -name $serial
Posted on 07-03-2017 12:51 PM
Yet another variation on getting the last 6 characters of the serial number that doesn't rely on system_profiler
ioreg -rd1 -c IOPlatformExpertDevice | awk -F'"' '/IOPlatformSerialNumber/{print $4}' | tail -c 7
@chris.kemp You must have a pretty old version of the JSS if you're still using /usr/sbin/jamf as the path to the binary. :)
Posted on 07-03-2017 01:14 PM
Okay --
Setting up my test machine now. I think i've had the path to binary wrong in my first code @mm2270
Here is what I have, I'll know progress in a few minutes after this OS installs. Testing for DEP deployment next month!
Thank you for the quick responses everyone!
#!/bin/sh
serial=`system_profiler SPHardwareDataType | awk '/Serial/ {print $4}' | grep -o '......$'`
/usr/local/jamf/bin/jamf setComputerName -prefix 'BSM' -suffix 'AIR' -name $serial
Posted on 07-03-2017 01:16 PM
Using your code would be this?
#!/bin/sh
serial='ioreg -rd1 -c IOPlatformExpertDevice | awk -F'"' '/IOPlatformSerialNumber/{print $4}' | tail -c 7'
/usr/local/jamf/bin/jamf setComputerName -prefix 'BSM' -suffix 'AIR' -name $serial
Posted on 07-03-2017 01:19 PM
Posted on 07-03-2017 01:30 PM
@bcheney Almost.
To make serial
into a valid variable, you have to enclose the command in either backticks (not as preferable) or the $(command)
syntax. So that line should look like this instead
serial=$(ioreg -rd1 -c IOPlatformExpertDevice | awk -F'"' '/IOPlatformSerialNumber/{print $4}' | tail -c 7)
Posted on 07-03-2017 04:05 PM
If you want to just snip that tail
part off:
ioreg -rd1 -c IOPlatformExpertDevice | awk -F" '/S.*N/{print $(NF-1)}'
Posted on 07-03-2017 07:47 PM
Hey @grepoli The tail
was actually to grab the last 6 characters of the serial number as requested by @bcheney in the original post. I couldn't figure out a way of not using something like tail to get those. Your awk skills may be better than mine, so if you know of a way to get the serial's last 6 without needing to pipe it into another process, I would love to know myself. Maybe sed
can do it, but I didn't really try.
I know if the serial is first stored into a variable you can then use bash variable expansion to get the last 6, but that's an extra step regardless.
Posted on 10-31-2017 02:31 PM
@mm2270 this is super late, guess i'm not receiving notifications. tail -c 7
works fine but if u wanted it all in the awk string you'd just add the substr tool and specify the character position to print from: print substr(<column>,<character position>)
. We want the output of the second-to-last column ($(NF-1)
) beginning with the 7th character, so it'd look like:
ioreg -rd1 -c IOPlatformExpertDevice | awk -F" '/S.*N/{print substr($(NF-1),7)}'