LocalHostName change error

Joostvantwout
New Contributor III
Hi, I got some issues with the computer name changes in my environment. Changing the computer names directly after the prestage enrolment and changing the computers based on serial number (HMserial). Everything is correct except for the LocalHostName change. Unfortunately this does not happen for all devices but just for some of them. Script I use: #!/bin/bash #Get Serial number SerialNumber=$(system_profiler SPHardwareDataType | awk '/Serial/ {print $4}') scutil --set ComputerName "HM-$SerialNumber" scutil --set HostName "HM-$SerialNumber" scutil --set LocalHostName "HM-$SerialNumber" ComputerName and HostName are changing correctly but for the LocalHostName change I receive a failure: SCPreferencesSetLocalHostName() failed: Invalid argument The strange part is that this is not happening for al computers but just for some of them. Anyone has an idea what can be the cause of this error and how to solve it? Help is appreciated!
1 ACCEPTED SOLUTION

Joostvantwout
New Contributor III

Got it working via a workaround script:

#!/bin/bash

 

#Grab current computer name

computername=$(scutil --get ComputerName)

echo "Computername: $computername"

 

#Grab current LocalHostName

LocalHostName=`/usr/sbin/scutil --get LocalHostName`

echo "LocalHostName: $LocalHostName"

 

if [ "$computername" == "$LocalHostName" ]; then

echo "Computername and LocalHostName is a match"

else

echo "Computername and LocalHostName do not match"

echo "$computername" >> /Users/Shared/CorrectHostName.txt

File="/Users/Shared/CorrectHostName.txt"

Hostname=$(cat "$File")

echo "LocalHostname will be changed to: $Hostname"

sudo scutil --set LocalHostName "$HostName"

sleep 3

LocalHostNameChanged=`/usr/sbin/scutil --get LocalHostName`

echo "LocalHostName after change: $LocalHostNameChanged"

rm /Users/Shared/CorrectHostName.txt

fi

 

View solution in original post

3 REPLIES 3

efil4xiN
Contributor II

we have used the following for the last few years. I  do not recall seeing that error. Maybe order matters or the jamf command is masking it in our script,hth

 

scutil --set ComputerName $SerialNumber
scutil --set LocalHostName $SerialNumber
scutil --set HostName $SerialNumber
jamf setComputerName -name $SerialNumber

jamf recon

Joostvantwout
New Contributor III

Like I said, there is nothing wrong with the script itself as on half of my Mac's it's changing the names just fine. It's just for a couple of Mac's where we receive the error: SCPreferencesSetLocalHostName() failed: Invalid argument
Manual changing is not an option.

Joostvantwout
New Contributor III

Got it working via a workaround script:

#!/bin/bash

 

#Grab current computer name

computername=$(scutil --get ComputerName)

echo "Computername: $computername"

 

#Grab current LocalHostName

LocalHostName=`/usr/sbin/scutil --get LocalHostName`

echo "LocalHostName: $LocalHostName"

 

if [ "$computername" == "$LocalHostName" ]; then

echo "Computername and LocalHostName is a match"

else

echo "Computername and LocalHostName do not match"

echo "$computername" >> /Users/Shared/CorrectHostName.txt

File="/Users/Shared/CorrectHostName.txt"

Hostname=$(cat "$File")

echo "LocalHostname will be changed to: $Hostname"

sudo scutil --set LocalHostName "$HostName"

sleep 3

LocalHostNameChanged=`/usr/sbin/scutil --get LocalHostName`

echo "LocalHostName after change: $LocalHostNameChanged"

rm /Users/Shared/CorrectHostName.txt

fi