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.
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.
True enough.
Also, never just run something you've copied and pasted from someone else without understanding what it does…
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?
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
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'
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.
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?
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.