convention computer naming base on serial number policy

bmee
Contributor

I've been looking for a scrip pick up the machine serial number and change the machine name, local host name and host name to our normal naming convention base of the serial number.

Example:

Machine serial number: W1234567890JH; change host, local and computer name to: mac12345

I need to create a policy that I can update or add new serial number and computer name on it.

14 REPLIES 14

ryan_ball
Valued Contributor

There are a ton of threads where folks want to do this exact same thing.

Here is a quick example:

jamf setComputerName -useSerialNumber

Here are the results when I search for "rename mac serial"
https://www.jamf.com/jamf-nation/search?query=rename+mac+serial

bmee
Contributor

I don't want the computer name to be the serial number. I want the serial number equal to the asset number thats label on the computer.

The machine has to be name exactly on how it's label on it or it'll through all of our certs and av tools off.

I see ton of threads but couldn't find one that do that. Sorry for the confusion.

hkabik
Valued Contributor

Sounds like a job for sed.

#!/bin/sh

Serial=$(ioreg -c IOPlatformExpertDevice -d 2 | awk -F" '/IOPlatformSerialNumber/{print $(NF-1)}')
Name=mac$(echo $Serial | sed 's/.//;s/.{6}$//')

scutil --set ComputerName $Name
scutil --set LocalHostName $Name
scutil --set HostName $Name

killall -HUP mDNSResponder

sleep 2

Per your example above this will trim the 1st character and the last 6 characters from the serial, append mac in front of it and set the new name as requested.

Edit... I added a DNS Cache Refresh in there at the end for good measure.

hkabik
Valued Contributor

So @meexiong did that do it for you?

bmee
Contributor

@hkabik where do i put in the machine name and suppose to match the serial number?

ryan_ball
Valued Contributor

@meexiong It is too hard to determine what you are looking for based on your responses.

Here is an example of a serial number: C18MD9CQDY4J

What would you want an example machine name to be based on that serial number?

bmee
Contributor

So I need to serial number C18MD9CQDY4J to rename to mac25698 and then then next serial number C02XN0BVJHD3 to rename to mac25645.

Can something like that be done? (Adding the serial number and naming convention into the script)

mm2270
Legendary Contributor III

I'm not sure any of us really understand what you are asking here @meexiong Where is 25698 coming from based on the serial number C18MD9CQDY4J in your example? I don't see that string within the example serial. Same with the second example. Not seeing the pattern there at all. Is it a random number or iterating from a previous value somewhere? Or is there a list of serials somewhere that correspond to Mac names and you're looking to match them up?
And lastly, is that the name you want for the Mac? Because hopefully you aren't looking to change the actual serial number itself since that's not possible.

ifbell
Contributor

I have done something similar to what you are asking.

newfront="MAC"
setserial=$(system_profiler SPHardwareDataType | grep 'Serial Number' | awk '{print $4}' | tail -c -6) #this grabs the last 6 of the serial number
machinename="$newfront$setserial"
scutil --set HostName $machinename
# jamf setComputerName -name $machinename
scutil --set ComputerName $machinename
scutil --set LocalHostName $machinename

bmee
Contributor

thats number is the asset tag from the machine. That asset tag is generated by our asset management team and its tie to the serial number of the mac. that asset tag number will never change until the machine leaves our company.

The end goal is to get script in jamf with all the asset number associate with the machine's serial number. The script would rename the machine name to the asset number that are associate with the machine's serial number.

hkabik
Valued Contributor

Oh I thought you puled the number from the serial, as in your example:

"Machine serial number: W1234567890JH; change host, local and computer name to: mac12345"

I assumed you wanted the 2nd through 6th character appended to mac to name it, thus that's what I wrote that script to do. Determine serial, pull characters 2-6 and append the "mac" prefix to it, then apply that name.

sorry, misunderstood your needs.

bmee
Contributor

Sorry for the confusion but is something like that can be done?

damienbarrett
Valued Contributor

This is likely what I'll be doing with our new fleet of laptops in May/June. It allows you to enroll the laptops using DEP --> JSS/MDM, and then have a policy that will name the machine based on a Google Sheet. I haven't tried this yet, but will be doing so in about a week (waiting on new test hardware to show up).

https://www.macblog.org/post/automatically-renaming-computers-from-a-google-sheet-with-jamf-pro/

bmee
Contributor

@damienbarrett can cvs file host it on the on-prem jamf server?