-
Notifications
You must be signed in to change notification settings - Fork 14
/
MAUCacheAdmin
482 lines (448 loc) · 15.6 KB
/
MAUCacheAdmin
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
#!/bin/bash
#set -x
TOOL_NAME="Microsoft AutoUpdate Cache Admin"
TOOL_VERSION="2.4"
## Copyright (c) 2019 Microsoft Corp. All rights reserved.
## Scripts are not supported under any Microsoft standard support program or service. The scripts are provided AS IS without warranty of any kind.
## Microsoft disclaims all implied warranties including, without limitation, any implied warranties of merchantability or of fitness for a
## particular purpose. The entire risk arising out of the use or performance of the scripts and documentation remains with you. In no event shall
## Microsoft, its authors, or anyone else involved in the creation, production, or delivery of the scripts be liable for any damages whatsoever
## (including, without limitation, damages for loss of business profits, business interruption, loss of business information, or other pecuniary
## loss) arising out of the use of or inability to use the sample scripts or documentation, even if Microsoft has been advised of the possibility
## of such damages.
## Feedback: pbowden@microsoft.com
# Constants
MAUID_MAU3X="0409MSau03"
MAUID_MAU4X="0409MSau04"
MAUID_WORD2019="0409MSWD2019"
MAUID_EXCEL2019="0409XCEL2019"
MAUID_POWERPOINT2019="0409PPT32019"
MAUID_OUTLOOK2019="0409OPIM2019"
MAUID_ONENOTE2019="0409ONMC2019"
MAUID_WORD2016="0409MSWD15"
MAUID_EXCEL2016="0409XCEL15"
MAUID_POWERPOINT2016="0409PPT315"
MAUID_OUTLOOK2016="0409OPIM15"
MAUID_ONENOTE2016="0409ONMC15"
MAUID_OFFICE2011="0409MSOF14"
MAUID_LYNC2011="0409UCCP14"
MAUID_SKYPE2016="0409MSFB16"
MAUID_INTUNECP="0409IMCP01"
MAUID_REMOTEDESKTOP10="0409MSRD10"
CHANNEL_COLLATERAL_PROD="https://officecdn-microsoft-com.akamaized.net/pr/C1297A47-86C4-4C1F-97FA-950631F94777/MacAutoupdate/"
CHANNEL_COLLATERAL_INSIDERSLOW="https://officecdn-microsoft-com.akamaized.net/pr/1ac37578-5a24-40fb-892e-b89d85b6dfaa/MacAutoupdate/"
CHANNEL_COLLATERAL_INSIDERFAST="https://officecdn-microsoft-com.akamaized.net/pr/4B2D7701-0A4F-49C8-B4CB-0C2D4043F51F/MacAutoupdate/"
SCRATCH_AREA="$TMPDIR""MAUCache"
# Variables for HipChat Notifications
HIPCHAT_NOTIFY=false
HIPCHAT_NOTIFY_ADDRESS="https://api.hipchat.com/v1/rooms/"
HIPCHAT_AUTH_TOKEN="<PUT HIPCHAT AUTHTOKEN HERE>"
HIPCHAT_ROOM_ID="<PUT HIPCHAT ROOM ID HERE>"
# Variables for Slack Notifications
SLACK_NOTIFY=false
SLACK_WEBHOOK_URL="https://hooks.slack.com/services/<COMPLETE URL HERE>"
SLACK_ICON_URL="https://macadmins.software/icons/mau4.png"
# Platform detection
PLATFORM=$(uname -s)
ShowUsage () {
# Shows tool usage and parameters
echo $TOOL_NAME - $TOOL_VERSION
echo "Purpose: Downloads MAU collateral and packages from the Office CDN to a local web server"
echo "Usage: MAUCacheAdmin --CachePath:<path> [--CheckInterval:<minutes>] [--HTTPOnly] [--NoPackages] [--NoCollateral] [--ShowCollateral] [--CopyCollateralFrom:<from Folder>] [--CopyCollateralTo:<to Folder>] [--InsiderSupport]"
echo "Example: MAUCacheAdmin --CachePath:/Volumes/web/MAU/cache --CheckInterval:60"
echo "Example: MAUCacheAdmin --CachePath:/Volumes/web/MAU/cache --ShowCollateral"
echo "Example: MAUCacheAdmin --CachePath:/Volumes/web/MAU/cache --CopyCollateralFrom:15.27.16101000 --CopyCollateralTo:Production"
echo
exit 0
}
InitializeScratchArea () {
# Creates and cleans temporary file area
if [ -d "$SCRATCH_AREA" ]; then
rm -i -f "$SCRATCH_AREA""*"
else
mkdir "$SCRATCH_AREA"
fi
}
InitializeCacheArea () {
# Verifies cache area connectivity and cleans up as necessary
if [ ! -d "$CACHEPATH" ]; then
echo "Error: $CACHEPATH is not accessible"
echo
exit 1
fi
}
ResolveDownloadUrl () {
# Selects either HTTPS from the Office CDN origin, or HTTP directly from Akamai
if [ "$HTTPONLY" ]; then
HTTPURL=(${1/https/http})
FILEURL=(${HTTPURL/officecdn.microsoft.com/officecdn-microsoft-com.akamaized.net})
else
FILEURL="$1"
fi
echo "$FILEURL"
}
BuildApplicationArray () {
# Builds an array of all the MAU-enabled applications that we care about
MAUAPP[0]="$MAUID_MAU3X"
MAUAPP[1]="$MAUID_MAU4X"
MAUAPP[2]="$MAUID_WORD2019"
MAUAPP[3]="$MAUID_EXCEL2019"
MAUAPP[4]="$MAUID_POWERPOINT2019"
MAUAPP[5]="$MAUID_OUTLOOK2019"
MAUAPP[6]="$MAUID_ONENOTE2019"
MAUAPP[7]="$MAUID_WORD2016"
MAUAPP[8]="$MAUID_EXCEL2016"
MAUAPP[9]="$MAUID_POWERPOINT2016"
MAUAPP[10]="$MAUID_OUTLOOK2016"
MAUAPP[11]="$MAUID_ONENOTE2016"
MAUAPP[12]="$MAUID_OFFICE2011"
MAUAPP[13]="$MAUID_LYNC2011"
MAUAPP[14]="$MAUID_SKYPE2016"
MAUAPP[15]="$MAUID_INTUNECP"
MAUAPP[16]="$MAUID_REMOTEDESKTOP10"
}
BuildCollateralArray () {
# Builds an array of MAU collateral file-paths which we'll use later for downloading XML and CAT files
DOWNLOADARRAY[0]="$1"$MAUID_MAU3X
DOWNLOADARRAY[1]="$1"$MAUID_MAU4X
DOWNLOADARRAY[2]="$1"$MAUID_WORD2019
DOWNLOADARRAY[3]="$1"$MAUID_EXCEL2019
DOWNLOADARRAY[4]="$1"$MAUID_POWERPOINT2019
DOWNLOADARRAY[5]="$1"$MAUID_OUTLOOK2019
DOWNLOADARRAY[6]="$1"$MAUID_ONENOTE2019
DOWNLOADARRAY[7]="$1"$MAUID_WORD2016
DOWNLOADARRAY[8]="$1"$MAUID_EXCEL2016
DOWNLOADARRAY[9]="$1"$MAUID_POWERPOINT2016
DOWNLOADARRAY[10]="$1"$MAUID_OUTLOOK2016
DOWNLOADARRAY[11]="$1"$MAUID_ONENOTE2016
DOWNLOADARRAY[12]="$1"$MAUID_OFFICE2011
DOWNLOADARRAY[13]="$1"$MAUID_LYNC2011
DOWNLOADARRAY[14]="$1"$MAUID_SKYPE2016
DOWNLOADARRAY[15]="$1"$MAUID_INTUNECP
DOWNLOADARRAY[16]="$1"$MAUID_REMOTEDESKTOP10
}
DownloadCollateralFiles () {
# Downloads XML/CAT collateral files
for i in "$@"
do
echo Downloading collateral file: "$i"
(cd "$SCRATCH_AREA" && curl --progress-bar --remote-name --location "$i.{xml,cat}" && curl --progress-bar --remote-name --location "$i-chk.xml")
done
}
DownloadProductionBuildSource () {
# Downloads list of official production builds
local CHANNELURL="$1"
(cd "$SCRATCH_AREA" && curl --progress-bar --remote-name --location "$CHANNELURL""builds.txt")
IFS=$'\n' read -d '' -r -a PRODBUILDS < "$SCRATCH_AREA""/builds.txt"
if [[ "$PRODBUILDS[0]" == "1"* ]]; then
return
else
PRODBUILDS=""
fi
}
ArchiveCollateralFiles () {
# Archives collateral files in the cache folder
local APPID="$1"
if [ $NOCOLLATERAL ]; then
return
else
if [ ! -d "$CACHEPATH/collateral" ]; then
mkdir "$CACHEPATH/collateral"
fi
local APPVER=$(GetAppVersionFromXML "$APPID")
if [ ! -d "$CACHEPATH/collateral/$APPVER" ]; then
mkdir "$CACHEPATH/collateral/$APPVER"
fi
cp "$SCRATCH_AREA/$APPID-chk.xml" "$CACHEPATH/collateral/$APPVER"
cp "$SCRATCH_AREA/$APPID.xml" "$CACHEPATH/collateral/$APPVER"
cp "$SCRATCH_AREA/$APPID.cat" "$CACHEPATH/collateral/$APPVER"
fi
}
DownloadUpdatePackages () {
# Logic to evaluate what files need to be downloaded into the cache
APPID="$1"
APPNAME=$(GetAppNameFromMAUID "$1")
if [ "$APPID" == "0409MSOF14" ] || [ "$APPID" == "0409UCCP14" ]; then
LOCATION=($(cd $SCRATCH_AREA && (grep -o 'http[^\"]*dmg' $APPID".xml") | sort | uniq))
else
LOCATION=($(cd $SCRATCH_AREA && (grep -o 'http[^\"]*pkg' $APPID".xml") | sort | uniq))
fi
for i in "${LOCATION[@]}"
do
SKIPDOWNLOAD=false
PACKAGENAME=$(basename "$i")
if [[ $PACKAGENAME == *"Delta"* ]]; then
for PRODVER in "${PRODBUILDS[@]}"
do
if [[ $PACKAGENAME == *"$PRODVER""_to_"* ]]; then
SKIPDOWNLOAD=false
break
else
SKIPDOWNLOAD=true
fi
done
fi
if [ "$SKIPDOWNLOAD" == false ]; then
PACKAGEURL=$(ResolveDownloadUrl "$i")
PACKAGESIZECDN=$(GetDownloadSize "$i")
PACKAGESIZELOCAL=$(GetLocalSize "$PACKAGENAME")
PACKAGESIZECDNMEG=$(expr $PACKAGESIZECDN / 1024 / 1024)
if [ -f "$CACHEPATH/$PACKAGENAME" ]; then
if [ "$PACKAGESIZELOCAL" == "$PACKAGESIZECDN" ]; then
echo Package "$i" already exists in the cache ...skipping
else
echo Package "$i" exists in the cache but is corrupt ...removing
(cd "$CACHEPATH" && rm -f "$i")
DownloadPackage "$PACKAGEURL" "$APPNAME" "$PACKAGESIZECDNMEG"
fi
else
DownloadPackage "$PACKAGEURL" "$APPNAME" "$PACKAGESIZECDNMEG"
fi
fi
done
}
GetAppVersionFromXML () {
# Returns the current app version from the XML collateral
XML="$1"".xml"
local APPVER=($(cd $SCRATCH_AREA && grep -A1 -m2 'Update Version' "$XML" | grep 'string' | sed -e 's,.*<string>\([^<]*\)</string>.*,\1,g'))
if [ "$APPVER" == '' ]; then
echo "Legacy"
else
echo "$APPVER"
fi
}
GetAppNameFromMAUID () {
# Performs a reverse look-up from MAUID to friendly name
case "$1" in
$MAUID_MAU3X) APPNAME="MAU 3.x"
;;
$MAUID_MAU4X) APPNAME="MAU 4.x"
;;
$MAUID_WORD2019) APPNAME="Word 2019"
;;
$MAUID_EXCEL2019) APPNAME="Excel 2019"
;;
$MAUID_POWERPOINT2019) APPNAME="PowerPoint 2019"
;;
$MAUID_OUTLOOK2019) APPNAME="Outlook 2019"
;;
$MAUID_ONENOTE2019) APPNAME="OneNote 2019"
;;
$MAUID_WORD2016) APPNAME="Word 2016"
;;
$MAUID_EXCEL2016) APPNAME="Excel 2016"
;;
$MAUID_POWERPOINT2016) APPNAME="PowerPoint 2016"
;;
$MAUID_OUTLOOK2016) APPNAME="Outlook 2016"
;;
$MAUID_ONENOTE2016) APPNAME="OneNote 2016"
;;
$MAUID_OFFICE2011) APPNAME="Office 2011"
;;
$MAUID_LYNC2011) APPNAME="Lync 2011"
;;
$MAUID_SKYPE2016) APPNAME="Skype for Business 2016"
;;
$MAUID_INTUNECP) APPNAME="Intune Company Portal"
;;
$MAUID_REMOTEDESKTOP10) APPNAME="Remote Desktop v10"
;;
esac
echo "$APPNAME"
}
GetDownloadSize () {
# Gets the size of a file based on its header, then strips non-numeric characters
URL="$1"
local CONTENTHTTPLENGTH=$(curl --head -s $URL | awk '/Content-Length/' | cut -d ' ' -f2)
CONTENTLENGTH=$(echo ${CONTENTHTTPLENGTH//[!0-9]/})
echo $CONTENTLENGTH
}
GetLocalSize () {
# Gets the size of a file from the local disk
local FILENAME="$1"
# The stat command works differently between macOS and other Linux platforms like RHEL
if [ "$PLATFORM" == "Darwin" ]; then
local FILELENGTH=($(cd "$CACHEPATH" && stat -f%z "$FILENAME"))
else
local FILELENGTH=($(cd "$CACHEPATH" && stat -c%s "$FILENAME"))
fi
echo $FILELENGTH
}
HipChatNotify () {
# Send Alert to HipChat Room Specified in Global Variable when Package is Downloaded
if [ $HIPCHAT_NOTIFY == true ]; then
echo "New Package Detected: $PACKAGE"
curl -v -k -d "room_id=${HIPCHAT_ROOM_ID}&from=ServerAlert&message=""<b>MAU Server Alert</b> - New Package Downloaded:<b> ${PACKAGE}</b>""&color=purple" ${HIPCHAT_NOTIFY_ADDRESS}/message?auth_token=${HIPCHAT_AUTH_TOKEN}&format=json
fi
}
SlackNotify () {
# Send Alert to Slack Webhook in Global Variable when Package is Downloaded
if [ $SLACK_NOTIFY == true ]; then
echo "New Package Detected: $PACKAGE"
curl -X POST -H 'Content-type: application/json' --data '{"username":"MAUCacheAdmin","icon_url":"'"$SLACK_ICON_URL"'","text":"New Package Downloaded: '"$PACKAGE"'"}' $SLACK_WEBHOOK_URL
fi
}
DownloadPackage () {
# Downloads the specified update package
URL="$1"
APPLICATION="$2"
SIZE="$3"
PACKAGE=$(basename "$1")
HipChatNotify
SlackNotify
echo "==================================================="
echo Application: "$APPLICATION"
echo Package: "$PACKAGE"
echo Size: "$SIZE" MB
echo URL: "$URL"
(cd "$CACHEPATH" && curl --progress-bar --remote-name --location $URL)
}
ShowCollateral () {
# Shows collateral information of the downloaded collateral files
echo "----------------------------------------------------------------------------------------"
printf '%-14s %-42s %-11s %s\n' \
"App ID" "Application" "Build Date" "[Folder]"
echo "----------------------------------------------------------------------------------------"
for d in "$CACHEPATH/collateral/"*;
do
cd "$d/";
if ls *.xml >/dev/null 2>&1; then
for XML in *.xml;
do
Folder=$(echo "$d" | awk -F "/" '{print $NF}')
if [ "$Folder" = "Legacy" ]; then
AppIdentifier=$(echo "$XML" | sed -e 's/.xml//g')
ver=""
BuildDate=$(grep -A1 'Date' "$XML" | grep 'date' | sed -e 's,.*<date>\([^<]*\)</date>.*,\1,g' | tail -1 | cut -d"T" -f1)
AppTitle=$(grep -A1 'Title' "$XML" | grep 'string' | sed -e 's,.*<string>\([^<]*\)</string>.*,\1,g' | tail -1)
printf '%-14s %-42s %-11s %s\n' \
"$AppIdentifier" "$AppTitle" "$BuildDate" "[$Folder]"
else
AppIdentifier=$(echo "$XML" | sed -e 's/.xml//g')
ver=$(grep -A1 -m2 'Update Version' "$XML" | grep 'string' | sed -e 's,.*<string>\([^<]*\)</string>.*,\1,g')
BuildDate=$(grep -A1 'Date' "$XML" | grep 'date' | sed -e 's,.*<date>\([^<]*\)</date>.*,\1,g' | tail -1 | cut -d"T" -f1)
AppTitle=$(grep -A1 'Title' "$XML" | grep 'string' | sed -e 's,.*<string>\([^<]*\)</string>.*,\1,g' | tail -1)
printf '%-14s %-42s %-11s %s\n' \
"$AppIdentifier" "$AppTitle" "$BuildDate" "[$Folder]"
fi
done
else
Folder=$(echo "$d" | awk -F "/" '{print $NF}')
printf '%-14s %-42s %-11s %s\n' \
"" "" "" "[$Folder]"
fi
done
echo "----------------------------------------------------------------------------------------"
exit 0
}
CopyCollateral () {
# Copies collateral files from one folder to another.
COPYCOLLATERALFROM=$(echo $COPYCOLLATERALFROM | sed 's/[][]//g')
COPYCOLLATERALTO=$(echo $COPYCOLLATERALTO | sed 's/[][]//g')
echo "Copying collateral files FROM:[$COPYCOLLATERALFROM] folder TO:[$COPYCOLLATERALTO] folder."
cp "$CACHEPATH/collateral/$COPYCOLLATERALFROM/"* "$CACHEPATH/collateral/$COPYCOLLATERALTO/"
exit 0
}
# Evaluate command-line arguments
if [[ $# = 0 ]]; then
ShowUsage
else
for KEY in "$@"
do
case $KEY in
--Help|-h|--help)
ShowUsage
shift # past argument
;;
--CachePath:*|-c:*|--cachepath:*)
CACHEPATH=${KEY#*:}
shift # past argument
;;
--CheckInterval:*|-i:*|--checkinterval:*)
CHECKINTERVAL=${KEY#*:}
shift # past argument
;;
--HTTPOnly|-h|--httponly)
HTTPONLY=true
shift # past argument
;;
--NoPackages|-np|--nopackages)
NOPACKAGES=true
shift # past argument
;;
--NoCollateral|-n|--nocollateral)
NOCOLLATERAL=true
shift # past argument
;;
--ShowCollateral|-sc|--showcollateral)
ShowCollateral
shift # past argument
;;
--CopyCollateralFrom:*|-ccf:*|--copycollateralfrom:*)
COPYCOLLATERALFROM=${KEY#*:}
shift # past argument
;;
--CopyCollateralTo:*|-cct:*|--copycollateralto:*)
COPYCOLLATERALTO=${KEY#*:}
CopyCollateral
shift # past argument
;;
--InsiderSupport|-is|--insidersupport)
INSIDER=true
shift # past argument
;;
*)
ShowUsage
;;
esac
shift # past argument or value
done
fi
## Main
while :
do
# Get a clean area for writing temporary files
InitializeScratchArea
# Verify that the cache area is ready to go
InitializeCacheArea
# Build channel array
CHANNELS[0]="$CHANNEL_COLLATERAL_PROD"
if [ $INSIDER ]; then
CHANNELS[1]="$CHANNEL_COLLATERAL_INSIDERSLOW"
CHANNELS[2]="$CHANNEL_COLLATERAL_INSIDERFAST"
fi
# Routine for all channels in the array
for c in "${CHANNELS[@]}"
do
# Resolve collateral download URL
COLLATERALURL=$(ResolveDownloadUrl "$c")
# Download list of production builds as source
DownloadProductionBuildSource "$COLLATERALURL"
# Build an array of the collateral files to download
BuildCollateralArray "$COLLATERALURL"
# Download collateral files for each application
DownloadCollateralFiles "${DOWNLOADARRAY[@]}"
# Build an array of all MAU-enabled applications
BuildApplicationArray
# Build an array of each package location and download those packages
for a in "${MAUAPP[@]}"
do
ArchiveCollateralFiles "$a"
if [ ! $NOPACKAGES ]; then
DownloadUpdatePackages "$a" "$c"
fi
done
done
# If CheckInterval wasn't specified on the command-line, just run once
if [ "$CHECKINTERVAL" == '' ]; then
exit 0
else
# Otherwise, sleep for the specified number of minutes before checking again
echo "Sleeping for $CHECKINTERVAL minutes..."
CHECKINTERVALSECS=$(expr $CHECKINTERVAL \* 60)
# Wait until the next check interval
sleep "$CHECKINTERVALSECS"
fi
done
exit 0