Jamf computer naming script

_aDiedericks
Contributor

Hi there,

 

I know this topic was covered quite a bit but I'm not able to find something that fits our requirements.
I'm looking for a script that will rename a devices on jamf to the following format:

"FirstName LastName - MacBookModel - S/N"

And what is the best methodology to trigger this script, I found that with the previous script when the policy is set to run during enrolment and I left the device at the account creation page for 5 minutes, the device would be named "_mbsetupuser" so to avoid that is there any way to create a custom trigger for a device that was logged in for the first time and have the policy and script run on that trigger?

 

1 ACCEPTED SOLUTION

karthikeyan_mac
Valued Contributor

@_aDiedericks Do you need spaces in the Computer Name? Recommended to avoid space. You can try something like this.

#!/bin/bash

userName=$(/bin/ls -l /dev/console | /usr/bin/awk '{ print $3 }')
#echo $userName

#fullName=$(sudo -u $userName id -F) # Uncomment this if you need space in Full Name 

fullName=$(sudo -u $userName id -F | tr -d " \t\n\r") # this will remove space from Full Name. Comment this line if you need space
#echo $fullName
model=$(sysctl hw.model | awk '{ print $2 }' | rev | cut -c5- | rev )
#echo $model
serialNumber=$(system_profiler SPHardwareDataType | awk '/Serial/ {print $4}')
#echo $serialNumber

name=$fullName-$model-$serialNumber
#echo $name

scutil --set HostName $name
scutil --set LocalHostName $name
scutil --set ComputerName $name

or you can also use Jamf binary instead of scutil.

jamf setComputerName -name $name

Thanks

 

View solution in original post

22 REPLIES 22

karthikeyan_mac
Valued Contributor

@_aDiedericks Do you need spaces in the Computer Name? Recommended to avoid space. You can try something like this.

#!/bin/bash

userName=$(/bin/ls -l /dev/console | /usr/bin/awk '{ print $3 }')
#echo $userName

#fullName=$(sudo -u $userName id -F) # Uncomment this if you need space in Full Name 

fullName=$(sudo -u $userName id -F | tr -d " \t\n\r") # this will remove space from Full Name. Comment this line if you need space
#echo $fullName
model=$(sysctl hw.model | awk '{ print $2 }' | rev | cut -c5- | rev )
#echo $model
serialNumber=$(system_profiler SPHardwareDataType | awk '/Serial/ {print $4}')
#echo $serialNumber

name=$fullName-$model-$serialNumber
#echo $name

scutil --set HostName $name
scutil --set LocalHostName $name
scutil --set ComputerName $name

or you can also use Jamf binary instead of scutil.

jamf setComputerName -name $name

Thanks

 

Be aware that if these devices are going to be bound to Active Directory, that naming scheme will create names that are far too long for AD. Active Directory is limited to 16 characters in a computer name.

@AVmcclint Thanks for your response. We don't use AD from our side. This might help someone else who's planning to implement this same naming scheme.

Thanks for your speedy response @karthikeyan_mac 

Below is how I have this script configured. I've tried to deploy it to a MacOS Ventura test device and it doesn't seem to do change anything.

I'm not too familiar with these scripts most likely it's something on my end. It does deploy successfully but just doesn't change anything,

Screenshot 2022-11-28 at 14.58.09.png

@karthikeyan_mac My apologies I realized I had to manually run an inventory update through "sudo jamf recon" to get this data to propagate to jamf.
I'll leave this here in case someone else finds the same issue.

Just add the "Maintenance" as part of the policy, that will update the inventory

Yes, I usually do. In this case just because I was testing first I didn't select maintenance and forgot to manually run the inventory update. Thanks.

@karthikeyan_mac Just testing out the name with space option. Seems the end result is a device named with only the first name and nothing else. 

This is the config of the script I tested with.

 

#!/bin/bash

userName=$(/bin/ls -l /dev/console | /usr/bin/awk '{ print $3 }')
#echo $userName

fullName=$(sudo -u $userName id -F) # Uncomment this if you need space in Full Name 

#fullName=$(sudo -u $userName id -F | tr -d " \t\n\r") # this will remove space from Full Name. Comment this line if you need space
#echo $fullName
model=$(sysctl hw.model | awk '{ print $2 }' | rev | cut -c5- | rev )
#echo $model
serialNumber=$(system_profiler SPHardwareDataType | awk '/Serial/ {print $4}')
#echo $serialNumber

name=$fullName-$model-$serialNumber
#echo $name

scutil --set HostName $name
scutil --set LocalHostName $name
scutil --set ComputerName $name

 

In this script, the variable userName is pulling the logged-in username not the full name. So whatever you have the logged-in user account set as, is what will be pulled.. 

