Skip to main content
Solved

Setting a Location and matching naming convention

  • September 12, 2024
  • 3 replies
  • 1 view

Forum|alt.badge.img+5

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

 

 

Best answer by YanW

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

View original
Did this topic help you find an answer to your question?

3 replies

AJPinto
Forum|alt.badge.img+26
  • Legendary Contributor
  • 2721 replies
  • September 12, 2024

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.


YanW
Forum|alt.badge.img+11
  • Contributor
  • 180 replies
  • Answer
  • September 12, 2024

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


Forum|alt.badge.img+5
  • Author
  • Contributor
  • 33 replies
  • September 13, 2024

Thanks a lot, this really helped


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings