Script to change computer name

tomasitow77
New Contributor

Hi all,

I just migrated from JamfNow to JamfPro and Im very new here. 

Im addressing computer's name by a Smart Group with a policy to execute this Script, which is not working for me.

Basically I want to use the prefix MBP or MBA, depending on the model, following the SN of the laptop. 

Any advice or help will be highly value. 

 

#!/bin/sh

# Get laptop model
model=$(system_profiler SPHardwareDataType | awk '/Model Identifier/ {print $3}')

# Get Serial Number
serial=$(system_profiler SPHardwareDataType | grep Serial | awk '{ print $4 }')

# Assign a prefix
if echo "$model" | grep -q "MacBookPro"; then
    prefix="MBP"
elif echo "$model" | grep -q "MacBookAir"; then
    prefix="MBA"
else
    prefix="APPLE"
fi

 

# Generate computername
computerName="$prefix-$serial"
echo "$computerName"

 

# Rename computer
scutil --set ComputerName "$computerName"
sleep 5
scutil --set HostName "$computerName"
sleep 5
scutil --set LocalHostName "$computerName"
sleep 5

 

echo "Computer renamed to $computerName"

 

# Clean caché and push an inventory sync
dscacheutil -flushcache
/usr/local/bin/jamf recon

 

 

Thanks a lot,

 

1 ACCEPTED SOLUTION

tomasitow77
New Contributor

Thanks everybody, 

Here is the final result, if you want to use it anytime. 

#!/bin/sh

# Get laptop model
model=$(system_profiler SPHardwareDataType | grep Model | awk '{print $4}')

# Get SN
serial=$(system_profiler SPHardwareDataType | grep Serial | awk '{ print $4 }')

# Assign a prefix
if echo "$model" | grep -q "Pro"; then
prefix="MBP"
elif echo "$model" | grep -q "Air"; then
prefix="MBA"
else
prefix="Apple"
fi

# Generate computername
computerName="$prefix-$serial"
echo "$computerName"

# Rename computer
scutil --set ComputerName "$computerName"
sleep 5
scutil --set HostName "$computerName"
sleep 5
scutil --set LocalHostName "$computerName"
sleep 5

echo "Equipo renombrado a $computerName"

# Clean caché and push an inventory sync
dscacheutil -flushcache
/usr/local/bin/jamf recon

View solution in original post

5 REPLIES 5

AJPinto
Honored Contributor III

We just match the hostname of the device to the Serial Number, and below is the script I use.

 

It should not be difficult to add a function and if statements to add a prefix to the new hostname based on the device model from system profiler, something like the command below. Once you have MacBook Pro or MacBook Air, write an if to run the correct commands to put MBA or MBP in front of the hostname depending on the result of the command.

/usr/sbin/system_profiler SPHardwareDataType | grep "Model Name"

 

 

 

#!/usr/bin/bash
#*=============================================================================
#* Script Name: Alias_2208
#* Created: [08/12/22]
#* Author:
#*=============================================================================
#* Purpose: Changes Mac hostname to match SN if it does not already match
#*=============================================================================
 

#*=============================================================================
#* GLOBAL 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 '"')
 
#*=============================================================================
#* FUNCTIONS
#*=============================================================================
 

#*=============================================================================
#* 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 "Results:"; $DIV3
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 standars"
    echo "More troubleshooting will be necessary."
    $DIV1; exit 1
fi
 
#*=============================================================================
#* END OF SCRIPT
#*=============================================================================

 

 

 

obi-k
Valued Contributor III

Something like this?

#!/bin/bash

OLDCOMPNAME=`/usr/sbin/scutil --get ComputerName`
sudo /System/Library/CoreServices/RemoteManagement/ARDAgent.app/Contents/Resources/kickstart -configure -computerinfo -set1 -1 "${OLDCOMPNAME}"
PREFIX="MBP"
SERIAL=`system_profiler SPHardwareDataType | grep "r (system)" | awk '{ print $4}'`
HOSTNAME="${PREFIX}${SERIAL}"
sudo /usr/sbin/scutil --set ComputerName "${HOSTNAME}"
sudo /usr/sbin/scutil --set LocalHostName "${HOSTNAME}"

exit 0

mrrobertbuss
Contributor

Somebody from JAMF Nation provided this script. It changes the name of the Mac to the Serial Number.

!/bin/bash

serialNumber=$(system_profiler SPHardwareDataType | awk '/Serial/ {print $4}')

echo $serialNumber

sudo jamf setComputerName -name $serialNumber

sudo jamf recon

pete_c
Contributor III

Faster way to get the Mac’s serial:

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

tomasitow77
New Contributor

Thanks everybody, 

Here is the final result, if you want to use it anytime. 

#!/bin/sh

# Get laptop model
model=$(system_profiler SPHardwareDataType | grep Model | awk '{print $4}')

# Get SN
serial=$(system_profiler SPHardwareDataType | grep Serial | awk '{ print $4 }')

# Assign a prefix
if echo "$model" | grep -q "Pro"; then
prefix="MBP"
elif echo "$model" | grep -q "Air"; then
prefix="MBA"
else
prefix="Apple"
fi

# Generate computername
computerName="$prefix-$serial"
echo "$computerName"

# Rename computer
scutil --set ComputerName "$computerName"
sleep 5
scutil --set HostName "$computerName"
sleep 5
scutil --set LocalHostName "$computerName"
sleep 5

echo "Equipo renombrado a $computerName"

# Clean caché and push an inventory sync
dscacheutil -flushcache
/usr/local/bin/jamf recon