-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmacOS Patching Updates.sh
299 lines (245 loc) · 9.82 KB
/
macOS Patching Updates.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
#!/bin/bash
#
# Created by Perry 28/2/2022
#
# Script to update macOS
#
#################################################################
##############################################################
# Variables
##############################################################
Notify=/Library/Application\ Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper
processor=$(uname -m)
CURRENT_USER=$(ls -l /dev/console | awk '{ print $3 }')
min_drive_space=10
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(){
# Check user has Securetoken
token=$(sudo dscl . -read /Users/$CURRENT_USER AuthenticationAuthority | grep -o 'SecureToken')
if [[ $token == SecureToken ]]; then
echo "$CURRENT_USER has a secure token. Continuing to elevate user."
else
echo "$CURRENT_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 $CURRENT_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
##############################################################
# 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 Updates" \
-heading "MacOS Updates Available" \
-description "MacOS updates are available to install.
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 updates"
rm $day1
rm $day2
rm $day3
rm $day4
elif [[ ! -f $day1 ]]; then
echo "User postponed the macOS updates 1st Day" > $day1
echo "User postponed the macOS updates 1st Day"
exit 0
elif [[ -f $day1 ]] && [[ ! -f $day2 ]]; then
echo "User postponed the macOS updates 2nd Day" > $day2
echo "User postponed the macOS updates 2nd Day"
exit 0
elif [[ -f $day1 ]] && [[ -f $day2 ]] && [[ ! -f $day3 ]]; then
echo "User postponed the macOS updates 3rd Day" > $day3
echo "User postponed the macOS updates 3rd Day"
exit 0
elif [[ -f $day1 ]] && [[ -f $day2 ]] && [[ -f $day3 ]] && [[ ! -f $day4 ]]; then
echo "User postponed the macOS updates 4th Day" > $day4
echo "User postponed the macOS updates 4th Day"
exit 0
elif [[ -f $day4 ]]; then
message=$("$Notify" \
-windowType hud \
-lockHUD \
-title "MacOS Updates" \
-heading "MacOS Updates 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
##############################################################
# 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"
##############################################################
# Run software update
##############################################################
if [[ $processor == arm64 ]]; then
echo "Mac is M1"
launchctl kickstart -k system/com.apple.softwareupdated
if dscl . read /Groups/admin | grep $CURRENT_USER; then
echo "$CURRENT_USER is admin. Checking Secure Token status....."
token=$(sudo dscl . -read /Users/$CURRENT_USER AuthenticationAuthority | grep -o 'SecureToken')
if [[ $token == SecureToken ]]; then
echo "$CURRENT_USER has a secure token. Continuing updates....."
else
echo "$CURRENT_USER does not have a secure token. A local admin will be needed to run upgrades."
"$Notify" \
-windowType hud \
-lockHUD \
-title "MacOS Updates" \
-heading "MacOS Update Error" \
-description "MacOS Updates cannot be installed.
The user account does not have a secure token. Please contact your administrator." \
-icon /System/Library/PreferencePanes/SoftwareUpdate.prefPane/Contents/Resources/SoftwareUpdate.icns \
-button1 "Ok" \
-defaultButton 1 \
exit 1
fi
else
echo "$CURRENT_USER is not admin"
elevate
# Create plist to remove admin at next login
# Create directory and removal script
mkdir -p /Library/.TRAMS/Scripts/
cat << EOF > /Library/.TRAMS/Scripts/RemoveAdmin.sh
#!/bin/bash
dseditgroup -o edit -d $user -t user admin
EOF
chown root:wheel /Library/.TRAMS/Scripts/RemoveAdmin.sh
chmod 755 /Library/.TRAMS/Scripts/RemoveAdmin.sh
# 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
chown root:wheel /Library/LaunchDaemons/com.Trams.adminremove.plist
chmod 644 /Library/LaunchDaemons/com.Trams.adminremove.plist
# Check secure token status
token=$(sudo dscl . -read /Users/$CURRENT_USER AuthenticationAuthority | grep -o 'SecureToken')
if [[ $token == SecureToken ]]; then
echo "$CURRENT_USER has a secure token. Continuing updates....."
else
echo "$CURRENT_USER does not have a secure token. A local admin will be needed to run upgrades."
"$Notify" \
-windowType hud \
-lockHUD \
-title "MacOS Updates" \
-heading "MacOS Update Error" \
-description "MacOS Updates cannot be installed.
The user account does not have a secure token. Please contact your administrator." \
-icon /System/Library/PreferencePanes/SoftwareUpdate.prefPane/Contents/Resources/SoftwareUpdate.icns \
-button1 "Ok" \
-defaultButton 1 \
exit 1
fi
fi
# Get Password for updates
adminPswd=$(osascript -e 'Tell application "System Events" to display dialog "To install the available macOS updates 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 $CURRENT_USER $adminPswd)
until [[ $pswdCheck == "" ]]
do
echo "Password was incorrect"
adminPswd=$(osascript -e 'Tell application "System Events" to display dialog "Password is 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 $CURRENT_USER $adminPswd)
echo $pswdCheck
done
echo "Password Validation passed. Continuing Updates....."
sleep 5
# Run Updates
"$Notify" \
-windowType hud \
-lockHUD \
-title "MacOS Updates" \
-heading "MacOS Updates Installing" \
-description "MacOS updates are now being installed.
This process can take 20-60min so please do not turn off your device during this time.
Once the update is downloaded and ready to install your device will reboot so please save any open work." \
-icon /System/Library/PreferencePanes/SoftwareUpdate.prefPane/Contents/Resources/SoftwareUpdate.icns &
expect -c "
set timeout -1
spawn softwareupdate --install --all --restart --force --no-scan --agree-to-license
expect \"Password:\"
send {${adminPswd}}
send \r
expect eof
wait
" > /dev/null
else
echo "Mac is Intel"
"$Notify" \
-windowType hud \
-lockHUD \
-title "MacOS Updates" \
-heading "MacOS Updates Installing" \
-description "MacOS updates are now being installed.
This process can take 20-60min so please do not turn off your device during this time.
Once the update is downloaded and ready to install your device will reboot so please save any open work." \
-icon /System/Library/PreferencePanes/SoftwareUpdate.prefPane/Contents/Resources/SoftwareUpdate.icns &
softwareupdate --install --all --agree-to-license --restart --force
fi