Script works properly when launched from terminal, but fails after being executed as a policy. Self Service tries to open, then crashes. App version is 10.35.0, the OS it gets executed from is 10.14.6, fully patched.
The same script fails on a manually upgraded Catalina as well. (Did it for testing purposes, just to see what happens; system is already running 10.15.7).
#!/bin/zsh
#
########################################
# Global Variables
########################################
loggedInUser=$( /bin/echo "show State:/Users/ConsoleUser" | /usr/sbin/scutil | /usr/bin/awk '/Name :/ && ! /loginwindow/ { print $3 }' ) # Get the current user - shell agnostic (sh, zsh, bash, dash)
myJamfHelper="/Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper" # Path to the jamfHelper binary - not part of PATH by default
########################################
########################################
# XXXXXXX XXXX OIT Notification Variables
########################################
notificationLogName="AG-Notification-Sample" # Name of notification - for log filename
windowTitleText="Generic Notification" # Notification window title
headingText="macOS Catalina Upgrade" # Bold heading text in notification body
iconPath="/Library/Application Support/AGCustom/Generic_Vertical_Wordmark.ico" # Icon shown in notification window
button1RightText="Upgrade OS" # Text on button1, the right button
button2LeftText="Postpone" # Text on button2, the left button
declare -i timeout=300 # Timeout in seconds - must be an integer and not a string
# Message text below appears in the notification body. Use literal line breaks as seen in this sample.
messageText="Your computer is currently running an older and unsupported version of macOS. Please start the upgrade process at your earliest convenience. To start it now please select Upgrade OS from the options below.
Click '${button2LeftText}' to view the installer in Self Service. Click '${button1RightText}' to initialize the upgrade now.
This notification will time out in ${timeout} seconds.
########################################
########################################
# Logging
########################################
# Logs are written to our logfile and output to console for capture by Jamf policy logging
logPath="/Library/Logs/AG"
logFile="${logPath}/${notificationLogName}.log"
if [ ! -d "${logPath}" ]; then
/bin/mkdir -p "$logPath"
fi
WriteLog()
{
logTxt=$1cata
/bin/echo "$(/bin/date +%Y-%m-%d_%H:%M:%S) : ${logTxt}" | /usr/bin/tee -a "$logFile"
}
########################################
########################################
# Main Script
########################################
# If there is no actively logged in user, exit.
# _mbsetupuser is the account in use during Setup Assistant.
if [ "$loggedInUser" = "" ] || [ "$loggedInUser" = "root" ] || [ "$loggedInUser" = "_mbsetupuser" ]; then
WriteLog "WARN: No logged in user. Stopping."
exit 0
fi
# Pull down our logo file if it does not exist
if [ ! -f "$iconPath" ]; then
/usr/local/bin/jamf policy -event agLogo
fi
WriteLog "INFO: Starting notification for user ($loggedInUser)."
# Call jamfHelper with our parameters
# 'utility' window types do not have window controls to close the window without interacting with our buttons
# 'button1' returns 0, 'button2' returns 2
# 'timeout' is in seconds and will run the defaultButton action when it is reached
# 'defaultButton' is the button returned on timeout, or if the user hits the return key
# 'cancelButton' is the button returned if the user hits the escape key
notificationResponse=$("$myJamfHelper" -windowType utility -timeout $timeout -button1 "$button1RightText" -button2 "$button2LeftText" -defaultButton 1 -cancelButton 2 -icon "$iconPath" -iconSize 200 -title "$windowTitleText" -heading "$headingText" -description "$messageText")
# Button actions start here
# Check jamfHelper's return value and act accordingly
# Button 2 returns 2, Button 1 returns 0
if [ "$notificationResponse" -eq 2 ]; then
sudo -u "$loggedInUser" /usr/bin/open "jamfselfservice://content?entity=policy&id=1609&action=view"
WriteLog "INFO: (${loggedInUser}) clicked (${button2LeftText})."
elif [ "$notificationResponse" -eq 0 ]; then
sudo -u "$loggedInUser" /usr/bin/sudo -u $loggedInUser /usr/bin/open /Applications/Install\\ macOS\\ Catalina.app
WriteLog "INFO: (${loggedInUser}) clicked (${button1RightText}) or notification timed out after ${timeout} seconds (see log timestamps)."
else
WriteLog "WARN: Notification ended with non-standard exit code $?"
exit 1
fi
WriteLog "INFO: Notification complete"
exit 0
########################################