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
1#!/bin/sh
2/usr/sbin/jamf setComputerName -useSerialNumber
Even more options:
1Usage: jamf setComputerName [-target <target volume>] [-name <name>]
2 [-useMACAddress] [-useSerialNumber] [-suffix <suffix>]
3 [-prefix <prefix>] [-fromFile <path to file>]
4
5
6 -target The target drive to set the name on
7
8 -name The new name for the computer
9
10 -useMACAddress Generate the name using the MAC Address
11
12 -useSerialNumber Generate the name using the Serial Number
13
14 -prefix Add this prefix to the MAC Address or Serial Number
15
16 -suffix Add this suffix to the MAC Address or Serial Number
17
18 -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?
1#!/bin/sh
2/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.
1acodega:~ adamcodega$ sudo jamf setComputerName -useSerialNumber -prefix 'LT-' -suffix 'M'
2Set Computer Name to LT-C02N940YG5RPM
3acodega:~ adamcodega$ sudo jamf setComputerName -useSerialNumber -prefix 'LT-' -suffix '.M'
4Set Computer Name to LT-C02N940YG5RP.M
5acodega:~ adamcodega$ sudo jamf setComputerName -useSerialNumber -prefix 'LT-' -suffix '-M'
6Set Computer Name to LT-C02N940YG5RP
7acodega:~ adamcodega$ sudo jamf setComputerName -useSerialNumber -prefix 'LT-' -suffix '--M'
8Set Computer Name to LT-C02N940YG5RP
9acodega:~ adamcodega$ sudo jamf setComputerName -useSerialNumber -prefix 'LT-' -suffix '/M'
10Set 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.