Worklet - Hostname Standardization

This worklet can be run against Mac Hosts to standardize hostnames. This is valuable to our org in that we have a hostname standard of username-macbookModel that helps us identify the machine. You can change whatever values you’d like to accomodate your org.

Evaluation Code:

pro="-MBPro"
air="-MBAir"
emp=$(stat -f%Su /dev/console)

mbpro="${emp}${pro}"
mbair="${emp}${air}"

if [[ $HOSTNAME = "${mbpro}" ]] || [[ $HOSTNAME = "${mbair}" ]]; then
  exit 0
else
  exit 1 
fi

Remediation code:

#!/bin/bash
# The script will identify the current logged in user and machine type and it will
# run as root and will set hostname to: user-computer type based on the information returned

pro="-MBPro"
air="-MBAir"
model=$(sysctl hw.model | sed 's/[0-9,]//g' | awk '/^[a-zA-Z]/ {print $2}')
emp=$(stat -f%Su /dev/console)

case "${model}" in
    "MacBookPro" )
    sudo scutil --set ComputerName "${emp}${pro}"
    sudo scutil --set LocalHostName "${emp}${pro}"
    sudo scutil --set HostName "${emp}${pro}"
    sudo defaults write /Library/Preferences/SystemConfiguration/com.apple.smb.server NetBIOSName -string "${emp}${pro}"

    ;;
    "MacBookAir" )
    sudo scutil --set ComputerName "${emp}${air}"
    sudo scutil --set LocalHostName "${emp}${air}"
    sudo scutil --set HostName "${emp}${air}"
    sudo defaults write /Library/Preferences/SystemConfiguration/com.apple.smb.server NetBIOSName -string "${emp}${air}"
esac
2 Likes