Sonoma Device Naming Script

_aDiedericks
Contributor

HI there,

We've been using a script to set the computer name on devices in the format of 'fullname-model'. This script has been working fine up until Sonoma where it breaks by not providing the full name variable. The only script that seems to work at the moment is one that pulls the username instead but this is not ideal for our environment due to some historic devices having usernames completely unrelated to the user.

Is anyone using a script that is able to pull full name from a source that works on Monterey, Ventura and Sonoma in the 'fullName-model' format?

This is what we have now

#!/bin/bash

userName=$(/bin/ls -l /dev/console | /usr/bin/awk '{ print $3 }')
echo $userName
model=$(system_profiler SPHardwareDataType | grep "Model Name:" | sed 's/.*://' | tr -d " \t\n\r")
echo $model
serialNumber=$(system_profiler SPHardwareDataType | awk '/Serial/ {print $4}')
echo $serialNumber

name=$userName-$model-$serialNumber
echo $name

scutil --set HostName "${name}"
scutil --set LocalHostName "${name}"
scutil --set ComputerName "${name}"

 

1 ACCEPTED SOLUTION

howie_isaacks
Valued Contributor II

After using the "userName" variable, this command will give you the full name of the user:

finger -s $userName | head -2 | tail -n 1 | /usr/bin/awk '{print $2,$3}'

This will result in FirstName LastName. If you want to remove the space between the first name and last name, you need to add a sed command to the end.

finger -s $userName | head -2 | tail -n 1 | /usr/bin/awk '{print $2,$3}' | sed 's/[[:blank:]]//g'

To make this a variable you could write something like:

fullName=$(finger -s $userName | head -2 | tail -n 1 | /usr/bin/awk '{print $2,$3}' | sed 's/[[:blank:]]//g')

echo $fullName

 I tested all of this in Sonoma.

View solution in original post

9 REPLIES 9

mm2270
Legendary Contributor III

Where in this script are you trying to grab the full name? I only see you getting the short username.

Also, I don't think it's wise to try to pull a full name as part of a computer name. There could be odd characters, spaces and other things in that which could cause some issues when using it as a host name. Just my $0.02 on the issue. I'd use something more computer/device specific for this. Why not just use the model and serial number like you already have and drop the user name?

The username/fullname are the only identifying variables for the devices. In order for us to confirm a user just based on serial number we would either have to check our Asset Manager or directly navigate into serial numbers to see the local account associated with the devices.

We currently dont have 'User and location' populated since we use Okta I don't think it can be used to generate this data which would've allowed us to make use of S/N in the device name and create an advanced search filter looking at the User and location data. I may be wrong though.

Rhio
New Contributor III
That’s what I was curious by if it was your reasoning.

If you go into your Settings in Jamf then Inventory Display, you can change what shows up in a computer search. If you go under the User and Location and select username. Now when you’re browsing your inventory by hitting the search button with the field blank then after it lists all your devices type in a name and boom!

(You must also be collecting username from Inventory Collection tab under settings)
This email and any files transmitted with it are confidential, proprietary and intended solely for the individual or entity to whom they are addressed. If you have received this email in error please delete it immediately.

Interesting. Up until just now I thought this data was pulled from and LDAP connector like from Google Workspace or Azure. Thanks for this feedback, I'll give it a go.

howie_isaacks
Valued Contributor II

After using the "userName" variable, this command will give you the full name of the user:

finger -s $userName | head -2 | tail -n 1 | /usr/bin/awk '{print $2,$3}'

This will result in FirstName LastName. If you want to remove the space between the first name and last name, you need to add a sed command to the end.

finger -s $userName | head -2 | tail -n 1 | /usr/bin/awk '{print $2,$3}' | sed 's/[[:blank:]]//g'

To make this a variable you could write something like:

fullName=$(finger -s $userName | head -2 | tail -n 1 | /usr/bin/awk '{print $2,$3}' | sed 's/[[:blank:]]//g')

echo $fullName

 I tested all of this in Sonoma.

Rhio
New Contributor III

I'm not trying to be rude here but understand; Why would you want a naming scheme like that vs just Serial Number? Seems way more complex than required.

howie_isaacks
Valued Contributor II

But complexity is so much more fun! I have actually taught myself a lot by taking on the challenge of solving problems posted in Jamf Nation. Personally, I would not name a Mac using the user's name since that could be a security problem on a public WiFi network. I wouldn't want my name being broadcast to others. That's one reason all of my own Apple devices use Star Trek names. My MacBook Pro has been called "Enterprise" for several years.

joshuasee
Contributor III

Just since nobody has brought it up:

fullName=$(id -F $username)

 

howie_isaacks
Valued Contributor II

Well that works too! I didn't know that command.