Renaming computers with partial serial # + prefix/suffix

bcheney
Release Candidate Programs Tester

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'
1 ACCEPTED SOLUTION

mm2270
Legendary Contributor III

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

View solution in original post

10 REPLIES 10

Cornoir
Contributor II

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

chris_kemp
Contributor III

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

mm2270
Legendary Contributor III

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. :)

bcheney
Release Candidate Programs Tester

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

bcheney
Release Candidate Programs Tester

@mm2270

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

bcheney
Release Candidate Programs Tester
 

mm2270
Legendary Contributor III

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

geoffrepoli
Contributor

If you want to just snip that tail part off:

ioreg -rd1 -c IOPlatformExpertDevice | awk -F" '/S.*N/{print $(NF-1)}'

mm2270
Legendary Contributor III

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.

geoffrepoli
Contributor

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