#!/bin/bash ########################################################################## # Shellscript : macOS Update with deferral # Autor : Andreas Vogel, NEXT Enterprise gmbh, 2021 ########################################################################## # set -x setDeferral (){ BundleID="${1}" DeferralType="${2}" DeferralValue="${3}" DeferralPlist="${4}" if [[ "$DeferralType" == "date" ]]; then DeferralDate="$(/usr/libexec/PlistBuddy -c "print :$BundleID:date" "$DeferralPlist" 2>/dev/null)" # Set deferral date if [[ -n "$DeferralDate" ]] && [[ ! "$DeferralDate" =~ "File Doesn't Exist" ]]; then # /usr/libexec/PlistBuddy -c "set :$BundleID:date '07/04/2019 11:21:51 +0000'" "$DeferralPlist" /usr/libexec/PlistBuddy -c "set :$BundleID:date $DeferralValue" "$DeferralPlist" 2>/dev/null else # /usr/libexec/PlistBuddy -c "add :$BundleID:date date '07/04/2019 11:21:51 +0000'" "$DeferralPlist" /usr/libexec/PlistBuddy -c "add :$BundleID:date date $DeferralValue" "$DeferralPlist" 2>/dev/null fi elif [[ "$DeferralType" == "count" ]]; then DeferralCount="$(/usr/libexec/PlistBuddy -c "print :$BundleID:count" "$DeferralPlist" 2>/dev/null)" # Set deferral count if [[ -n "$DeferralCount" ]] && [[ ! "$DeferralCount" =~ "File Doesn't Exist" ]]; then /usr/libexec/PlistBuddy -c "set :$BundleID:count $DeferralValue" "$DeferralPlist" 2>/dev/null else /usr/libexec/PlistBuddy -c "add :$BundleID:count integer $DeferralValue" "$DeferralPlist" 2>/dev/null fi else echo "Incorrect deferral type used." exit 14 fi } OSMajorVersion="$(/usr/bin/sw_vers -productVersion | /usr/bin/cut -d '.' -f 1)" DeferralPlist="/Library/Application Support/JAMF/com.custom.deferrals.plist" BundleID="com.apple.SoftwareUpdate" DeferralType="count" DeferralValue="${4}" if [[ -z "$DeferralValue" ]]; then DeferralValue=3 fi TimeOutinSec="${5}" if [[ -z "$TimeOutinSec" ]]; then TimeOutinSec="900" fi ITContact="${6}" if [[ -z "$ITContact" ]]; then ITContact="IT" fi CurrentDeferralValue="$(/usr/libexec/PlistBuddy -c "print :$BundleID:count" "$DeferralPlist" 2>/dev/null)" # Set up the deferral value if it does not exist already if [[ -z "$CurrentDeferralValue" ]] || [[ "$CurrentDeferralValue" =~ "File Doesn't Exist" ]]; then setDeferral "$BundleID" "$DeferralType" "$DeferralValue" "$DeferralPlist" CurrentDeferralValue="$(/usr/libexec/PlistBuddy -c "print :$BundleID:count" "$DeferralPlist" 2>/dev/null)" fi jamfHelper="/Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper" jamf="/usr/local/bin/jamf" AppleSUIcon="/System/Library/CoreServices/Install Command Line Developer Tools.app/Contents/Resources/SoftwareUpdate.icns" CURRENT_USER=$(/usr/bin/python -c 'from SystemConfiguration import SCDynamicStoreCopyConsoleUser; import sys; username = (SCDynamicStoreCopyConsoleUser(None, None, None) or [None])[0]; username = [username,""][username in [u"loginwindow", None, u""]]; sys.stdout.write(username + "\n");') realname=$(dscl . read /Users/$CURRENT_USER RealName | tail -n1) ListOfSoftwareUpdates="/tmp/ListOfSoftwareUpdates" ######################### Messages ################################################# ######################### find Language ############################################ Language=$(/usr/libexec/PlistBuddy -c 'print AppleLanguages:0' "/Users/${CURRENT_USER}/Library/Preferences/.GlobalPreferences.plist") if [[ $Language = de* ]]; then UserLanguage="de" else UserLanguage="en" fi ######################### Message Box DE ########################################### no_ac_power_de="Der Computer läuft derzeit im Akkubetrieb und ist nicht an eine Stromquelle angeschlossen. Schließen Sie den Computer an eine Stromquelle an und versuchen Sie es erneut." StandardUpdatePrompt_de="Hallo, $realname für deinen Mac ist ein Betriebssystem-Update verfügbar. Klicken auf Fortfahren, um mit der Softwareaktualisierung fortzufahren und die Aktualisierung auszuführen. Wenn du den Vorgang zu diesem Zeitpunkt nicht starten kannst, kannst du den Vorgang um einen Tag verschieben. Wenn die Anzahl der Aufschübe aufgebraucht ist, wird die Aktualisierung erzwungen. Die Aktualisierung dauert etwa 30 Minuten. Du kannst das macOS Software-Update jederzeit installieren gehe hierzu  > System Preferences > Software Update Verbleibende Anzahl der Verschiebungen: $CurrentDeferralValue" ForcedUpdatePrompt_de="Hallo, $realname für deinen Mac sind Betriebssystem-Updates verfügbar, die einen Neustart erfordern. Du hast die Aktualisierungen bereits so oft wie möglich verschoben. Bitte speichern deine Arbeit und klicken Sie auf Aktualisieren. Andernfalls wird diese Meldung ausgeblendet und der Computer wird automatisch neu gestartet." PasswordInvalid_de="Passwort ungültig" PasswordInvalidDescription_de="Du hast dreimal versucht, ein falsches Passwort einzugeben" button1_de="Fortfahren" button2_de="verschieben" ######################### Message Box EN ########################################### no_ac_power_en="The computer is currently running on battery power and is not connected to a power source. Connect the computer to a power source and try again." StandardUpdatePrompt_en="Hello, $realname for your Mac is an operating system update available. Click Continue to proceed with the software update and run the update. If you are unable to start the process at this time, you can postpone the process for one day. When the number of postponements is used up, the update will be forced. The update takes about 30 minutes to complete. You can install the macOS software update at any time by navigate  > System Preferences > Software Update Remaining number of shifts: $CurrentDeferralValue" ForcedUpdatePrompt_en="Hello, $realname for your Mac operating system updates are available that require a reboot. You have already moved the updates as many times as possible. Please save your work and click Update. Otherwise, this message will disappear and the computer will restart automatically." PasswordInvalid_en="Password Invalid" PasswordInvalidDescription_en="You made three incorrect password attempts" button1_en="Continue" button2_en="delay" no_ac_power=no_ac_power_${UserLanguage} StandardUpdatePrompt=StandardUpdatePrompt_${UserLanguage} ForcedUpdatePrompt=ForcedUpdatePrompt_${UserLanguage} PasswordInvalid=PasswordInvalid_${UserLanguage} PasswordInvalidDescription=PasswordInvalidDescription_${UserLanguage} button1=button1_${UserLanguage} button2=button2_${UserLanguage} ## Functions ## powerCheck (){ # This is meant to be used when doing CLI update installs. # Updates through the GUI can already determine its own requirements to proceed with # the update process. # Let's wait 5 minutes to see if computer gets plugged into power. for (( i = 1; i <= 5; ++i )); do if [[ "$(/usr/bin/pmset -g ps | /usr/bin/grep "Battery Power")" = "Now drawing from 'Battery Power'" ]]; then echo "${!no_ac_power}" /bin/sleep 60 else return 0 fi done exit 11 } runsoftwareupdate () { expect <