If you want to pull the full name no matter the logged-in username, you can try replacing the userName variable with this.. id -P $(stat -f%Su /dev/console) | awk -F '[:]' '{print $8}'

@_aDiedericks Uncomment the echo for the fullName and name variable to check whats being captured. Its working on my device. 

Screenshot 2022-11-28 at 7.22.03 PM.png
Thanks

Strangely the output is correct. It's the full name but when I check About it shows the name only as the first name. 

Also I just noticed M2 MacBooks are named "FirstNameLastName-Mac-S/N". For model it's just called "Mac".

@_aDiedericks Just noticed M2 Macs have a different model identifier. We can get the model from system_profiler. Updated the model variable in script.

 

#!/bin/bash

userName=$(/bin/ls -l /dev/console | /usr/bin/awk '{ print $3 }')
#echo $userName

fullName=$(sudo -u $userName id -F) # Uncomment this if you need space in Full Name 

#fullName=$(sudo -u $userName id -F | tr -d " \t\n\r") # this will remove space from Full Name. Comment this line if you need space
#echo $fullName
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=$fullName-$model-$serialNumber
#echo $name

scutil --set HostName $name
scutil --set LocalHostName $name
scutil --set ComputerName $name

 

Thanks

@karthikeyan_mac It looks like it still only pulls in the first name but if I run the script locally with the "echo $fullName" uncommented then I get full name correctly outputted but still only displaying first name in jamf and computer is renamed to first name only.


@JustDeWon added some information that might resolve this, but I'm not sure how to utilise it in the script without causing errors. I've tried a few times.

 

 

If you want to pull the full name no matter the logged-in username, you can try replacing the userName variable with this.. id -P $(stat -f%Su /dev/console) | awk -F '[:]' '{print $8}'

 

 

 

 

Using the script @karthikeyan_mac   provided, you can comment out the userName and update the fullName variable with the command I suggested. You just need to determine if you want spaces or not. This is configured with no spaces. See example below.. 

 

 

 

#!/bin/sh

#userName=$(/bin/ls -l /dev/console | /usr/bin/awk '{ print $3 }')
#echo $userName

#fullName=$(id -P $(stat -f%Su /dev/console) | awk -F '[:]' '{print $8}') # Uncomment this if you need space in Full Name 

fullName=$(id -P $(stat -f%Su /dev/console) | awk -F '[:]' '{print $8}'| tr -d " \t\n\r") # this will remove space from Full Name. Comment this line if you need space
echo $fullName

 

 

 

Thanks @JustDeWon  I've tried it now with space and the outcome is the same as with @karthikeyan_mac.

Seems the outputs are all correct but the final renaming under About is just using first name. Same for jamf. This might be because I'm on Ventura?

What happens with No space?

@_aDiedericks This is because of space in the value of variable $name. Weed need to use "${name}".

Note: LocalHostName does not accept space in the name and its not recommended to have space in the computer/hostname. You may need to reconsider having spaces. LocalHostName with space will show error as "SCPreferencesSetLocalHostName() failed: Invalid argument".

#!/bin/bash

userName=$(/bin/ls -l /dev/console | /usr/bin/awk '{ print $3 }')
#echo $userName

fullName=$(sudo -u $userName id -F) # Uncomment this if you need space in Full Name 

#fullName=$(sudo -u $userName id -F | tr -d " \t\n\r") # this will remove space from Full Name. Comment this line if you need space
#echo $fullName
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=$fullName-$model-$serialNumber
#echo $name

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

 

Thanks

@karthikeyan_mac Looks like now none of the M2's display any model info in the name, it just says "FirstNameLastName-" with no model info.

 

[UPDATE]

Excuse my mistake, I see profiler is spelt incorrectly in your first post. I've changed it now and is reporting the correct data on non-m2 models, I will test it with M2 models now.

I've been using the Jamf binary to pull the name from a csv stored in /var/tmp. These are public/lab computers bound to AD. For example: sudo /usr/local/jamf/bin/jamf setComputerName -fromFile /var/tmp/LabComputerNames.csv. Recently using 12.6.1 it returns the error 

"*** -[__NSSingleObjectArrayI objectAtIndex:]: index 1 beyond bounds [0 .. 0]"

I haven't tried this with 13.x but I assume it's still broken. Any suggestions?

mcrispin
Contributor II

I'm sure you have your reasons, but you should know that computer names are broadcast over networks. Adding any identifiable information in a computer names goes against CIS and PII guidance. There are a lot of great suggestions here, but I would raise this issue with your security folks and consider less identifiable options. There is a ton of room here that may allow a social hack.

Hopefully there's another one for us to name device directly on jamf, that's the only reason we need these measures 

If you are using a directory service, and that directory service is mapped in such a way that the user short name maps with a corresponding field in Jamf Pro, Jamf Pro will connect that device to the user record automatically.