Skip to main content
Question

Rename Mac Script

  • February 23, 2018
  • 8 replies
  • 35 views

KyleEricson
Forum|alt.badge.img+17

I want to build a script that will find the local user excluding the local admin and use it to rename the Mac,
Example:
John Doe has a MacBook Air with a serial number C02FM2JEGTF1 The account name is his full name John Doe and his username is in our domain format first3 first last name.

I would like the name to be
johdoe-MBA-GTF1

Any ideas on how to do this?

8 replies

dsavageED
Forum|alt.badge.img+8
  • New Contributor
  • February 23, 2018

I can think how you would get close to this, but you might need a lookup table of some sort to change the computer to MBA. I've assumed the only accounts are your support account and the user.

#!/bin/sh

product_name=$(ioreg -l | awk '/product-name/ { split($0, line, """); printf("%s
", line[4]); }')

serial_no=$(ioreg -c IOPlatformExpertDevice -d 2 | awk -F" '/IOPlatformSerialNumber/{print $(NF-1)}' | tail -c 5)

user_name=$(ls  "/Users/" | grep -v '^[.*]' | grep -v 'Shared' | grep -v 'SupportAccount')

computer_name="${user_name}-${product_name}-${serial_no}"

/usr/sbin/scutil --set LocalHostName "${computer_name}"
/usr/sbin/scutil --set ComputerName "${computer_name}"
/usr/sbin/scutil --set HostName "${computer_name}"

jmahlman
Forum|alt.badge.img+17
  • Valued Contributor
  • February 23, 2018

I'm sure you can cobble something together between the script above and my script which renames a machine based on the username in the User and Location tab:

#!/bin/sh

if [ -e "/Library/Preferences/com.jamfsoftware.jamf.plist" ]; then
    jssURL=$(defaults read /Library/Preferences/com.jamfsoftware.jamf.plist jss_url)
else
    echo "No JSS server set. Exiting..."
    exit 1
fi

# Define API username and password information & JSS Group name from passed parameters
if [ ! -z "$4" ]; then
    apiUser="$4"
else
    echo "No value passed to $4 for api username. Exiting..."
    exit 1
fi

if [ ! -z "$5" ]; then
    apiPass="$5"
else
    echo "No value passed to $5 for api password. Exiting..."
    exit 1
fi

SERIAL=$(ioreg -c IOPlatformExpertDevice -d 2 | awk -F" '/IOPlatformSerialNumber/{print $(NF-1)}')

USERINFO=$(curl -k ${jssURL}JSSResource/computers/serialnumber/${SERIAL}/subset/location -H "Accept: application/xml" --user "${apiUser}:${apiPass}")
USERNAME=$(echo $USERINFO | /usr/bin/awk -F'<username>|</username>' '{print $2}' | tr [A-Z] [a-z])

DEVICEINFO=$(curl -k ${jssURL}JSSResource/computers/serialnumber/${SERIAL}/subset/hardware -H "Accept: application/xml" --user "${apiUser}:${apiPass}")
MODEL=$(echo $DEVICEINFO | /usr/bin/awk -F'<model_identifier>|</model_identifier>' '{print $2}')

if [ -z "$USERNAME" ]
then
    USERNAME="$SERIAL"
fi

if echo "$MODEL" | grep -q "MacBookAir"
then
    PREFIX="MBA"
elif echo "$MODEL" | grep -q "MacBookPro"
then
    PREFIX="MBP"
else
    echo "No model identifier found."
    PREFIX=""
fi

COMPUTERNAME="${USERNAME}-${PREFIX}"
COMPUTERNAME=`echo ${COMPUTERNAME:0:15}`
echo "Setting computer name to $COMPUTERNAME"
/usr/sbin/scutil --set ComputerName "$COMPUTERNAME"
/usr/sbin/scutil --set LocalHostName "$COMPUTERNAME"
/usr/sbin/scutil --set HostName "$COMPUTERNAME"
dscacheutil -flushcache

KyleEricson
Forum|alt.badge.img+17
  • Author
  • Valued Contributor
  • March 9, 2018

Say I want to exclude users admin account from the users how would I do that?
Example johdoe also may have an account called johdoeadmin. I don't want the mac to be named johdoejohdoeadmin-last4serial


KyleEricson
Forum|alt.badge.img+17
  • Author
  • Valued Contributor
  • March 12, 2018

I created this, but it doesn't seem to work when I create it into a policy. I ran it on 20 macs today and they all said that it completed but the name is still messed up. I even added the update inventory after the script is done.


Forum|alt.badge.img+7
  • Contributor
  • March 12, 2018

@kericson You may want to write to some logs to see what's going on. Also, try running it locally on one of them to see if it operates as expected.


easyedc
Forum|alt.badge.img+16
  • Esteemed Contributor
  • March 12, 2018

+1 for running it locally to see what it's doing, but when you do that add a

--verbose

to get more info on it. Something like

sudo sh --verbose /path/to/script.sh

easyedc
Forum|alt.badge.img+16
  • Esteemed Contributor
  • March 12, 2018

Also - post the script you tried.


KyleEricson
Forum|alt.badge.img+17
  • Author
  • Valued Contributor
  • March 14, 2018

Here is my script which runs and works when run locally.

#!/bin/sh



serial_no=$(ioreg -c IOPlatformExpertDevice -d 2 | awk -F" '/IOPlatformSerialNumber/{print $(NF-1)}' | tail -c 5)

user_name=$(ls  "/Users/" | grep -v '^[.*]' | grep -v '.admin' | grep -v 'Administrator' | grep -v 'administrator'| grep -v 'Guest' |  grep -v 'Shared')



computer_name="${user_name}-${serial_no}"

/usr/sbin/scutil --set LocalHostName "${computer_name}"
/usr/sbin/scutil --set ComputerName "${computer_name}"
/usr/sbin/scutil --set HostName "${computer_name}"

dscacheutil -flushcache