I've been trying to adapt our computer naming script to wait for input from the user by cobbling together information fromhere and here and probably a few others, I can't seem to get it to work.
I just can't seem to figure out what I'm doing wrong, maybe there's an easier way? Can someone take a crack at it?
Original script that works (however if someone's not staring at the screen and they miss the prompts, then the names get messed up)
Also, if you can happen to fix the part at the top I commented out that looked at the machine type (laptop/desktop) and automatically puts in the correct type - that would be awesome :).
identifying data in script removed due to internal security requirements - but scrubbed version below retains the syntax and just made generic
#!/bin/bash
set -x
# jamf recon
loggedInUser=$(stat -f%Su /dev/console)
loggedInUID=$(id -u $loggedInUser)
if [[ "$loggedInUser" != "root" ]] && [[ "$loggedInUser" != "_mbsetup" ]]; then
## Create local script
cat << EOD > /private/tmp/computerrenamescript.sh
#!/bin/bash
TT=$(/usr/bin/osascript -e 'tell application "System Events" to set MacTYPE to text returned of (display dialog "Please input the machine type - MD = Mac Desktop or ML = Mac Laptop" default answer "" with icon 2)')
#ModelType=$(system_profiler SPHardwareDataType | grep 'Model Name:' | awk '{ print $3 }')
#echo "$ModelType"
#if [[ "$ModelType" == "MacBook" ]]; then
# TT=ML
#else
# TT=MD
#fi
BB=$(/usr/bin/osascript -e 'tell application "System Events" to set BRAND to text returned of (display dialog "Please input the brand - AA = AA, AB = AB, AC = AC," default answer "" with icon 2)')
LL=$(/usr/bin/osascript -e 'tell application "System Events" to set MacLoc to text returned of (display dialog "Please enter the location - BA = BA, BB = BB, BC = BC" default answer "" with icon 2)')
NN=$(/usr/bin/osascript -e 'tell application "System Events" to set DEPARTMENT to text returned of (display dialog "Please enter department - 01 = 01, 02 = 02, " default answer "" with icon 2)')
SN=$(system_profiler SPHardwareDataType | grep 'Serial Number' | awk '{print $4}' | tail -c -8) #this grabs the last 7 of the serial number
echo "${TT}${BB}${LL}${NN}${SN}" > /private/tmp/computerrenametext.txt
EOD
## Make script executable
/bin/chmod +x /private/tmp/computerrenamescript.sh
## Run the script as logged in user
/bin/launchctl asuser "$loggedInUID" sudo -iu "$loggedInUser" "/private/tmp/computerrenamescript.sh"
## Get the new name from the local file
newComputerName=$(cat /tmp/computerrenametext.txt)
if [ ! -z "$newComputerName" ]; then
echo "$newComputerName"
## Rename the computer to the new name
/usr/local/bin/jamf setComputerName -name "$newComputerName"
echo $newComputerName
scutil --set HostName $newComputerName
scutil --set LocalHostName $newComputerName
scutil --set ComputerName $newComputerName
dscacheutil -flushcache
jamf recon
## Remove local script
rm -f /private/tmp/computerrenamescript.sh
exit 0
else
echo "No name was found to rename to"
## Remove local script
rm -f /private/tmp/computerrenamescript.sh
exit 1
fi
else
echo "No-one logged in. Exiting"
exit 0
fi