Skip to content

Instantly share code, notes, and snippets.

@NiceRath
Created April 17, 2024 13:11
Show Gist options
  • Save NiceRath/1f6bcd0dd775c482620efed66d8f521e to your computer and use it in GitHub Desktop.
Save NiceRath/1f6bcd0dd775c482620efed66d8f521e to your computer and use it in GitHub Desktop.
MacOS Update Notification (Jamf)
#!/bin/bash
set -euo pipefail
# NOTES:
# as Apple is not able to provide any good option to force updates on managed clients - you might want to notify users to install them
# will be silent if no updates are available
# see also: https://ss64.com/osx/softwareupdate.html
LOG_FILE='/var/log/update_notify.log'
FILTER_NO_NEW='No new software available'
RANDOM_PROMPT=()
RANDOM_PROMPT[0]="please install them"
RANDOM_PROMPT[1]="just click on install"
RANDOM_PROMPT[2]="today is a beautiful day to install updates"
RANDOM_PROMPT_USE=$(( RANDOM % 3 ))
PROMPT_UPDATE="There are available system updates - ${RANDOM_PROMPT[RANDOM_PROMPT_USE]}!"
BUTTON_UPDATE_YES='Will do'
BUTTON_UPDATE_NO='Not now'
ICON_UPDATE='caution'
function log {
echo "$(date '+%Y/%m/%d %H:%M:%S') | $1" | tee -a "$LOG_FILE"
}
sudo touch "$LOG_FILE"
sudo chmod 664 "$LOG_FILE"
login_user=$(/bin/ls -la /dev/console | /usr/bin/cut -d ' ' -f 4)
log "INFO: Searching for updates"
# sudo launchctl kickstart -k system/com.apple.softwareupdated # stopped working: https://www.kevinmcox.com/2024/03/changes-to-launchctl-kickstart-in-macos-14-4/
available_updates=$(softwareupdate --list --os-only 2>&1)
if ! echo "$available_updates" | grep -q "$FILTER_NO_NEW"
then
log "INFO: Available system update: '$available_updates'"
response_update=$(/usr/bin/osascript -e "display dialog \"${PROMPT_UPDATE}\" buttons {\"$BUTTON_UPDATE_YES\", \"$BUTTON_UPDATE_NO\"} default button \"$BUTTON_UPDATE_YES\" with icon $ICON_UPDATE")
if echo "$response_update" | grep -q "returned:${BUTTON_UPDATE_YES}"
then
log "INFO: Opening software-update in system-preferences!"
su -l "$login_user" -c "open -b com.apple.systempreferences /System/Library/PreferencePanes/SoftwareUpdate.prefPane"
else
log "INFO: User skipped Update"
# testing
# open -b com.apple.systempreferences /System/Library/PreferencePanes/SoftwareUpdate.prefPane
fi
else
log "INFO: No system updates available!"
# testing
# response_update=$(/usr/bin/osascript -e "display dialog \"${PROMPT_UPDATE}\" buttons {\"$BUTTON_UPDATE_YES\", \"$BUTTON_UPDATE_NO\"} default button \"$BUTTON_UPDATE_YES\" with icon $ICON_UPDATE")
# su -l "$login_user" -c "open -b com.apple.systempreferences /System/Library/PreferencePanes/SoftwareUpdate.prefPane"
fi
log "INFO: FINISHED"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment