Rename Computers Based on Serial Number

Not applicable

Here is my problem. I want to wright a script to rename my computer based on the serial number of the computer and also in include a prefix of GISD-. I have found out the you can do a grep on the serial number from system profiler but that is as far as I can get.

Please Help

Brian Weeks

![external image link](attachments/b43c7fb4aa7d4f51a5937e05b3eb75ae)
Brian Weeks
Systems Engineer
Groesbeck ISD
1202 N. Ellis
Groesbeck, TX

Office: (254) 729-4130
Email: bweeks at groesbeckisd.net<mailto:bweeks at groesbeckisd.net>
Website: http://www.groesbeckisd.net<https://gisdexch.groesbeck.k12.tx.us/owa/redir.aspx?C=39a81fe758b64100bcaaf454287d183f&URL=http%3a%2f%2fwww.groesbeckisd.net>

9 REPLIES 9

rockpapergoat
Contributor III

Here's an example:

#!/bin/bash

base="GISD-"
tld="example.com"
serial=/usr/sbin/system_profiler SPHardwareDataType | /usr/bin/awk '/Serial Number (system)/ {print $NF}'
/usr/sbin/scutil --set ComputerName $base$serial
/usr/sbin/scutil --set LocalHostName $base$serial
/usr/sbin/scutil --set HostName $base$serial.tld

Practice writing scripts so you can get more familiar with this stuff. If you're managing Macs, you need to be comfortable with the command line.

Not applicable

That line should read:

/usr/sbin/scutil --set HostName $base$serial.$tld

Or better yet:

/usr/sbin/scutil --set HostName "${base}${serial}.${tld}"

This way ensures nothing unexpected happens, when expanding the variables or running the command. Be sure to test it, of course.

rockpapergoat
Contributor III

True enough.

Also, never just run something you've copied and pasted from someone else without understanding what it does…

dwsak
New Contributor

I was trying to use a script similar to this to set hostname at deploy time, but when testing I noticed that it doesn't change the remote login name (under system preferences|Sharing) Does anyone know how to change that name too?

rob_potvin
Contributor III
Contributor III

You could just do this with the JAMF binary

#!/bin/sh
/usr/sbin/jamf setComputerName -useSerialNumber

Even more options:

Usage:   jamf setComputerName [-target <target volume>] [-name <name>]
         [-useMACAddress] [-useSerialNumber] [-suffix <suffix>]
         [-prefix <prefix>] [-fromFile <path to file>]


     -target         The target drive to set the name on

     -name           The new name for the computer

     -useMACAddress      Generate the name using the MAC Address

     -useSerialNumber    Generate the name using the Serial Number

     -prefix         Add this prefix to the MAC Address or Serial Number

     -suffix         Add this suffix to the MAC Address or Serial Number

     -fromFile       The path to a CSV file containing the computer's MAC Address or Serial Number followed by the desired name

jyergatian
Contributor

I'm looking to automate our naming convention during Casper Imaging. Here is what I have now, I expect to have this run At Reboot.

My issue is the Suffix is not recorded. I've attempted to escape the dash with double quotes, double dash, forward slash, and backslash - none of which do as I like. Any Ideas?

#!/bin/sh
/usr/sbin/jamf setComputerName -useSerialNumber -prefix 'LT-' -suffix '-M'

erickj
New Contributor II

Did anyone get this to work? Also interested of naming to the same similar convention. Would like to hear opinions on what is the best avenue to take on this process.

adamcodega
Valued Contributor

I tested on version 9.81 and it seems like some sort of bug.

acodega:~ adamcodega$ sudo jamf setComputerName -useSerialNumber -prefix 'LT-' -suffix 'M'
Set Computer Name to LT-C02N940YG5RPM
acodega:~ adamcodega$ sudo jamf setComputerName -useSerialNumber -prefix 'LT-' -suffix '.M'
Set Computer Name to LT-C02N940YG5RP.M
acodega:~ adamcodega$ sudo jamf setComputerName -useSerialNumber -prefix 'LT-' -suffix '-M'
Set Computer Name to LT-C02N940YG5RP
acodega:~ adamcodega$ sudo jamf setComputerName -useSerialNumber -prefix 'LT-' -suffix '--M'
Set Computer Name to LT-C02N940YG5RP
acodega:~ adamcodega$ sudo jamf setComputerName -useSerialNumber -prefix 'LT-' -suffix '/M'
Set Computer Name to LT-C02N940YG5RP/M

Did anyone contact Support?

mm2270
Legendary Contributor III

FWIW, I did the same test as @adamcodega above using 9.73 and see the same issue. So its not a new bug in 9.81 so you know. Its dropping the suffix if there is a hyphen at the start of the suffix text. I tried it with double quotes surrounding the text and saw the same issue, so it doesn't have anything to do with the quotes. Seems support needs to be contacted so they can reproduce it and create a defect for it.