Renaming machine with contents of a text file

MarcosMunoz
New Contributor III

I am working on a project that requires us to change a machine's HostName to the serial number. Before that happens, I capture the current HostName and write it to a text file. After all the changes are complete, I need to go back and rename the HostName to what was captured in the text file.

As part of the changes, I need to have the users verify that their HostNames are following Corporate naming convention standards. I present them with an example and their currently HostName for comparison.

The intent is to have 2 buttons for the users. Button 1 verifies that the naming convention is being followed and no further action is needed by the user (the script continues onto other tasks). Button 2 allows the user to change the name by presenting them with a text box to enter their new HostName.

I am having an issue with getting the portion of the script to exit without any further interaction from the user when no action is needed. No matter which button is pressed, you are required to change the HostName.

Here is the portion of the script:

#!/bin/sh

hostname="$(awk '{print $0}' /tmp/hostname.txt)"

sudo scutil --set HostName $hostname


button=$(/Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper -windowType utility -title "Verify Machine name" -description "Please verify that your machine name matches the Corporate Naming Convention:

MACHINENAME 

Example MACHINE123 

Your Machine name is: $HostName 

If your machine name matches the example, click the OK button. Otherwise, click the RENAME button and change your machine name using the Corporate Naming Convention"  -button1 "RENAME" -defaultButton "2" -button2 "OK")

if [ "$button"=="1" ]; then

    (rv=$(osascript -e 'set T to text returned of (display dialog "Please verify that your machine name matches the Corporate Naming Convention:

MACHINENAME 

Example MACHINE123

Enter the computer name you wish to set" with title "Verify Machine Name" buttons {"Cancel", "OK"} default button "OK" default answer "")')
COMPUTERNAME=$rv


# Set Hostname using variable created above
scutil --set HostName $COMPUTERNAME
scutil --set LocalHostName $COMPUTERNAME
scutil --set ComputerName $COMPUTERNAME)


elif [ "$button"=="2" ]; then

    echo "Thank you for following the proper naming convention"

fi

I'm not quite certain what I need to modify to make this work properly. Any help would be greatly appreciated.

Thanks

Marcos

1 REPLY 1

MarcosMunoz
New Contributor III

Please disregard. I solved the issue.