forked from dan-snelson/dialog-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSetup-Your-Mac-via-Dialog.bash
1259 lines (946 loc) · 47.4 KB
/
Setup-Your-Mac-via-Dialog.bash
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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
####################################################################################################
#
# Setup Your Mac via swiftDialog
# https://snelson.us/setup-your-mac/
#
####################################################################################################
#
# HISTORY
#
# Version 1.5.0, 28-Nov-2022, Dan K. Snelson (@dan-snelson)
# - Prompt user for additional fields at Welcome dialog
#
####################################################################################################
####################################################################################################
#
# Variables
#
####################################################################################################
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Script Version, Jamf Pro Script Parameters and default Exit Code
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
scriptVersion="1.5.0"
export PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin/
scriptLog="${4:-"/var/tmp/org.churchofjesuschrist.log"}"
debugMode="${5:-"true"}" # [ true (default) | false ]
welcomeDialog="${6:-"true"}" # [ true (default) | false ]
completionActionOption="${7:-"Restart Attended"}" # [ wait | sleep (with seconds) | Shut Down | Shut Down Attended | Shut Down Confirm | Restart | Restart Attended (default) | Restart Confirm | Log Out | Log Out Attended | Log Out Confirm ]
reconOptions="" # Initialize dynamic recon options; built based on user's input at Welcome dialog
exitCode="0"
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Reflect Debug Mode in `infotext` (i.e., bottom, left-hand corner of each dialog)
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
if [[ "${debugMode}" == "true" ]]; then
scriptVersion="DEBUG MODE | Dialog: v$(dialog --version) • Setup Your Mac: v${scriptVersion}"
fi
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Confirm Setup Assistant complete
# Useful for triggering on Enrollment Complete and will not pause if run via Self Service
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
while true
do
currentUser=$( echo "show State:/Users/ConsoleUser" | scutil | awk '/Name :/ { print $3 }' )
if [[ "$currentUser" = "root" ]] || [[ "$currentUser" = "_mbsetupuser" ]] || [[ "$currentUser" = "loginwindow" ]]; then
sleep 5
else
break
fi
done
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Set Dialog path, Command Files, JAMF binary, log files and currently logged-in user
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
dialogApp="/usr/local/bin/dialog"
welcomeCommandFile=$( mktemp /var/tmp/dialogWelcome.XXX )
setupYourMacCommandFile=$( mktemp /var/tmp/dialogSetupYourMac.XXX )
failureCommandFile=$( mktemp /var/tmp/dialogFailure.XXX )
jamfBinary="/usr/local/bin/jamf"
loggedInUser=$( echo "show State:/Users/ConsoleUser" | scutil | awk '/Name :/ { print $3 }' )
loggedInUserFullname=$( id -F "${loggedInUser}" )
loggedInUserFirstname=$( echo "$loggedInUserFullname" | cut -d " " -f 1 )
####################################################################################################
#
# Welcome dialog
#
####################################################################################################
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# "Welcome" dialog Title, Message and Icon
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
welcomeTitle="Welcome to your new Mac, ${loggedInUserFirstname}!"
welcomeMessage="To begin, please enter the required information below, then click **Continue** to start applying settings to your new Mac. \n\nOnce completed, the **Quit** button will be re-enabled and you'll be prompted to restart your Mac. \n\nIf you need assistance, please contact the Help Desk: +1 (801) 555-1212."
# Welcome icon set to either light or dark, based on user's Apperance setting (thanks, @mm2270!)
appleInterfaceStyle=$( /usr/bin/defaults read /Users/"${loggedInUser}"/Library/Preferences/.GlobalPreferences.plist AppleInterfaceStyle 2>&1 )
if [[ "${appleInterfaceStyle}" == "Dark" ]]; then
welcomeIcon="https://cdn-icons-png.flaticon.com/512/740/740878.png"
else
welcomeIcon="https://cdn-icons-png.flaticon.com/512/979/979585.png"
fi
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# "Welcome" JSON (thanks, @bartreardon!)
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
welcomeJSON='{
"title" : "'"${welcomeTitle}"'",
"message" : "'"${welcomeMessage}"'",
"icon" : "'"${welcomeIcon}"'",
"iconsize" : "198.0",
"button1text" : "Continue",
"button2text" : "Quit",
"infotext" : "'"${scriptVersion}"'",
"blurscreen" : "true",
"ontop" : "true",
"titlefont" : "size=26",
"messagefont" : "size=16",
"textfield" : [
{ "title" : "Comment",
"required" : false,
"prompt" : "Enter a comment",
"editor" : true
},
{ "title" : "Computer Name",
"required" : false,
"prompt" : "Computer Name"
},
{ "title" : "User Name",
"required" : false,
"prompt" : "User Name"
},
{ "title" : "Asset Tag",
"required" : true,
"prompt" : "Please enter the seven-digit Asset Tag",
"regex" : "^(AP|IP)?[0-9]{7,}$",
"regexerror" : "Please enter (at least) seven digits for the Asset Tag, optionally preceed by either AP or IP."
}
],
"selectitems" : [
{ "title" : "Department",
"default" : "Please select your department",
"values" : [
"Please select your department",
"Asset Management",
"Australia Area Office",
"Board of Directors",
"Business Development",
"Corporate Communications",
"Creative Services",
"Customer Service / Customer Experience",
"Engineering",
"Finance / Accounting",
"General Management",
"Human Resources",
"Information Technology / Technology",
"Investor Relations",
"Legal",
"Marketing",
"Operations",
"Product Management",
"Production",
"Project Management Office",
"Purchasing / Sourcing",
"Quality Assurance",
"Risk Management",
"Sales",
"Strategic Initiatives & Programs",
"Technology"
]
},
{ "title" : "Select B",
"values" : [
"B1",
"B2",
"B3"
]
},
{ "title" : "Select C",
"values" : [
"C1",
"C2",
"C3"
]
}
],
"height" : "635"
}'
####################################################################################################
#
# Setup Your Mac dialog
#
####################################################################################################
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# "Setup Your Mac" dialog Title, Message, Overlay Icon and Icon
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
title="Setting up ${loggedInUserFirstname}'s Mac"
message="Please wait while the following apps are installed …"
overlayicon=$( defaults read /Library/Preferences/com.jamfsoftware.jamf.plist self_service_app_path )
# Set initial icon based on whether the Mac is a desktop or laptop
if system_profiler SPPowerDataType | grep -q "Battery Power"; then
icon="SF=laptopcomputer.and.arrow.down,weight=semibold,colour1=#ef9d51,colour2=#ef7951"
else
icon="SF=desktopcomputer.and.arrow.down,weight=semibold,colour1=#ef9d51,colour2=#ef7951"
fi
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# "Setup Your Mac" dialog Settings and Features
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
dialogSetupYourMacCMD="$dialogApp \
--title \"$title\" \
--message \"$message\" \
--icon \"$icon\" \
--progress \
--progresstext \"Initializing configuration …\" \
--button1text \"Wait\" \
--button1disabled \
--infotext \"$scriptVersion\" \
--titlefont 'size=28' \
--messagefont 'size=14' \
--height '70%' \
--position 'centre' \
--blurscreen \
--ontop \
--overlayicon \"$overlayicon\" \
--quitkey k \
--commandfile \"$setupYourMacCommandFile\" "
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# "Setup Your Mac" policies to execute (Thanks, Obi-@smithjw!)
#
# For each configuration step, specify:
# - listitem: The text to be displayed in the list
# - icon: The hash of the icon to be displayed on the left
# - See: https://vimeo.com/772998915
# - progresstext: The text to be displayed below the progress bar
# - trigger: The Jamf Pro Policy Custom Event Name
# - path: The filepath for validation
#
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# The fully qualified domain name of the server which hosts your icons, including any required sub-directories
# (P.S. I tried to come up with a longer variable name, but couldn't.)
setupYourMacPolicyArrayIconPrefixUrl="https://ics.services.jamfcloud.com/icon/hash_"
# shellcheck disable=SC1112 # use literal slanted single quotes for typographic reasons
policy_array=('
{
"steps": [
{
"listitem": "FileVault Disk Encryption",
"icon": "f9ba35bd55488783456d64ec73372f029560531ca10dfa0e8154a46d7732b913",
"progresstext": "FileVault is built-in to macOS and provides full-disk encryption to help prevent unauthorized access to your Mac.",
"trigger_list": [
{
"trigger": "filevault",
"path": "/Library/Preferences/com.apple.fdesetup.plist"
}
]
},
{
"listitem": "Sophos Endpoint",
"icon": "c70f1acf8c96b99568fec83e165d2a534d111b0510fb561a283d32aa5b01c60c",
"progresstext": "You’ll enjoy next-gen protection with Sophos Endpoint which doesn’t rely on signatures to catch malware.",
"trigger_list": [
{
"trigger": "sophosEndpoint",
"path": "/Applications/Sophos/Sophos Endpoint.app/Contents/Info.plist"
}
]
},
{
"listitem": "Palo Alto GlobalProtect",
"icon": "fcccf5d72ad9a4f6d3a4d780dcd8385378a0a8fd18e8c33ad32326f5bd53cca0",
"progresstext": "Use Palo Alto GlobalProtect to establish a Virtual Private Network (VPN) connection to Church headquarters.",
"trigger_list": [
{
"trigger": "globalProtect",
"path": "/Applications/GlobalProtect.app/Contents/Info.plist"
}
]
},
{
"listitem": "Microsoft Teams",
"icon": "dcb65709dba6cffa90a5eeaa54cb548d5ecc3b051f39feadd39e02744f37c19e",
"progresstext": "Microsoft Teams is a hub for teamwork in Office 365. Keep all your team’s chats, meetings and files together in one place.",
"trigger_list": [
{
"trigger": "microsoftTeams",
"path": "/Applications/Microsoft Teams.app/Contents/Info.plist"
}
]
},
{
"listitem": "Zoom",
"icon": "be66420495a3f2f1981a49a0e0ad31783e9a789e835b4196af60554bf4c115ac",
"progresstext": "Zoom is a videotelephony software program developed by Zoom Video Communications.",
"trigger_list": [
{
"trigger": "zoom",
"path": "/Applications/zoom.us.app/Contents/Info.plist"
}
]
},
{
"listitem": "Google Chrome",
"icon": "12d3d198f40ab2ac237cff3b5cb05b09f7f26966d6dffba780e4d4e5325cc701",
"progresstext": "Google Chrome is a browser that combines a minimal design with sophisticated technology to make the Web faster.",
"trigger_list": [
{
"trigger": "googleChrome",
"path": "/Applications/Google Chrome.app/Contents/Info.plist"
}
]
},
{
"listitem": "Final Configuration",
"icon": "00d7c19b984222630f20b6821425c3548e4b5094ecd846b03bde0994aaf08826",
"progresstext": "Finalizing Configuration …",
"trigger_list": [
{
"trigger": "finalConfiguration",
"path": ""
},
{
"trigger": "reconAtReboot",
"path": ""
}
]
},
{
"listitem": "Computer Inventory",
"icon": "90958d0e1f8f8287a86a1198d21cded84eeea44886df2b3357d909fe2e6f1296",
"progresstext": "A listing of your Mac’s apps and settings — its inventory — is sent automatically to the Jamf Pro server daily.",
"trigger_list": [
{
"trigger": "recon",
"path": ""
}
]
}
]
}
')
####################################################################################################
#
# Failure dialog
#
####################################################################################################
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# "Failure" dialog Title, Message and Icon
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
failureTitle="Failure Detected"
failureMessage="Placeholder message; update in the 'finalise' function"
failureIcon="SF=xmark.circle.fill,weight=bold,colour1=#BB1717,colour2=#F31F1F"
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# "Failure" dialog Settings and Features
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
dialogFailureCMD="$dialogApp \
--moveable \
--title \"$failureTitle\" \
--message \"$failureMessage\" \
--icon \"$failureIcon\" \
--iconsize 125 \
--width 625 \
--height 400 \
--position topright \
--button1text \"Close\" \
--infotext \"$scriptVersion\" \
--titlefont 'size=22' \
--messagefont 'size=14' \
--overlayicon \"$overlayicon\" \
--commandfile \"$failureCommandFile\" "
#------------------------ With the execption of the `finalise` function, -------------------------#
#------------------------ edits below these line are optional. -----------------------------------#
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Dynamically set `button1text` based on the value of `completionActionOption`
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
case ${completionActionOption} in
"Shut Down" )
button1textCompletionActionOption="Shutting Down …"
progressTextCompletionAction="shut down and "
;;
"Shut Down "* )
button1textCompletionActionOption="Shut Down"
progressTextCompletionAction="shut down and "
;;
"Restart" )
button1textCompletionActionOption="Restarting …"
progressTextCompletionAction="restart and "
;;
"Restart "* )
button1textCompletionActionOption="Restart"
progressTextCompletionAction="restart and "
;;
"Log Out" )
button1textCompletionActionOption="Logging Out …"
progressTextCompletionAction="log out and "
;;
"Log Out "* )
button1textCompletionActionOption="Log Out"
progressTextCompletionAction="log out and "
;;
"Sleep"* )
button1textCompletionActionOption="Close"
progressTextCompletionAction=""
;;
"Quit" )
button1textCompletionActionOption="Quit"
progressTextCompletionAction=""
;;
* )
button1textCompletionActionOption="Close"
progressTextCompletionAction=""
;;
esac
####################################################################################################
#
# Functions
#
####################################################################################################
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Client-side Script Logging
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
function updateScriptLog() {
echo -e "$( date +%Y-%m-%d\ %H:%M:%S ) - ${1}" | tee -a "${scriptLog}"
}
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Run command as logged-in user (thanks, @scriptingosx!)
# shellcheck disable=SC2145
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
function runAsUser() {
updateScriptLog "Run \"$@\" as \"$loggedInUserID\" … "
launchctl asuser "$loggedInUserID" sudo -u "$loggedInUser" "$@"
}
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Check for / install swiftDialog (Thanks big bunches, @acodega!)
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
function dialogCheck() {
# Get the URL of the latest PKG From the Dialog GitHub repo
dialogURL=$(curl --silent --fail "https://api.github.com/repos/bartreardon/swiftDialog/releases/latest" | awk -F '"' "/browser_download_url/ && /pkg\"/ { print \$4; exit }")
# Expected Team ID of the downloaded PKG
expectedDialogTeamID="PWA5E9TQ59"
# Check for Dialog and install if not found
if [ ! -e "/Library/Application Support/Dialog/Dialog.app" ]; then
updateScriptLog "Dialog not found. Installing..."
# Create temporary working directory
workDirectory=$( /usr/bin/basename "$0" )
tempDirectory=$( /usr/bin/mktemp -d "/private/tmp/$workDirectory.XXXXXX" )
# Download the installer package
/usr/bin/curl --location --silent "$dialogURL" -o "$tempDirectory/Dialog.pkg"
# Verify the download
teamID=$(/usr/sbin/spctl -a -vv -t install "$tempDirectory/Dialog.pkg" 2>&1 | awk '/origin=/ {print $NF }' | tr -d '()')
# Install the package if Team ID validates
if [[ "$expectedDialogTeamID" == "$teamID" ]]; then
/usr/sbin/installer -pkg "$tempDirectory/Dialog.pkg" -target /
sleep 2
updateScriptLog "swiftDialog version $(dialog --version) installed; proceeding..."
else
# Display a so-called "simple" dialog if Team ID fails to validate
runAsUser osascript -e 'display dialog "Please advise your Support Representative of the following error:\r\r• Dialog Team ID verification failed\r\r" with title "Setup Your Mac: Error" buttons {"Close"} with icon caution'
completionActionOption="Quit"
exitCode="1"
quitScript
fi
# Remove the temporary working directory when done
/bin/rm -Rf "$tempDirectory"
else
updateScriptLog "swiftDialog version $(dialog --version) found; proceeding..."
fi
}
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Update the "Welcome" dialog
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
function dialogUpdateWelcome(){
updateScriptLog "WELCOME DIALOG: $1"
echo "$1" >> "$welcomeCommandFile"
}
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Update the "Setup Your Mac" dialog
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
function dialogUpdateSetupYourMac() {
updateScriptLog "SETUP YOUR MAC DIALOG: $1"
echo "$1" >> "$setupYourMacCommandFile"
}
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Update the "Failure" dialog
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
function dialogUpdateFailure(){
updateScriptLog "FAILURE DIALOG: $1"
echo "$1" >> "$failureCommandFile"
}
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Finalise User Experience
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
function finalise(){
if [[ "${jamfProPolicyTriggerFailure}" == "failed" ]]; then
killProcess "caffeinate"
updateScriptLog "Jamf Pro Policy Name Failures: ${jamfProPolicyPolicyNameFailures}"
dialogUpdateSetupYourMac "title: Sorry ${loggedInUserFirstname}, something went sideways"
dialogUpdateSetupYourMac "icon: SF=xmark.circle.fill,weight=bold,colour1=#BB1717,colour2=#F31F1F"
dialogUpdateSetupYourMac "progresstext: Failures detected. Please click Continue for troubleshooting information."
dialogUpdateSetupYourMac "button1text: Continue …"
dialogUpdateSetupYourMac "button1: enable"
dialogUpdateSetupYourMac "progress: complete"
# Wait for user-acknowledgment due to detected failure
wait
dialogUpdateSetupYourMac "quit:"
eval "${dialogFailureCMD}" & sleep 0.3
dialogUpdateFailure "message: A failure has been detected, ${loggedInUserFirstname}. \n\nPlease complete the following steps:\n1. Reboot and login to your Mac \n2. Login to Self Service \n3. Re-run any failed policy listed below \n\nThe following failed to install: \n${jamfProPolicyPolicyNameFailures} \n\n\n\nIf you need assistance, please contact the Help Desk, \n+1 (801) 555-1212, and mention [KB86753099](https://servicenow.company.com/support?id=kb_article_view&sysparm_article=KB86753099#Failures). "
dialogUpdateFailure "icon: SF=xmark.circle.fill,weight=bold,colour1=#BB1717,colour2=#F31F1F"
dialogUpdateFailure "button1text: ${button1textCompletionActionOption}"
# Wait for user-acknowledgment due to detected failure
wait
dialogUpdateFailure "quit:"
quitScript "1"
else
dialogUpdateSetupYourMac "title: ${loggedInUserFirstname}'s Mac is ready!"
dialogUpdateSetupYourMac "icon: SF=checkmark.circle.fill,weight=bold,colour1=#00ff44,colour2=#075c1e"
dialogUpdateSetupYourMac "progresstext: Complete! Please ${progressTextCompletionAction}enjoy your new Mac, ${loggedInUserFirstname}!"
dialogUpdateSetupYourMac "progress: complete"
dialogUpdateSetupYourMac "button1text: ${button1textCompletionActionOption}"
dialogUpdateSetupYourMac "button1: enable"
# If either "wait" or "sleep" has been specified for `completionActionOption`, honor that behavior
if [[ "${completionActionOption}" == "wait" ]] || [[ "${completionActionOption}" == "[Ss]leep"* ]]; then
updateScriptLog "Honoring ${completionActionOption} behavior …"
eval "${completionActionOption}" "${dialogSetupYourMacProcessID}"
fi
quitScript "0"
fi
}
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Parse JSON via osascript and JavaScript
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
function get_json_value() {
JSON="$1" osascript -l 'JavaScript' \
-e 'const env = $.NSProcessInfo.processInfo.environment.objectForKey("JSON").js' \
-e "JSON.parse(env).$2"
}
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Parse JSON via osascript and JavaScript for the Welcome dialog (thanks, @bartreardon!)
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
function get_json_value_welcomeDialog () {
for var in "${@:2}"; do jsonkey="${jsonkey}['${var}']"; done
JSON="$1" osascript -l 'JavaScript' \
-e 'const env = $.NSProcessInfo.processInfo.environment.objectForKey("JSON").js' \
-e "JSON.parse(env)$jsonkey"
}
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Execute Jamf Pro Policy Custom Events (thanks, @smithjw)
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
function run_jamf_trigger() {
trigger="$1"
if [[ "${debugMode}" == "true" ]]; then
updateScriptLog "SETUP YOUR MAC DIALOG: DEBUG MODE: TRIGGER: $jamfBinary policy -event $trigger"
if [[ "$trigger" == "recon" ]]; then
updateScriptLog "SETUP YOUR MAC DIALOG: DEBUG MODE: RECON: $jamfBinary recon ${reconOptions}"
fi
sleep 1
elif [[ "$trigger" == "recon" ]]; then
dialogUpdateSetupYourMac "listitem: index: $i, status: wait, statustext: Updating …, "
updateScriptLog "SETUP YOUR MAC DIALOG: Updating computer inventory with the following reconOptions: \"${reconOptions}\" …"
eval "${jamfBinary} recon ${reconOptions}"
else
updateScriptLog "SETUP YOUR MAC DIALOG: RUNNING: $jamfBinary policy -event $trigger"
"$jamfBinary" policy -event "$trigger"
fi
}
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Kill a specified process (thanks, @grahampugh!)
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
function killProcess() {
process="$1"
if process_pid=$( pgrep -a "${process}" 2>/dev/null ) ; then
updateScriptLog "Attempting to terminate the '$process' process …"
updateScriptLog "(Termination message indicates success.)"
kill "$process_pid" 2> /dev/null
if pgrep -a "$process" >/dev/null ; then
updateScriptLog "ERROR: '$process' could not be terminated."
fi
else
updateScriptLog "The '$process' process isn't running."
fi
}
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Completion Action (i.e., Wait, Sleep, Logout, Restart or Shutdown)
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
function completionAction() {
if [[ "${debugMode}" == "true" ]]; then
# If Debug Mode is enabled, ignore specified `completionActionOption`, display simple dialog box and exit
runAsUser osascript -e 'display dialog "Setup Your Mac is operating in Debug Mode.\r\r• completionActionOption == '"'${completionActionOption}'"'\r\r" with title "Setup Your Mac: Debug Mode" buttons {"Close"} with icon note'
exitCode="0"
else
shopt -s nocasematch
case ${completionActionOption} in
"Shut Down" )
updateScriptLog "Shut Down sans user interaction"
killProcess "Self Service"
# runAsUser osascript -e 'tell app "System Events" to shut down'
sleep 5 && runAsUser osascript -e 'tell app "System Events" to shut down' &
# shutdown -h +1 &
;;
"Shut Down Attended" )
updateScriptLog "Shut Down, requiring user-interaction"
killProcess "Self Service"
wait
# runAsUser osascript -e 'tell app "System Events" to shut down'
sleep 5 && runAsUser osascript -e 'tell app "System Events" to shut down' &
# shutdown -h +1 &
;;
"Shut Down Confirm" )
updateScriptLog "Shut down, only after macOS time-out or user confirmation"
runAsUser osascript -e 'tell app "loginwindow" to «event aevtrsdn»'
;;
"Restart" )
updateScriptLog "Restart sans user interaction"
killProcess "Self Service"
# runAsUser osascript -e 'tell app "System Events" to restart'
sleep 5 && runAsUser osascript -e 'tell app "System Events" to restart' &
# shutdown -r +1 &
;;
"Restart Attended" )
updateScriptLog "Restart, requiring user-interaction"
killProcess "Self Service"
wait
# runAsUser osascript -e 'tell app "System Events" to restart'
sleep 5 && runAsUser osascript -e 'tell app "System Events" to restart' &
# shutdown -r +1 &
;;
"Restart Confirm" )
updateScriptLog "Restart, only after macOS time-out or user confirmation"
runAsUser osascript -e 'tell app "loginwindow" to «event aevtrrst»'
;;
"Log Out" )
updateScriptLog "Log out sans user interaction"
killProcess "Self Service"
# runAsUser osascript -e 'tell app "loginwindow" to «event aevtrlgo»'
sleep 5 && runAsUser osascript -e 'tell app "loginwindow" to «event aevtrlgo»' &
# launchctl bootout user/"${loggedInUserID}"
;;
"Log Out Attended" )
updateScriptLog "Log out sans user interaction"
killProcess "Self Service"
wait
# runAsUser osascript -e 'tell app "loginwindow" to «event aevtrlgo»'
sleep 5 && runAsUser osascript -e 'tell app "loginwindow" to «event aevtrlgo»' &
# launchctl bootout user/"${loggedInUserID}"
;;
"Log Out Confirm" )
updateScriptLog "Log out, only after macOS time-out or user confirmation"
runAsUser osascript -e 'tell app "System Events" to log out'
;;
"Sleep"* )
sleepDuration=$( awk '{print $NF}' <<< "${1}" )
updateScriptLog "Sleeping for ${sleepDuration} seconds …"
sleep "${sleepDuration}"
killProcess "Dialog"
updateScriptLog "Goodnight!"
;;
"Quit" )
updateScriptLog "Quitting script"
exitCode="0"
;;
* )
updateScriptLog "Using the default of 'wait'"
wait
;;
esac
shopt -u nocasematch
fi
exit "${exitCode}"
}
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Quit Script (thanks, @bartreadon!)
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
function quitScript() {
updateScriptLog "Exiting …"
# Stop `caffeinate` process
updateScriptLog "De-caffeinate …"
killProcess "caffeinate"
# Remove welcomeCommandFile
if [[ -e ${welcomeCommandFile} ]]; then
updateScriptLog "Removing ${welcomeCommandFile} …"
rm "${welcomeCommandFile}"
fi
# Remove setupYourMacCommandFile
if [[ -e ${setupYourMacCommandFile} ]]; then
updateScriptLog "Removing ${setupYourMacCommandFile} …"
rm "${setupYourMacCommandFile}"
fi
# Remove failureCommandFile
if [[ -e ${failureCommandFile} ]]; then
updateScriptLog "Removing ${failureCommandFile} …"
rm "${failureCommandFile}"
fi
# Remove any default dialog file
if [[ -e /var/tmp/dialog.log ]]; then
updateScriptLog "Removing default dialog file …"
rm /var/tmp/dialog.log
fi
# Check for user clicking "Quit" at Welcome dialog
if [[ "${welcomeReturnCode}" == "2" ]]; then
exitCode="1"
exit "${exitCode}"
else
updateScriptLog "Executing Completion Action Option: '${completionActionOption}' …"
completionAction "${completionActionOption}"
fi
}
####################################################################################################
#
# Pre-flight Checks
#
####################################################################################################
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Confirm script is running as root
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
if [[ $(id -u) -ne 0 ]]; then
echo "This script must be run as root; exiting."
exit 1
fi
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Client-side Logging
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
if [[ ! -f "${scriptLog}" ]]; then
touch "${scriptLog}"
updateScriptLog "*** Created log file via script ***"
fi
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Validate logged-in user
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
if [[ -z "${loggedInUser}" || "${loggedInUser}" == "loginwindow" ]]; then
echo "No user logged-in; exiting."
exitCode="1"
quitScript
else
loggedInUserID=$(id -u "${loggedInUser}")
fi
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Logging preamble
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
if [[ "${debugMode}" == "true" ]]; then
updateScriptLog "\n\n###\n# ${scriptVersion}\n###\n"
else
updateScriptLog "\n\n###\n# Setup Your Mac (${scriptVersion})\n###\n"
fi
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Confirm Dock is running and user at Desktop
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
dockStatus=$( pgrep -x Dock )
updateScriptLog "Waiting for Desktop …"
while [[ "$dockStatus" == "" ]]; do
updateScriptLog "Desktop is not loaded; waiting 5 seconds …"
sleep 5
dockStatus=$( pgrep -x Dock )
done
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Validate swiftDialog is installed
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
dialogCheck
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Ensure computer does not go to sleep while running this script (thanks, @grahampugh!)
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
updateScriptLog "Caffeinating this script (pid=$$)"
caffeinate -dimsu -w $$ &
####################################################################################################
#
# Program
#
####################################################################################################
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# If Debug Mode is enabled, replace `blurscreen` with `movable`
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
if [[ "${debugMode}" == "true" ]]; then
welcomeJSON=${welcomeJSON//blurscreen/moveable}
dialogSetupYourMacCMD=${dialogSetupYourMacCMD//blurscreen/moveable}
fi
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Write Welcome JSON to disk
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
echo "$welcomeJSON" > "$welcomeCommandFile"
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Display Welcome dialog and capture user's input
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
if [[ "${welcomeDialog}" == "true" ]]; then
welcomeResults=$( ${dialogApp} --jsonfile "$welcomeCommandFile" --json )
if [[ -z "${welcomeResults}" ]]; then
welcomeReturnCode="2"
else
welcomeReturnCode="0"
fi