4 weeks ago
Hi,
i am trying to script the final step in our onboarding process, where the user is presented with a list of Locations to chose, and once selected, the script will pull the serial number and rename the machine the location-serialnumber
I'm 90% sure I have it right but i am getting echo errors when testing in CodeRunner... and i'd appreciate some extra eyes / advice....
#!/bin/bash
#/usr/local/bin/dialog
# Pull device serial number for lookup
serialNumber=$(ioreg -c IOPlatformExpertDevice -d 2 | awk -F\" '/IOPlatformSerialNumber/{print $(NF-1)}')
nserialNumber=${serialNumber}
#Specify variables for swiftDialog
dialogInstalled="/usr/local/bin/dialog"
downloadDialog="https://github.com/swiftDialog/swiftDialog/releases/download/v2.5.1/dialog-2.5.1-4775.pkg"
pathToDownload="/usr/local/dialog"
pathToPackage="/usr/local/dialog/dialog-2.5.1-4775.pkg"
#Check if swiftDialog is installed
if [ -e "$dialogInstalled" ];then
echo "Dialog Is installed"
else
echo "Dialog is not installed"
echo "Creating download directory at $pathToDownload"
mkdir $pathToDownload
echo "Downloading swiftDialog from $downloadDialog"
curl -L $downloadDialog -o $pathToPackage
echo "Installing swift Dialog"
installer -pkg $pathToPackage -target /
wait
fi
location=$(/usr/local/bin/dialog -s --icon /usr/local/images/duck.icns --blurscreen --title "Welcome to swiftDialog" --message "Hi There {username}. <br>Please choose your current location" --selecttitle "Select Location",required --selectvalues "Barcelona, Camden, Cape Town, Durban, Florida, Gold Coast, Gothenburg, IOM, Ipswich, Kyiv, Malaga, Pretoria, Richmond, Stockholm, Sydney" | grep "SelectedOption" | awk -F " : " '{print $NF}' | tr -d '"')
#selection
#
case ${location} in
Barcelona)
invCode='SBA';;
Camden)
invCode='UCA';;
'Cape Town')
invCode='ZCT';;
Durban)
invCode='ZFP';;
Florida)
invCode='ORL';;
'Gold Coast')
invCode='ADI';;
Gothenburg)
invCode='SGB';;
IOM)
invCode='DIL';;
Ipswich)
invCode='UIP';;
Kyiv)
invCode='UKK';;
Malaga)
invCode='SMA';;
Pretoria)
invCode='ZPT';;
Richmond)
invCode='MHL';;
Stockholm)
invCode='SST';;
Sydney)
invCode='ADS';;
esac
echo "Selection was $location setting prefix to $invCode"
computerName="$invCode-$nserialNumber"
echo "Computer Name: $computerName"
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
/usr/local/bin/jamf recon
exit 0
Solved! Go to Solution.
4 weeks ago
I use osascript for testing. Change it back to SwiftDialog
#!/bin/bash
loggedInUser=$(stat -f%Su /dev/console)
loggedInUID=$(id -u $loggedInUser)
serialNumber=$(ioreg -c IOPlatformExpertDevice -d 2 | awk -F\" '/IOPlatformSerialNumber/{print $(NF-1)}')
OSASCRIPT="/usr/bin/osascript"
location=$( /bin/launchctl asuser $loggedInUID sudo -iu $loggedInUser << EOF
/usr/bin/osascript -e 'tell application "System Events"
activate
choose from list {"Barcelona", "Camden", "Cape Town", "Durban", "Florida", "Gold Coast", "Gothenburg", "IOM", "Ipswich", "Kyiv", "Malaga", "Pretoria", "Richmond", "Stockholm", "Sydney"} with prompt "Hi There, ${loggedInUser}" default items {"Barcelona"} with title "Please choose your current location"
end tell' 2>/dev/null
EOF
)
#selection
#
case ${location} in
Barcelona)
invCode='SBA' ;;
Camden)
invCode='UCA' ;;
'Cape Town')
invCode='ZCT' ;;
Durban)
invCode='ZFP' ;;
Florida)
invCode='ORL' ;;
'Gold Coast')
invCode='ADI' ;;
Gothenburg)
invCode='SGB' ;;
IOM)
invCode='DIL' ;;
Ipswich)
invCode='UIP' ;;
Kyiv)
invCode='UKK' ;;
Malaga)
invCode='SMA' ;;
Pretoria)
invCode='ZPT' ;;
Richmond)
invCode='MHL' ;;
Stockholm)
invCode='SST' ;;
Sydney)
invCode='ADS' ;;
esac
echo "Selection was $location setting prefix to $invCode"
echo "Computer Name: "$invCode"-"$serialNumber""
exit 0
4 weeks ago
Rather than asking the user, have you considered using something like network segments to determine where the device is and give it a name based on that? This would not work for remote workforces or locations that mask their IP addresses.
4 weeks ago
I use osascript for testing. Change it back to SwiftDialog
#!/bin/bash
loggedInUser=$(stat -f%Su /dev/console)
loggedInUID=$(id -u $loggedInUser)
serialNumber=$(ioreg -c IOPlatformExpertDevice -d 2 | awk -F\" '/IOPlatformSerialNumber/{print $(NF-1)}')
OSASCRIPT="/usr/bin/osascript"
location=$( /bin/launchctl asuser $loggedInUID sudo -iu $loggedInUser << EOF
/usr/bin/osascript -e 'tell application "System Events"
activate
choose from list {"Barcelona", "Camden", "Cape Town", "Durban", "Florida", "Gold Coast", "Gothenburg", "IOM", "Ipswich", "Kyiv", "Malaga", "Pretoria", "Richmond", "Stockholm", "Sydney"} with prompt "Hi There, ${loggedInUser}" default items {"Barcelona"} with title "Please choose your current location"
end tell' 2>/dev/null
EOF
)
#selection
#
case ${location} in
Barcelona)
invCode='SBA' ;;
Camden)
invCode='UCA' ;;
'Cape Town')
invCode='ZCT' ;;
Durban)
invCode='ZFP' ;;
Florida)
invCode='ORL' ;;
'Gold Coast')
invCode='ADI' ;;
Gothenburg)
invCode='SGB' ;;
IOM)
invCode='DIL' ;;
Ipswich)
invCode='UIP' ;;
Kyiv)
invCode='UKK' ;;
Malaga)
invCode='SMA' ;;
Pretoria)
invCode='ZPT' ;;
Richmond)
invCode='MHL' ;;
Stockholm)
invCode='SST' ;;
Sydney)
invCode='ADS' ;;
esac
echo "Selection was $location setting prefix to $invCode"
echo "Computer Name: "$invCode"-"$serialNumber""
exit 0
4 weeks ago
Thanks a lot, this really helped