I could use some feedback on my StartOSInstall script

Contributor III

We've started getting machines with the T2 chip, so our old reimaging approach isn't going to work anymore. I've written up a quick script that should make things easier for us, but I could use a second set of eyes to see if there's anything I forgot. It should be flexible enough to work with both 10.13 and 10.14, and it should be easy enough to update once 10.15 becomes a thing. We'll still need to delete the machine out of the JSS, but this at least handles most of the work.

# This script uses the pre-existing official OS installer located in /Applications folder
# This can be auto-deployed through VPP purchasing of the installer itself.
# $4 is the OS version.
# $5 is the name of the hard drive after imaging.

# Check OS version that is going to be installed
if [ "$4" == "10.13" ] ; then

    ApplicationPath="Install macOS High Sierra.app"
    ApplicationName="Install macOS High Sierra.app"

elif [ "$4" == "10.14" ] ; then

    ApplicationPath="Install macOS Mojave.app"
    ApplicationName="Install macOS Mojave.app"


    echo "You must specify an OS to install/upgrade.  Cancelling the task."
    exit 1


# The standard -d and -e tests would not work with .app files for unknown reasons.
# Roundabout way to test if the installer is present
Exists=`ls /Applications/ | grep -i "$ApplicationName"`

# If it is present, just state that.
if [ "$Exists" != "" ] ; then

    echo "$ApplicationName is present."

# If it is not, exit.

    echo "Error: "$ApplicationName" is not located in the Applications folder."
    echo "Aborting install."
    exit 2


# Check to see if the drive is APFS.
DiskType=`diskutil info / | awk -F': ' '/File System/{print $NF}' | xargs`

if [ $DiskType == "APFS" ] ; then

    echo "Drive is APFS."

    # If no value specified in $5 then it is "Macintosh HD"
    if [ "$5" == "" ] ; then 
        $5="Macintosh HD"

    echo "Naming the drive "$5"."

    # Add flags for wiping the drive to our eventual Install command
    InstallCommand="/Applications/$ApplicationPath/Contents/Resources/startosinstall --eraseinstall --newvolumename "$5" --agreetolicense --nointeraction &"


    # Drive has to be APFS.  If it isn't, cancelling the imaging attempt.
    echo "Error.  Drive isn't APFS."
    echo "You need to upgrade this machine before it can be reimaged this way."
    echo "Cancelling."
    exit 3


# Run the final command to either upgrade in place or reimage the machine.
echo "Reimaging the machine now."
eval $InstallCommand

exit 0