Posted on 04-30-2024 07:39 AM
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,
Solved! Go to Solution.
Posted on 05-02-2024 05:19 AM
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
04-30-2024 07:45 AM - edited 04-30-2024 07:48 AM
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
#*=============================================================================
Posted on 04-30-2024 08:17 AM
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
Posted on 04-30-2024 11:53 AM
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
Posted on 04-30-2024 12:20 PM
Faster way to get the Mac’s serial:
serial=$(ioreg -c IOPlatformExpertDevice -d 2 | awk -F\" '/IOPlatformSerialNumber/{print $(NF-1)}')
Posted on 05-02-2024 05:19 AM
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