-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmacOS Patching Upgrades.sh
374 lines (289 loc) · 12.1 KB
/
macOS Patching Upgrades.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
#!/bin/bash
#
# Created by Perry 28/2/2022
#
# Script to Upgrade macOS
#
#################################################################
##############################################################
# Variables
##############################################################
Latest=$(curl https://en.wikipedia.org/wiki/MacOS | grep 'Latest release' | tr '<' '\n' | grep 'infobox-data' | sed -n 4p | sed -e 's#td class="infobox-data">##' | xargs)
processor=$(uname -m)
Notify=/Library/Application\ Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper
user=$(ls -l /dev/console | awk '{ print $3 }')
min_drive_space=45
free_disk_space=$(osascript -l 'JavaScript' -e "ObjC.import('Foundation'); var freeSpaceBytesRef=Ref(); $.NSURL.fileURLWithPath('/').getResourceValueForKeyError(freeSpaceBytesRef, 'NSURLVolumeAvailableCapacityForImportantUsageKey', null); Math.round(ObjC.unwrap(freeSpaceBytesRef[0]) / 1000000000)") # with thanks to Pico
##############################################################
# Elevate user to admin
##############################################################
elevate(){
# Check user has Securetoken
token=$(sudo dscl . -read /Users/$user AuthenticationAuthority | grep -o 'SecureToken')
if [[ $token == SecureToken ]]; then
echo "$user has a secure token. Continuing to elevate user."
else
echo "$user does not have a secure token. A local admin will be needed to run upgrades."
fi
# Elevate user account
dscl . -append /groups/admin GroupMembership $user
}
##############################################################
# Free space check
##############################################################
if [[ ! "$free_disk_space" ]]; then
# fall back to df -h if the above fails
free_disk_space=$(df -Pk . | column -t | sed 1d | awk '{print $4}')
fi
if [[ $free_disk_space -ge $min_drive_space ]]; then
echo "OK - $free_disk_space GB free/purgeable disk space detected"
else
echo "ERROR - $free_disk_space GB free/purgeable disk space detected"
exit 1
fi
##############################################################
# Check if MIST is installed
##############################################################
if ! command -v mist &> /dev/null
then
echo "Mist is not installed. App will be installed now....."
sleep 2
# Variables
pkgfile="MIST.pkg"
logfile="/Library/Logs/MISTInstallScript.log"
version=$(curl -s https://github.com/ninxsoft/mist-cli | grep releases/tag | tr '/' ' ' | awk '{print $12}' | tr -d '"''>''v')
url="https://github.com/ninxsoft/mist-cli/releases/download/v$version/mist-cli.$version.pkg"
# Start Log entries
echo "--" >> ${logfile}
echo "`date`: Downloading latest version." >> ${logfile}
# Download installer
curl -L -J -o /tmp/${pkgfile} ${url}
echo "`date`: Installing..." >> ${logfile}
# Change to installer directory
cd /tmp
# Install application
sudo installer -pkg ${pkgfile} -target /
sleep 5
echo "`date`: Deleting package installer." >> ${logfile}
# Remove downloaded installer
rm /tmp/"${pkgfile}"
else
echo "MIST is installed. Continuing macOS Upgrade....."
fi
##############################################################
# Upgrade available and Deferment notification
##############################################################
day1=/var/tmp/postponed.txt
day2=/var/tmp/postponed2.txt
day3=/var/tmp/postponed3.txt
day4=/var/tmp/postponed4.txt
deferment(){
message=$("$Notify" \
-windowType hud \
-lockHUD \
-title "MacOS Upgrade" \
-heading "MacOS Upgrade Available" \
-description "A macOS upgrade is available.
This process can take 20-60min so please do not turn off your device during this time.
Your device will reboot by itself once completed." \
-icon /System/Library/PreferencePanes/SoftwareUpdate.prefPane/Contents/Resources/SoftwareUpdate.icns \
-button1 "Install now" \
-button2 "Postpone" \
-defaultButton 1 \
)
if [[ $message == 0 ]]; then
echo "User agreed to install macOS upgrade"
rm $day1
rm $day2
rm $day3
rm $day4
elif [[ ! -f $day1 ]]; then
echo "User postponed the macOS upgrade 1st Day" > $day1
echo "User postponed the macOS upgrade 1st Day"
exit 0
elif [[ -f $day1 ]] && [[ ! -f $day2 ]]; then
echo "User postponed the macOS upgrade 2nd Day" > $day2
echo "User postponed the macOS upgrade 2nd Day"
exit 0
elif [[ -f $day1 ]] && [[ -f $day2 ]] && [[ ! -f $day3 ]]; then
echo "User postponed the macOS upgrade 3rd Day" > $day3
echo "User postponed the macOS upgrade 3rd Day"
exit 0
elif [[ -f $day1 ]] && [[ -f $day2 ]] && [[ -f $day3 ]] && [[ ! -f $day4 ]]; then
echo "User postponed the macOS upgrade 4th Day" > $day4
echo "User postponed the macOS upgrade 4th Day"
exit 0
elif [[ -f $day4 ]]; then
message=$("$Notify" \
-windowType hud \
-lockHUD \
-title "MacOS Upgrade" \
-heading "MacOS Upgrade Available" \
-description "Update postponement has passed 4 days.
Your device will now be updated.
This process can take 20-60min so please do not turn off your device during this time.
Your device will reboot by itself once completed." \
-icon /System/Library/PreferencePanes/SoftwareUpdate.prefPane/Contents/Resources/SoftwareUpdate.icns \
-button1 "Install now" \
-defaultButton 1 \
)
rm $day1
rm $day2
rm $day3
rm $day4
fi
}
deferment
sleep 5
##############################################################
# Check and Download macOS
##############################################################
OScheck=$(ls /Applications/ | grep macOS)
if [[ $OScheck == "" ]]; then
echo "No installer found. Downloading now"
"$Notify" \
-windowType hud \
-lockHUD \
-title "MacOS Upgrade" \
-heading "MacOS Upgrade Downloading" \
-description "A macOS upgrade is available to Download.
This process can take 20-60min so please do not turn off your device during this time.
Your device will continue the upgrade once the new OS has downloaded." \
-icon /System/Library/PreferencePanes/SoftwareUpdate.prefPane/Contents/Resources/SoftwareUpdate.icns &
sudo launchctl kickstart -k system/com.apple.softwareupdated
sleep 5
sudo mist download installer "$Latest" application --output-directory "/Applications/" --quiet
killall jamfHelper
else
echo "macOS installer already downloaded"
fi
sleep 5
##############################################################
# Check Battery state
##############################################################
bat=$(pmset -g batt | grep 'AC Power')
model=$(ioreg -l | awk '/product-name/ { split($0, line, "\""); printf("%s\n", line[4]); }')
if [[ "$model" = *"Book"* ]]; then
until [[ $bat == "Now drawing from 'AC Power'" ]]; do
echo "Device not connected to power source"
"$Notify" \
-windowType hud \
-lockHUD \
-title "MacOS Updates" \
-heading "Connect Charger" \
-description "Please connect your device to a charger to continue installing updates." \
-icon /System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/AlertStopIcon.icns \
-button1 "Continue" \
-defaultButton 1 \
bat=$(pmset -g batt | grep 'AC Power')
sleep 2
done
fi
echo "Device connected to power source"
##############################################################
# Core upgrade script
##############################################################
if [[ $processor == arm64 ]]; then
echo "Mac is M1"
# Get credentials for upgrade
user=$(ls -l /dev/console | awk '{ print $3 }')
if dscl . read /Groups/admin | grep $user; then
echo "$user is admin"
adminuser=$user
adminpswd=$(osascript -e 'Tell application "System Events" to display dialog "To install the available macOS upgrade please enter your password" buttons {"Continue"} default button 1 with title "macOS Upgrade" with icon alias "System:Applications:Utilities:Keychain Access.app:Contents:Resources:AppIcon.icns" with hidden answer default answer ""' -e 'text returned of result' 2>/dev/null)
pswdCheck=$(dscl /Local/Default -authonly $user $adminPswd)
until [[ $pswdCheck == "" ]]
do
echo "Password was incorrect"
adminPswd=$(osascript -e 'Tell application "System Events" to display dialog "Password was incorrect. Please try again." buttons {"Continue"} default button 1 with title "macOS Upgrade" with icon alias "System:Applications:Utilities:Keychain Access.app:Contents:Resources:AppIcon.icns" with hidden answer default answer ""' -e 'text returned of result' 2>/dev/null)
pswdCheck=$(dscl /Local/Default -authonly $user $adminPswd)
echo $pswdCheck
done
echo "Password Validation passed. Continuing Updates....."
sleep 5
else
echo "$user is not admin. Elevating user account....."
# Create directory and removal script
mkdir -p /Library/.TRAMS/Scripts/
sleep 2
cat << EOF > /Library/.TRAMS/Scripts/RemoveAdmin.sh
#!/bin/bash
dseditgroup -o edit -d $user -t user admin
EOF
if [[ -f /Library/.TRAMS/Scripts/RemoveAdmin.sh ]]; then
echo "Admin removal script setup ok."
chown root:wheel /Library/.TRAMS/Scripts/RemoveAdmin.sh
chmod 755 /Library/.TRAMS/Scripts/RemoveAdmin.sh
else
echo "Admin removal script setup failed."
exit 1
fi
# Create plist to remove admin at next login
cat << EOF > /Library/LaunchDaemons/com.Trams.adminremove.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.Trams.adminremove</string>
<key>ProgramArguments</key>
<array>
<string>/Library/.TRAMS/Scripts/RemoveAdmin.sh</string>
</array>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
EOF
# Permission plist
if [[ -f /Library/LaunchDaemons/com.Trams.adminremove.plist ]]; then
echo "Admin removal LaunchDaemon setup ok."
chown root:wheel /Library/LaunchDaemons/com.Trams.adminremove.plist
chmod 644 /Library/LaunchDaemons/com.Trams.adminremove.plist
else
echo "Admin removal LaunchDaemon setup failed."
exit 1
fi
fi
elevate
sleep 5
adminuser=$user
adminpswd=$(osascript -e 'Tell application "System Events" to display dialog "To install the available macOS upgrade please enter your password" buttons {"Continue"} default button 1 with title "macOS Upgrade" with icon alias "System:Applications:Utilities:Keychain Access.app:Contents:Resources:AppIcon.icns" with hidden answer default answer ""' -e 'text returned of result' 2>/dev/null)
pswdCheck=$(dscl /Local/Default -authonly $user $adminPswd)
until [[ $pswdCheck == "" ]]
do
echo "Password was incorrect"
adminPswd=$(osascript -e 'Tell application "System Events" to display dialog "Password was incorrect. Please try again." buttons {"Continue"} default button 1 with title "macOS Upgrade" with icon alias "System:Applications:Utilities:Keychain Access.app:Contents:Resources:AppIcon.icns" with hidden answer default answer ""' -e 'text returned of result' 2>/dev/null)
pswdCheck=$(dscl /Local/Default -authonly $user $adminPswd)
echo $pswdCheck
done
echo "Password Validation passed. Continuing Updates....."
sleep 5
# Install macOS
"$Notify" \
-windowType hud \
-lockHUD \
-title "MacOS Upgrade" \
-heading "MacOS Upgrade Installing" \
-description "A macOS upgrade is now being Installed.
This process can take 20-60min so please do not turn off your device during this time.
Your device will Reboot automatically to finish off the install so please make sure any open work is saved." \
-icon /System/Library/PreferencePanes/SoftwareUpdate.prefPane/Contents/Resources/SoftwareUpdate.icns &
OSInstaller=$(ls /Applications/ | grep -i 'install macOS' )
echo $adminpswd | "/Applications/$OSInstaller/Contents/Resources/startosinstall" --agreetolicense --force --user $adminuser --stdinpass
else
echo "Mac is Intel"
sleep 5
# Install macOS
"$Notify" \
-windowType hud \
-lockHUD \
-title "MacOS Upgrade" \
-heading "MacOS Upgrade Installing" \
-description "A macOS upgrade is now being Installed.
This process can take 20-60min so please do not turn off your device during this time.
Your device will Reboot automatically to finish off the install so please make sure any open work is saved." \
-icon /System/Library/PreferencePanes/SoftwareUpdate.prefPane/Contents/Resources/SoftwareUpdate.icns &
OSInstaller=$(ls /Applications/ | grep -i 'install macOS' )
"/Applications/$OSInstaller/Contents/Resources/startosinstall" --agreetolicense --force
fi