JAMFhelper Delay Options

Person
New Contributor III
selection=$(/Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper -windowType utility -description "Please restart now. -button2 "Restart" -showDelayOptions "0, 60, 180, 300" -button1 "Cancel" -cancelButton 1)

I have this simple script above but want to set the the delay options with the -showDelayOptions from jamfhelper. It says XX1 and XX2 for number for seconds for buttons 1 and 2 but I have tried inputting numbers like 002, 602,... and nothing happens. Same goes when selecting cancel button. Would that be 001 or "254 - Cancel button was select with delay option present"? What return value do I need to be looking for?

Delay for 60 seconds?

if [[ "$selection" == 602 ]]; then sudo shutdown -r now fi
7 REPLIES 7

mm2270
Legendary Contributor III

These options are a bit confusing to use because jamfHelper lumps both the value returned from the dropdown and the button clicked into one string.

So basically, using your script above, if I set it to 3 minutes and click Restart, I get:

1802

returned. Breaking that down, its "180" for the number of seconds, and "2" for the button clicked. Sadly it doesn't split these up, which would be nice. Instead, you need to massage these values to get something useful you can use in the script.

What you would need to do is take the returned value and strip off the last character initially to get a time chosen value.

timeChosen="${selection%?}"

Then get the button clicked to make sure they actually clicked the "Restart" button:

buttonClicked="${selection:$i-1}"

The one exception is when Start Now is chosen. In that case, only the button clicked is returned.

Consider this full script example on how you would then take the correct action:

#!/bin/bash

selection=$("/Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper" -windowType utility -description "Please restart now." -button2 "Restart"  -showDelayOptions "0, 60, 180, 300" -button1 "Cancel" -cancelButton 1)

buttonClicked="${selection:$i-1}"
timeChosen="${selection%?}"

## Convert seconds to minutes for restart command
timeMinutes=$((timeChosen/60))

## Echoes for troubleshooting purposes
echo "Button clicked was: $buttonClicked"
echo "Time chosen was: $timeChosen"
echo "Time in minutes: $timeMinutes"

if [[ "$buttonClicked" == "2" ]] && [[ ! -z "$timeChosen" ]]; then
    echo "Restart button was clicked. Initiating restart in $timeMinutes minutes"
    shutdown -r +{$timeMinutes}
elif [[ "$buttonClicked" == "2" ]] && [[ -z "$timeChosen" ]]; then
    echo "Restart button was clicked. Initiating immediate restart"
    shutdown -r now
elif [ "$buttonClicked" == "1" ]; then
    echo "Cancel button clicked. Exiting..."
    exit 0
fi

Give that a try and see if it does what you want.

Person
New Contributor III

Thank you! This is some of the information I was looking for and some. The delay options returns button 1 as 1 instead of zero which added to my confusion.

mm2270
Legendary Contributor III

Sure. Just an FYI, I'm just seeing an error in my script above. On the line that states:

shutdown -r +{$timeMinutes}

that should actually be:

shutdown -r +${timeMinutes}

The open curly bracket was in the wrong spot for the variable.

easyedc
Valued Contributor II

Throwing this out there as this is my project of the moment. @mm2270's script works amazingly well. However looking at this, I think it makes more sense to invoke the jamf for the reboot, so I can remind users to save their work.

jamf reboot -minutes ${timeMinutes} -message "Please save your work" -background

Food for thought for the next guy/gal to stumble across this thread. If I'm wrong, please let me know.

easyedc
Valued Contributor II

Throwing this out there as this is my project of the moment. @mm2270's script works amazingly well. However looking at this, I think it makes more sense to invoke the jamf for the reboot, so I can remind users to save their work.

jamf reboot -minutes ${timeMinutes} -message "Please save your work" -background

Food for thought for the next guy/gal to stumble across this thread. If I'm wrong, please let me know.

brock_walters
Contributor

Hi guys -

Here is a script I created to play around with the output of the jamfhelper delay options:

#!/bin/bash

#######################################################################################
#
# Copyright (c) 2016, JAMF Software, LLC.  All rights reserved.
#
#       Redistribution and use in source and binary forms, with or without
#       modification, are permitted provided that the following conditions are met:
#               * Redistributions of source code must retain the above copyright
#                 notice, this list of conditions and the following disclaimer.
#               * Redistributions in binary form must reproduce the above copyright
#                 notice, this list of conditions and the following disclaimer in the
#                 documentation and/or other materials provided with the distribution.
#               * Neither the name of the JAMF Software, LLC nor the
#                 names of its contributors may be used to endorse or promote products
#                 derived from this software without specific prior written permission.
#
#       THIS SOFTWARE IS PROVIDED BY JAMF SOFTWARE, LLC "AS IS" AND ANY
#       EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
#       WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
#       DISCLAIMED. IN NO EVENT SHALL JAMF SOFTWARE, LLC BE LIABLE FOR ANY
#       DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
#       (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
#       LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
#       ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
#       (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
#       SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
#######################################################################################
#
# jss.jhp.delay.sh
# ©2016 brock walters jamf
#
# this script is generic. it creates a blank file on the user's Desktop.
#
# the information in the jamfhelper pop-up window can be modified by changing the following below:
#
#   -title
#   -heading
#   -description
#   -icon (eg, a .b64 encoded .png or .icns file in the script or a reference to a graphics file)
#   -button1 (limited characters in field)
#   -button2 (limited characters in field)
#   -showDelayOptions (in seconds)
#
#   for other jamfHelper options see:
#   
#      /Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper -help
#
# the case statement takes input from the jamfHelper button exit code variable "$result"
#
# the "$cmdstr" & "$pthstr" variables populate the Launch Daemon "ProgramArguments" array. 
# each string in the array would be separated by spaces on the command line, eg.
#
# $ /usr/bin/touch /Users/username/Desktop/foo.bar
#
# would translate to:
#
#   <key>ProgramArguments</key>
#   <array>
#       <string>/usr/bin/touch</string>
#       <string>/Users/username/Desktop/foo.bar</string>
#   </array>
#
# some command or a series of commands separated by pipes or newlines or other functions
# containing lists of commands could be added to the script & called by replacing "$cmdstr"
# & "$pthstr" with the function names.

jamfhelper()
{
/Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper 
-windowType hud 
-title "The Matrix" 
-heading "It's up to you." 
-description "The Red pill or the Blue..." 
-icon /System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/public.generic-pc.icns 
-button1 "Blue" 
-button2 "Red" 
-showDelayOptions "70, 3600, 86400, 604800" # 70 seconds, 1 hour, 1 day, 1 week
}

# variables

result=$(jamfhelper)
delayint=$(echo "$result" | /usr/bin/sed 's/.$//')
defercal=$(($(/bin/date +%s) + delayint))
month=$(/bin/date -j -f "%s" "$defercal" "+%m")
day=$(/bin/date -j -f "%s" "$defercal" "+%e")
hour=$(/bin/date -j -f "%s" "$defercal" "+%H")
minute=$(/bin/date -j -f "%s" "$defercal" "+%M")

# write launch daemon populated with variables from jamfHelper output

delay()
{
/bin/cat <<EOF > /Library/LaunchDaemons/com.jamfhelper.delay.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>com.jamfhelper.delay</string>
    <key>LaunchOnlyOnce</key>
    <true/>
    <key>ProgramArguments</key>
    <array>
        <string>$cmdstr</string>
        <string>$pthstr</string>
    </array>
    <key>StartCalendarInterval</key>
    <array>
        <dict>
            <key>Month</key>
            <integer>$month</integer>
            <key>Day</key>
            <integer>$day</integer>
            <key>Hour</key>
            <integer>$hour</integer>
            <key>Minute</key>
            <integer>$minute</integer>
        </dict>
    </array>
</dict>
</plist>
EOF
}

# select action based on user input

case "$result" in
    *1 )    cmdstr="/usr/bin/touch"
            pthstr=$(echo ~/Desktop/Blue)
            delay
            ;;
    *2 )    cmdstr="/usr/bin/touch"
            pthstr=$(echo ~/Desktop/Red)
            delay
            ;;
esac

# set ownership on launch daemon & load

/bin/chmod 644 /Library/LaunchDaemons/com.jamfhelper.delay.plist
/usr/sbin/chown root:wheel /Library/LaunchDaemons/com.jamfhelper.delay.plist
/bin/launchctl load /Library/LaunchDaemons/com.jamfhelper.delay.plist

What's kind of fun about this is the way it parses the output in seconds into variables that are used to create the StartCalendarInterval Launch Daemon. This is a POC (since in this form all it does is create a blank file on the desktop) but could actually be turned (by one of you) into something useful!

kadams
Contributor

Does anyone have an edited script like this for rebooting? I need to reboot after running policies from Jamf. The first option could be restart now. The second option could be a delay.