FULL NAME as the Computer Name

psyche
New Contributor

Hi everyone 

I want to use FULL NAME as the Computer Name because there are too many devices and making changes one by one is painful. Do you have any good scripts to recommend.
Thank you.

 

Thanks

7 REPLIES 7

AJPinto
Honored Contributor II

I use a script to set the device name to the Serial Number. The script below is what I use, it can be modified to rename the computer to whatever. I do caution with using the users name as that is PII information.  

 

 

#!/usr/bin/env bash
#*=============================================================================
#* Script Name: Alias
#* Created: [08.12.22]
#* Author:
#* Purpose: Changes Mac Hostname to match SN, confirms that Domain Object is also complaint
#*=============================================================================
 

#*=============================================================================
#* GLOBAL VARIABLES
#*=============================================================================
 
# Define Active User
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
# Set Variables
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 '"')
domainAccount=$(dsconfigad -show | grep "Computer Account" | awk '{print $4}' | tr -d '$' | awk '{ print toupper($0) }')
#*=============================================================================
#* FUNCTIONS
#*=============================================================================
 
# Identify if Active user information matches
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
 

## Check & Update Domain Account
if [ "$domainAccount" == "$serialNumber" ]
then
    echo "Domain Account matches serial number, $serialNumber"
else
    echo "Current Domain Account: $domainAccount"
    echo "Domain Account does not meet standards"
    echo "Computer will need to rejoin domain under new name"
    sudo jamf policy -event fixDomainConnection
fi; $DIV1
 
## Final Check
computerNameII=$(scutil --get ComputerName)
hostNameII=$(scutil --get HostName)
localHostII=$(scutil --get LocalHostName)
domainAccountII=$(dsconfigad -show | grep "Computer Account" | awk '{print $4}' | tr -d '$' | awk '{ print toupper($0) }')
 
echo "Results:"; $DIV3
echo "Serial number: $serialNumber"
echo "Computer Name: $computerNameII"
echo "Host Name: $hostNameII"
echo "Local Host: $localHostII"
echo "Domain Account: $domainAccountII"
if [[ "$computerNameII" == "$serialNumber" ]] && [[ "$hostNameII" == "$serialNumber" ]] && [[ "$localHostII" == "$serialNumber" ]] && [[ "$domainAccountII" == "$serialNumber" ]]
then
    echo "Computer Name satisfies naming standards"
    $DIV1; exit 0
else
    echo "Computer does not meet naming standars"
    echo "More troubleshooting will be necessary."
    $DIV1; exit 1
fi
#*=============================================================================
#* END OF SCRIPT
#*=============================================================================
 

 

psyche
New Contributor

FULL NAME, that is, the naming rule of the local account is like this. For example, 2025 SA-D Zhang San, which is easy to distinguish between classes and names.

AJPinto
Honored Contributor II

It may be easy for you to distinguish, but its also easy for a malicious actor to distinguish as well. Its best to keep Personally Identifiable Information out side of locations where it is publicly visible. Ultimately its your environment, and may caution. Do what is best for your organization. 

bfrench
Contributor III

ajanicke
New Contributor III

Why not something simple like..

 

 

#!/bin/sh

lastUser=`defaults read /Library/Preferences/com.apple.loginwindow lastUserName`
scutil --set ComputerName $lastUser
sleep 5
jamf recon

 

 

 We use a slightly more in depth version of this but this is the bare bones. We set it to run at check in. Though, I assume you have an admin account and maybe more so I'd add in to check if the last person logged in was Admin1 and if so end the script.

 

I tried the script and it worked pretty well. Although the name was not displayed in Chinese font, it didn't affect it.截屏2023-10-13 07.23.38.png

ajanicke
New Contributor III

Yeah, can’t help with the fonts so much but this should be a good basis to build upon. It’s pretty reliable for a 1:1 deployment.