Mac Names keep reverting after i change them with a Policy

PJ_WLC
New Contributor II

Hello!

 

So, Currently I have been having issues with Naming of machines, this isnt happening on ALL the machines just a handful and i can't seem to fathum why...

 

I'm using a policy that has a reccuring check-in, using the maintenance 'Reset Computer Names' to make the changes in jamf transfer onto the machine in question. 

 

Now, the names will change in jamf but after a few days it will revert back to something like 'imac(8)' and not what i previously set...

 

Any help? 

10 REPLIES 10

AJPinto
Honored Contributor II

Can you link the script you are using? MacOS has its hostname set in 3 spots and you need to change all of them.

 

 

This is the script I use, it may have some functions you dont want or need but it works.

#!/usr/bin/env bash
#*=============================================================================
#* Script Name: 
#* Created: 
#* Author: 
#*=============================================================================
#* Purpose: Requests input and renames computer based on Naming Convention 2301
#*=============================================================================

#*=============================================================================
#* REVISION HISTORY
#*=============================================================================
#* Date: 
#* Author: 
#* Issue: 
#* Solution: N/A
#*=============================================================================


#*=============================================================================
#* GLOBAL VARIABLES
#*=============================================================================
DIV1='echo ####################################################################'
DIV2='echo --------------------------------------------------------------------'
DIV3='echo ....................................................................'
ActiveUser=`/bin/ls -l /dev/console | /usr/bin/awk '{ print $3 }' | tr "[a-z]" "[A-Z]"`
ActiveUserRealName=`dscl . -read /Users/$ActiveUser | grep RealName: | cut -c11-`
if [[ -z $ActiveUserRealName ]]; then
	ActiveUserRealName=`dscl . -read /Users/$ActiveUser | awk '/^RealName:/,/^RecordName:/' | sed -n 2p | cut -c 2-`
fi
computerName=$(scutil --get ComputerName)
hostName=$(scutil --get HostName)
localHost=$(scutil --get LocalHostName)
serialNumber=$(system_profiler SPHardwareDataType | awk '/Serial/ {print $4}')
serialNumberII=$(system_profiler SPHardwareDataType | awk '/Serial/ {print $4}' | tr -d '"')
#*=============================================================================
#* FUNCTIONS
#*=============================================================================
userInfo () {
	echo; $DIV1
	echo "User Information:"
	if [[ "$ActiveUserRealName" == "$ActiveUser" ]]; then
		echo  "$ActiveUserRealName (Local Admin)"
	else 
		echo "$ActiveUserRealName ($ActiveUser)"
	fi
	$DIV1
}
#*=============================================================================
#* SCRIPT BODY
#*=============================================================================
userInfo

## Check & Update Computer Name
if [ "$computerName" == "$serialNumber" ]
then
	echo "Computer name matches serial number, $serialNumber"
else
	echo "Current Computer Name: $computerName"
	echo "Computer Name does not meet standards"
	echo "Changing Computer Name to match Serial Number"
	scutil --set ComputerName "$serialNumber"	
fi; $DIV2

## Check & Update Host Name
if [ "$hostName" == "$serialNumber" ]
then
	echo "Host Name matches serial number, $serialNumber"
else
	echo "Current Host Name: $hostName"
	echo "Host Name does not meet standards"
	echo "Changing Host Name to match Serial Number"
	scutil --set HostName "$serialNumber"
	
fi; $DIV2


## Check & Update Local Host
if [ "$localHost" == "$serialNumber" ]
then
	echo "Local Host matches serial number, $serialNumber"
else
	echo "Current Local Host: $localHost"
	echo "Local Host does not meet standards"
	echo "Changing Local Host to match Serial Number"
	scutil --set LocalHostName "$serialNumber"
	
fi; $DIV2


## Final Check
computerNameII=$(scutil --get ComputerName)
hostNameII=$(scutil --get HostName)
localHostII=$(scutil --get LocalHostName)

echo ""
echo "Serial number: $serialNumber"
echo "Computer Name: $computerNameII"
echo "Host Name: $hostNameII"
echo "Local Host: $localHostII"
if [[ "$computerNameII" == "$serialNumber" ]] && [[ "$hostNameII" == "$serialNumber" ]] && [[ "$localHostII" == "$serialNumber" ]] 
then 
	echo "Computer Name satisfies naming standards"
	$DIV1; exit 0
else
	echo "Computer does not meet naming standards"
	echo "More troubleshooting will be necessary."
	$DIV1; exit 1
fi 

## Perform final Recon to update computer name on Jamf
sudo jamf recon
#*=============================================================================
#* END OF SCRIPT
#*=============================================================================

 

PJ_WLC
New Contributor II

Thanks for the script, ill have a nosey! 

 

Currently not using a script, just the built in feature on jamf to rename machines and to change the machine name to match the one in jamf... 

 

Could explain why I'm seeing issues... 

PJ_WLC
New Contributor II

Does your script just rename it to its serial number? We have naming conventions that go by room names ect

AJPinto
Honored Contributor II

We use Serial Numbers, yes. I have other ways to identify where a device is like network locations and buildings/rooms/popup extension attributes. 

 

The only important part of the script is this bit, everything else can be changed to meet your needs.

scutil --set HostName "something here"
scutil --set LocalHostName "something here"
scutil --set ComputerName "something here"	

 

Tribruin
Valued Contributor II

Does the computer name completely change or does it just add a number (5) to the end? If it is just a number, this is an issue with bonjour network discovery. Macs use bonjour to find other network devices that can be connected to. However, it does not like having two computers with the exact same name (i.e. MacBookPro). The problem we have seen is that sometimes a computer sees itself and thinks that it is another computer and updates it's computer name. I have some computers with five digit numbers appended to the end. 

I haven't figured out how to prevent this. 

PJ_WLC
New Contributor II

It will go from something like 'Room2-Mac1' to 'imac2' 

Its really fustrating when soemthing so simple is yet so complex.

jamf-42
Valued Contributor

you change on the Mac.. jamf reflect this. if you edit the name in the device record in jamf.. next time it recons, it picks up the name from the Mac..  script is the way forward

PJ_WLC
New Contributor II

Screenshot 2023-02-16 145540.png

Would that be the case when running this? 

AJPinto
Honored Contributor II

If JAMF has something like MacBook Pro listed as the hostname it will attempt to sync that down. If there is already a "MacBook Pro" on the network, then macOS will append a number at the end like MacBook Pro (5). Host names must be unique on a network.

 

To really use that check box you need to be doing something to randomize the computer name in the inventory record. You could do this manually, or use JAMF API and some scripting. I prefer to steer clear of that noise and just adjust the hostname with a script, and use smart groups to make sure the name sticks. We use the serial number for the hostname as that is nearly assured to be unique. 

aparten
New Contributor III

You could also simplify the above script a bit and use the Jamf binary to set the name. Something like:

SerialNumber=$(system_profiler SPHardwareDataType | awk '/Serial/ {print $4}')
jamf setComputerName -name $SerialNumber

I do something like this and trim the last 7 digits of the Serial Number:

SerialNumber=$(system_profiler SPHardwareDataType | awk '/Serial/ {print $4}' | grep -o '.\{7\}$')

 Then use a standardized prefix plus the trimmed SN:

jamf setComputerName -name "UA-"$SerialNumber

For an output like: UA-ABC1234

 

Food for thought...
-Alex