Self Service Script, Exit If File Doesn't Exist, Otherwise Continue

derek_ritchison
Contributor

Hey all- I have a simple script that allows an end user to update to 10.13.6 (or, soon, 10.14) via Self Service with the click of a button. (I have a separate policy that caches the installer in the user's /Applications folder based on criteria in a Smart Group.) One thing I want my installer script to check for is whether or not that installer file has been cached or not, and if not the script should prompt the user with a dialogue box and exit. Here is what I have been trying, with no success.

#!/bin/sh

# First we gotta make sure the installer has already been cached per the user's Smart Group

if [ ! -f "/Applications/Install macOS High Sierra.app" ]; then

dialog="The High Sierra installer is not yet on your computer. Please reach out to IT."
description=`echo "$dialog"`
button1="OK"
jamfHelper="/Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper"
icon="/path/to/icon"
"$jamfHelper" -windowType utility -description "$description" -button1 "$button1" -icon "$icon"
exit 0
fi

# This is the heading to be used for jamfHelper

heading="Please wait as we prepare your computer for macOS High Sierra. Exciting!"

...and the installation continues from here.

What happens is I get that "error" dialogue even when the installer is in the /Applications folder. And when I remove the above portion from my script everything works as intended.

Ideas?

14 REPLIES 14

tuinte
Contributor III

.app files are seen as directories. Swap your -f for a -d then buy me a beer!

M

derek_ritchison
Contributor

Man! I was so excited. I do want to buy you a beer but the icon in Self Service still just spins endlessly. Here is my updated code:

#!/bin/sh

# First we gotta make sure the installer has already been cached per the user's Smart Group

dialog="The High Sierra installer is not yet on your computer. Please reach out to IT."
description=`echo "$dialog"`
button1="OK"
jamfHelper="/Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper"
icon="/path/to/icon"

if [[ ! -d "/Applications/Install macOS High Sierra.app" ]]; then
   "$jamfHelper" -windowType utility -description "$description" -button1 "$button1" -icon "$icon"
   exit 0
fi

# This is the heading to be used for jamfHelper

heading="Please wait as we prepare your computer for macOS Mojave. Exciting!"

...and it continues from here.

To test the if/then I simply temporarily changed the name of the "Install macOS High Sierra" installer to "Install macOS".

tuinte
Contributor III

Your icon path is clearly bust! Make that the path to an actual image file and give that a shot.

derek_ritchison
Contributor

Unfortunately that didn't change anything either. The process still just hangs with the if/then statement in place. Installs without issue when I comment it out.

ryan_ball
Valued Contributor

In POSIX [[ ]] is undefined. Switch that to bash first off.

Also setting the description twice is unnecessary you should be able to just set the description with the single variable, try this:

#!/bin/bash

# First we gotta make sure the installer has already been cached per the user's Smart Group

description="The High Sierra installer is not yet on your computer. Please reach out to IT."
button1="OK"
jamfHelper="/Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper"
icon="/path/to/icon"

if [[ ! -d "/Applications/Install macOS High Sierra.app" ]]; then
    "$jamfHelper" -windowType utility -description "$description" -button1 "$button1" -icon "$icon"
    exit 0
fi

bmcintire2
New Contributor II

I have made a couple I have been playing with. I have messed around with if it exists with -d, but was worried about the integrity of the installer. Made one that checks the sha1 hash and continues as needed. Obviously the checksum will change depending on version and all that - but its a nice way to at least make sure it is intact. Here is the meat of it. Of course, YMMV.

#!/bin/bash

hash=$(shasum -a 1 /Applications/Install macOS High Sierra.app/Contents/SharedSupport/InstallESD.dmg | awk '{print $1}')
    if [ $hash == 69159caf25666ea1c5d466e158e075d947f6a9ee ] ;then
    echo "Installer verified."
    else "Installer not verified."
fi

derek_ritchison
Contributor

Ok. The hash is probably a better way of checking once I get it solved, but it still just hangs when I run the Jamf script. The thing is, the below script works perfectly when I run it in Terminal:

#!/bin/sh

description="The Mojave installer is not yet on your computer. Please reach out to IT."
button1="OK"
jamfHelper="/Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper"
icon="/path/to/icon"

if [[ ! -d "/Applications/Install macOS Mojave.app" ]]; then
   "$jamfHelper" -windowType utility -description "$description" -button1 "$button1"
   exit 0
else
   echo "Beginning upgrade process..."
fi

If I temporarily change the name of the installer in /Applications I get the error. If I change it back I get the "Beginning upgrade process..." echo. I did have to run the script as sudo though, could that be the issue?

ryan_ball
Valued Contributor

@derek.ritchison Like I said, in POSIX shell (#!/bin/sh) using [[ ]] in if statements might not be recognized. Either change to [ ] or specify bash with #!/bin/bash.

That might not be your issue at all but you should use correct syntax.

derek_ritchison
Contributor

Ah. Got it. Missed that earlier. I fixed that. Didn't change anything, but it's at least proper syntax now.

derek_ritchison
Contributor

9e091c3e70bc43338f45a82c6341fd9a

When I run the script locally in Terminal it works as I'd expect. I do have to run it as sudo and enter a password though. Could that be what is causing Self Service to hang? If so, is there a way around having to enter a password?

derek_ritchison
Contributor

In case anyone is curious I finally got the script to work. Yesterday I noticed that it actually was working fine on other test machines, so I thought that maybe it was failing on my own because I actually did have the installer in my /Applications folder, but I just re-named it. After deleting the app entirely it worked. Odd, but we're in business. Now I only have to fine tune my Smart Group so that the installer doesn't cache if the user happens to be using, say, a hot spot or is on an otherwise undesirable WiFi network --- coffee shop, airport, et cetera. Any suggestions there would be great.

ryan_ball
Valued Contributor

If your ip addresses are always 10.X.X.X, then your smart group would be something like IP is not like "10.".

Phantom5
Contributor II

Why would you put a button on self service for something that is not yet there? Why don't you scope the policy to a smart group that checks when the installer is cached? That would you would spare the user of clicking on a button that's going to tell them they can do it yet.

derek_ritchison
Contributor

Holy crap. That... is a good idea. Beers for everyone!!!