Posted on 02-24-2023 07:16 AM
My Script doesnt seem to be working any ideas. Can anyone see what is wrong.
#!/bin/sh
#get serial number
serial=$(system_profiler SPHardwareDataType | awk '/Serial/ {print $4}')
#Define Laptop
laptop=$LPT
computerName="$LPT-$serial"
# Set the ComputerName, HostName and LocalHostName
/usr/sbin/scutil --set ComputerName "$computerName"
/usr/sbin/scutil --set HostName "$computerName"
/usr/sbin/scutil --set LocalHostName "$computerName"
02-24-2023 07:38 AM - edited 02-24-2023 07:41 AM
Well, a couple of things here. First you are creating the variable laptop and giving it the value of the variable $LPT which has no value because it has not been declared anywhere in the script.
Second, you don't need to declare the value $laptop or $LPT to pass a string to another variable.
So if you want to name your computers LPT-<serial> you could use:
#!/bin/sh
#get serial number
serial=$(system_profiler SPHardwareDataType | awk '/Serial/ {print $4}')
#Define Laptop
laptop="LTP"
computerName="$laptop-$serial"
# Set the ComputerName, HostName and LocalHostName
/usr/sbin/scutil --set ComputerName "$computerName"
/usr/sbin/scutil --set HostName "$computerName"
/usr/sbin/scutil --set LocalHostName "$computerName"
or you could simply do:
#!/bin/sh
#get serial number
serial=$(system_profiler SPHardwareDataType | awk '/Serial/ {print $4}')
#Define Laptop
computerName="LPT-$serial"
# Set the ComputerName, HostName and LocalHostName
/usr/sbin/scutil --set ComputerName "$computerName"
/usr/sbin/scutil --set HostName "$computerName"
/usr/sbin/scutil --set LocalHostName "$computerName"
02-24-2023 09:03 AM - edited 02-24-2023 09:07 AM
If you are using the convention "[Prefix]-[SERIALNUMBER]" this can be done with a single line:
/usr/local/bin/jamf setComputerName -name "Mac-$(system_profiler SPHardwareDataType | awk '/Serial/ {print $4}')"
...or if you prefer to use the Wi-Fi MAC address:
/usr/local/bin/jamf setComputerName -name "Mac-$(ifconfig en0 | awk '/ether/{print $2}' | tr -d :’)"
This allows you to use the 'Files and Processes' payload rather than creating and maintaining a script and then adding the script to a policy.
Posted on 02-24-2023 09:20 AM
Using 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="LPT"
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 02-24-2023 11:26 AM
Might not be a bad idea to detect if it's a laptop instead of hard coding LPT into the script. Searching for the presence of a battery should do that. Or you could set LPT as a parameter and feed it to the script from the policy, this way should you need the script for both laptops and desktops, you could have two policies call the same script. As for detecting battery, this should do it (I suggest testing this!)
#!/bin/zsh
serial=$(system_profiler SPHardwareDataType | awk '/Serial/ {print $4}')
deviceType=$(ioreg -l -w0 | grep capacity)
if [ -z "$deviceType" ]; then
pre="DT"
else
pre="LPT"
fi
computerName="$pre-$serial"
echo $computerName
I believe using "book" from model ID these days isn't as reliable as it once was, could be wrong though.
Posted on 04-25-2023 01:22 PM
Another way to detect for Desktop vs laptop.
battery=$(pmset -g batt | grep -Eo "\d+%" | cut -d% -f1)
Posted on 02-24-2023 01:10 PM
You can change the prefix however you like
#!/bin/bash
serialNumber=$(system_profiler SPHardwareDataType | awk '/Serial/ {print $4}')
modelID=$(sysctl hw.model | awk '{print $NF}')
case "$modelID" in
iMac*)
modelPrefix="iMac" ;;
MacBook*)
modelPrefix="MBA" ;;
MacPro*)
modelPrefix="MBP" ;;
Macmini*)
modelPrefix="Mini" ;;
*)
modelPrefix="Mac" ;;
esac
name=$modelPrefix-$serialNumber
jamf setComputerName -name $name
Posted on 04-25-2023 12:25 PM
system_profiler is pretty slow, relatively speaking. Try replacing it with ioreg:
serialNumber=$(ioreg -c IOPlatformExpertDevice -d 2 | awk -F\" '/IOPlatformSerialNumber/{print $(NF-1)})