Advertisement
taboc741

AppleSoftwareUpdatev2

Jul 13th, 2018
374
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.39 KB | None | 0 0
  1. #!/bin/sh
  2. ##  AppleSoftwareUpdate2.sh
  3. ##  Created by Chris on 5/31/18. Last edited by Chris 7/13/2018
  4. ##### Special thanks to mm2270, cstout, nvandam from JamfNation for helping me with some testing and code suggestions to problems I couldn't solve on my own.#####
  5.  
  6. ######### Create settings for logging and create log file #########
  7. ## Path to Log file
  8. LogPath=/tmp/log
  9. if [ ! -d "$LogPath" ];then
  10.     mkdir /tmp/log
  11. fi
  12. ## Set log file and console to recieve output of commands
  13. LOG_FILE=/tmp/log/SoftwareUpdateScript.log
  14. #Below lines for redirecting terminal output to both console and log file doesn't work.  saving for idea stealing later.
  15. #exec > >(tee -a ${LOG_FILE} )
  16. #exec 2> >(tee -a ${LOG_FILE} >&2)
  17. function SendToLog ()
  18. {
  19.  
  20. echo "$(date +"%Y-%b-%d %T") : $1" | tee -a "$LogFile"
  21.  
  22. }
  23. ## begin log file
  24. SendToLog "Script Started"
  25. ######### Set variables for the script ############
  26. ## Determine OS version
  27. OSVersion=`sw_vers -productVersion`
  28. ## Get the currently logged in user, if any.
  29. LoggedInUser=`who | grep console | awk '{print $1}'`
  30. ## Check for updates that require a restart and ones that do not.
  31. updates=`softwareupdate -l`
  32. UpdatesNoRestart=`echo $updates | grep recommended | grep -v restart`
  33. RestartRequired=`echo $updates | grep restart | grep -v '\*' | cut -d , -f 1`
  34. ShutDownRequired=`echo $updates | grep shutdown | grep -v '\*' | cut -d , -f 1`
  35.  
  36. ################ End Variable Set ################
  37. SendToLog "OS version is $OSVersion"
  38. SendToLog "Logged in user is $LoggedInUser"
  39.  
  40. ## If there are no system updates, quit
  41. if [ "$UpdatesNoRestart" == "" -a "$RestartRequired" == "" -a "$ShutDownRequired" == "" ]; then
  42.     SendToLog "No updates at this time, updating Jamf inventory"
  43.     jamf recon
  44.     SendToLog "Inventory update complete, script exit."
  45.     exit 0
  46. fi
  47. ######### If we get to this point and beyond, there are updates. #########
  48. SendToLog "Updates found."
  49. SendToLog "Warning user"
  50. prompt=`"/Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper" -windowType hud -title "Software Update Required" -heading "Required Mac OS update" -alignHeading justified -description 'A required OS update is available for your Mac.  You will be prompted again if a reboot is required.' -alignDescription left -icon '/System/Library/CoreServices/Install Command Line Developer Tools.app/Contents/Resources/SoftwareUpdate.icns' -button1 'Start Update' -timeout 14400 -countdown -lockHUD`
  51. SendToLog "prompt equaled $prompt. 0=start 1=failed to prompt 2=canceled 239=exited"
  52.     softwareupdate -i -r && SendToLog "Updates Applied"
  53. if [ "$ShutDownRequired" != "" -a "$RestartRequired" != "" ]; then
  54.     SendToLog "no reboot required, exiting"
  55.     SendToLog "Script exit"
  56.     exit 0
  57. fi
  58. ######### If we get to this point a reboot is required #########
  59. if [ "$RestartRequired" != "" -o "$ShutDownRequired" != "" ]; then
  60.     prompt=`"/Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper" -windowType hud -title "Software Update Required" -heading "Required Mac OS update" -alignHeading justified -description 'A reboot is required to apply the OS updates to your Mac.  Please close all open applications before restarting your mac.' -alignDescription left -icon '/System/Library/CoreServices/Install Command Line Developer Tools.app/Contents/Resources/SoftwareUpdate.icns' -button1 Reboot -timeout 86400 -countdown -lockHUD`
  61.     SendToLog "prompt equaled $prompt."
  62.     SendToLog "placing reboot message"
  63.     /Library/Application\ Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper -windowType hud -lockHUD -heading 'Required Mac OS update' -description 'We are now updating your Mac System software. These updates should not take longer than 30 to 45 minutes depending on how many updates your Mac needs. If you see this screen for more than 45 minutes please call Service Desk. Your machine may reboot or shutdown.  Please do not manually turn off this computer. This message will go away when updates are complete.' -icon '/System/Library/CoreServices/Install Command Line Developer Tools.app/Contents/Resources/SoftwareUpdate.icns' > /dev/null 2>&1 &
  64. fi
  65. if [ "$ShutDownRequired" != "" ]; then
  66.     SendToLog "Starting softwareupdate --install --recommended --restart"
  67.     softwareupdate --install --recommended --restart &
  68.     exit 0
  69. else
  70.     SendToLog "starting reboot"
  71.     Jamf reboot -background -minutes 1 &
  72.     SendToLog "Script exit"
  73.     exit 0
  74. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement