forked from dan-snelson/dialog-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Prompt-to-Setup-Your-Mac.bash
542 lines (358 loc) · 17.1 KB
/
Prompt-to-Setup-Your-Mac.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
#!/bin/bash
#################################################################################
#
# Prompt to Setup Your Mac
#
# Purpose: Prompts users to login to Self Service / run the Setup Your Mac policy
#
# Reference: https://snelson.us/2022/07/setup-your-mac-please/
#
#################################################################################
#
# HISTORY
#
# Version 0.0.1, 22-Jul-2022, Dan K. Snelson (@dan-snelson)
# Original version
#
# Version 0.0.2, 22-Jul-2022, Dan K. Snelson (@dan-snelson)
# Added script execution delay (Parameter 4)
#
# Version 0.0.3, 28-Jul-2022, Dan K. Snelson (@dan-snelson)
# Exit if odd-ball user is logged-in
# Added "--ignorednd" and "--blurscreen" to Dialog command
#
#################################################################################
#################################################################################
#
# Environmental Checks
#
#################################################################################
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Exit gracefully if odd-ball users are logged in
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
loggedInUser=$( /bin/echo "show State:/Users/ConsoleUser" | /usr/sbin/scutil | /usr/bin/awk '/Name :/ { print $3 }' )
if [[ -z "${loggedInUser}" ]] \
|| [[ ${loggedInUser} == "loginwindow" ]] \
|| [[ ${loggedInUser} == "_mbsetupuser" ]] ; then
echo "Odd-ball user \"${loggedInUser}\" logged in; exiting."
exit 0
fi
#################################################################################
#
# Variables
#
#################################################################################
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Global variables
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
scriptVersion="0.0.3"
scriptResult="Prompt to Setup Your Mac (${scriptVersion}); "
jamfProPolicyName="@Setup Your Mac"
plistPath="/Library/Preferences/com.company.plist"
plistKey="Setup Your Mac"
selfServiceAppPath=$( /usr/bin/defaults read /Library/Preferences/com.jamfsoftware.jamf.plist self_service_app_path )
dialogApp="/usr/local/bin/dialog"
dialogCommandFile=$( /usr/bin/mktemp "/var/tmp/Prompt-to-Setup-Your-Mac.XXXXXXX" )
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Check for a specified "secondsToWait" setting (Parameter 4); defaults to "2700"
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
if [[ "${4}" != "" ]] && [[ "${secondsToWait}" == "" ]]; then
secondsToWait="${4}"
scriptResult+="Using ${secondsToWait} as the number of seconds before script execution; "
else
secondsToWait="2700"
scriptResult+="Parameter 4 is blank; using ${secondsToWait} as the number of seconds before script execution; "
fi
#################################################################################
#
# Functions
#
#################################################################################
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Prompt user to execute the Self Service policy via swiftDialog
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
function promptUser() {
scriptResult+="Prompting user to execute the \"${jamfProPolicyName}\" policy; "
dialogCheck
title="Welcome to your new Mac!"
message="Please complete the following steps to apply Church settings to your new Mac: \n1. Login to the **Workforce App Store** \n2. Locate the **Setup Your Mac** policy \n3. Click **Setup** \n\nIf you need assistance, please contact the GSD: \n+1 (801) 555-1212 and mention **KB12345678**."
appleInterfaceStyle=$( /usr/bin/defaults read /Users/"${loggedInUser}"/Library/Preferences/.GlobalPreferences.plist AppleInterfaceStyle 2>&1 )
if [[ "${appleInterfaceStyle}" == "Dark" ]]; then
icon="/System/Library/CoreServices/Finder.app"
else
icon="/System/Library/CoreServices/Finder.app"
fi
overlayicon=$( defaults read /Library/Preferences/com.jamfsoftware.jamf.plist self_service_app_path )
dialogCMD="$dialogApp --ontop --title \"$title\" \
--message \"$message\" \
--icon \"$icon\" \
--button1text \"OK\" \
--overlayicon \"$overlayicon\" \
--titlefont 'size=28' \
--messagefont 'size=14' \
--infobuttontext 'KB12345678' \
--infobuttonaction 'https://servicenow.company.com/support?id=kb_article_view&sysparm_article=KB12345678' \
--small \
--moveable \
--timer 120 \
--position 'centre' \
--ignorednd \
--blurscreen \
--commandfile \"$dialogCommandFile\" "
eval "$dialogCMD"
returnCode=$?
case ${returnCode} in
0) scriptResult+="${loggedInUser} clicked OK; "
;;
2) scriptResult+="${loggedInUser} clicked Button2; "
;;
3) scriptResult+="${loggedInUser} clicked KB12345678; "
;;
4) scriptResult+="${loggedInUser} allowed timer to expire; "
;;
*) scriptResult+="Something else happened; swiftDialog Return Code: ${returnCode}; "
;;
esac
/bin/rm -f "$dialogCommandFile"
}
#--------------------- Edits below this line are optional ---------------------#
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Check for / install swiftDialog (thanks, Adam!)
# https://github.com/acodega/dialog-scripts/blob/main/dialogCheckFunction.sh
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
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
echo "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" ] || [ "$expectedDialogTeamID" = "" ]; then
/usr/sbin/installer -pkg "$tempDirectory/Dialog.pkg" -target /
else
jamfDisplayMessage "Dialog Team ID verification failed."
exit 1
fi
# Remove the temporary working directory when done
/bin/rm -Rf "$tempDirectory"
else
scriptResult+="Dialog v$(dialog --version) installed, proceeding; "
fi
}
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Display Message via the jamf binary
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
function jamfDisplayMessage() {
scriptResult+="${1}; "
/usr/local/jamf/bin/jamf displayMessage -message "${1}" &
}
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Gracefully exit for new enrollments
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
function gracefullyExitForNewEnrollments() {
testFile="/var/db/.AppleSetupDone"
testFileSeconds=$( /bin/date -j -f "%s" "$(/usr/bin/stat -f "%m" $testFile)" +"%s" )
nowSeconds=$( /bin/date +"%s" )
ageInSeconds=$((nowSeconds-testFileSeconds))
secondsToWaitHumanReadable=$( printf '"%dd, %dh, %dm, %ds"\n' $((secondsToWait/86400)) $((secondsToWait%86400/3600)) $((secondsToWait%3600/60)) $((secondsToWait%60)) )
ageInSecondsHumanReadable=$( printf '"%dd, %dh, %dm, %ds"\n' $((ageInSeconds/86400)) $((ageInSeconds%86400/3600)) $((ageInSeconds%3600/60)) $((ageInSeconds%60)) )
if [[ ${ageInSeconds} -le ${secondsToWait} ]]; then
scriptResult+="Set to wait ${secondsToWaitHumanReadable} and enrollment was ${ageInSecondsHumanReadable} ago; exiting."
echo "${scriptResult}"
exit 0
else
scriptResult+="Set to wait ${secondsToWaitHumanReadable} and enrollment was ${ageInSecondsHumanReadable} ago, proceeding; "
fi
}
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Validate Self Service installation
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
function selfServiceValidation() {
if [[ -d "${selfServiceAppPath}" ]]; then
selfServiceInstalled="Yes"
else
selfServiceInstalled="No"
fi
}
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Validate Logged-in User's Self Service Log
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
function selfServiceLogValidation() {
if [[ -f "/Users/${loggedInUser}/Library/Logs/JAMF/selfservice.log" ]]; then
selfServiceLogInstalled="Yes"
else
selfServiceLogInstalled="No"
fi
}
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Validate the number of times Self Service has been launched
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
function selfServiceLaunchValidation() {
if [[ ${selfServiceLogInstalled} == "Yes" ]]; then
selfServiceLaunches=$( /usr/bin/grep "Application successfully launched" /Users/"${loggedInUser}"/Library/Logs/JAMF/selfservice.log | /usr/bin/wc -l | /usr/bin/tr -d ' ' )
else
scriptResult+="Something went sideways when checking the number of times \"${selfServiceAppPath}\" has been launched; exiting."
echo "${scriptResult}"
exit 1
fi
}
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Validate the number of times the user has logged into Self Service
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
function selfServiceLoginValidation() {
if [[ ${selfServiceLogInstalled} == "Yes" ]]; then
selfServiceLogins=$( /usr/bin/grep "user logged in" /Users/"${loggedInUser}"/Library/Logs/JAMF/selfservice.log | /usr/bin/wc -l | /usr/bin/tr -d ' ' )
else
scriptResult+="Something went sideways when checking the number of times ${loggedInUser} has logged in to \"${selfServiceAppPath}\"; exiting."
echo "${scriptResult}"
exit 1
fi
}
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Validate Jamf's log
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
function jamfLogValidation() {
if [[ -f "/private/var/log/jamf.log" ]]; then
jamfLogInstalled="Yes"
else
jamfLogInstalled="No"
fi
}
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Validate Self Service Policy execution
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
function selfServicePolicyValidation() {
if [[ ${jamfLogInstalled} == "Yes" ]]; then
selfServicePolicyExecutions=$( /usr/bin/grep "${jamfProPolicyName}" /private/var/log/jamf.log | /usr/bin/wc -l | /usr/bin/tr -d ' ' )
else
scriptResult+="Something went sideways when checking the number of times ${loggedInUser} has executed the \"${jamfProPolicyName}\" policy; exiting."
echo "${scriptResult}"
exit 1
fi
}
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Validate Self Service Policy completion
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
function selfServicePolicyCompletionValidation() {
plistValue=$( /usr/bin/defaults read "${plistPath}" "${plistKey}" 2>&1 )
case "${plistValue}" in
*"does not exist" ) selfServicePolicyCompletion="No" ;;
* ) selfServicePolicyCompletion="Yes" ;;
esac
}
#################################################################################
#
# Program
#
#################################################################################
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Check 1. Gracefully exit for new enrollments
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
gracefullyExitForNewEnrollments
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Check 2. Validate Self Service installation
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
selfServiceValidation
if [[ ${selfServiceInstalled} == "No" ]]; then
scriptResult+="\"${selfServiceAppPath}\" was NOT installed; attempting re-installation; "
/usr/local/bin/jamf update -verbose
scriptResult+="Updating inventory; "
/usr/local/bin/jamf recon
scriptResult+="Exiting with error."
echo "${scriptResult}"
exit 1
else
scriptResult+="\"${selfServiceAppPath}\" is installed, proceeding; "
fi
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Check 3. Validate Logged-in User's Self Service Log
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
selfServiceLogValidation
if [[ ${selfServiceLogInstalled} == "No" ]]; then
scriptResult+="${loggedInUser}'s selfservice.log NOT found, launching \"${selfServiceAppPath}\"; "
/usr/bin/su "${loggedInUser}" -c "/usr/bin/open \"${selfServiceAppPath}\""
promptUser
echo "${scriptResult}"
exit 1
else
scriptResult+="${loggedInUser}'s selfservice.log found, proceeding; "
fi
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Check 4. Validate the number of times Self Service has been launched
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
selfServiceLaunchValidation
scriptResult+="\"${selfServiceAppPath}\" has been launched for ${loggedInUser} ${selfServiceLaunches} times; "
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Check 5. Validate the number of times the user has logged into Self Service
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
selfServiceLoginValidation
if [[ ${selfServiceLogins} -le "0" ]]; then
scriptResult+="${loggedInUser} has logged in to \"${selfServiceAppPath}\" ${selfServiceLogins} times; "
/usr/bin/su "${loggedInUser}" -c "/usr/bin/open \"${selfServiceAppPath}\""
promptUser
echo "${scriptResult}"
exit 0
else
scriptResult+="${loggedInUser} has logged in to \"${selfServiceAppPath}\" ${selfServiceLogins} times; "
fi
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Check 6. Validate Jamf's log
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
jamfLogValidation
if [[ ${jamfLogInstalled} == "No" ]]; then
scriptResult+="jamf.log NOT found, attempting to re-manage; "
/usr/local/bin/jamf manage -verbose
scriptResult+="Updating inventory; "
/usr/local/bin/jamf recon
scriptResult+="Exiting with error."
echo "${scriptResult}"
exit 1
else
scriptResult+="jamf.log found, proceeding; "
fi
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Check 7. Validate Self Service Policy execution
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
selfServicePolicyValidation
if [[ ${selfServicePolicyExecutions} -le "0" ]]; then
scriptResult+="${loggedInUser} has started the \"${jamfProPolicyName}\" policy ${selfServicePolicyExecutions} times; "
/usr/bin/su "${loggedInUser}" -c "/usr/bin/open \"${selfServiceAppPath}\""
promptUser
echo "${scriptResult}"
exit 0
else
scriptResult+="${loggedInUser} has started the \"${jamfProPolicyName}\" policy ${selfServicePolicyExecutions} times; "
fi
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Check 8. Validate Self Service Policy completion
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
selfServicePolicyCompletionValidation
if [[ ${selfServicePolicyCompletion} == "No" ]]; then
scriptResult+="The \"${jamfProPolicyName}\" policy has NOT completed; launching \"${selfServiceAppPath}\"; "
/usr/bin/su "${loggedInUser}" -c "/usr/bin/open \"${selfServiceAppPath}\""
promptUser
echo "${scriptResult}"
exit 0
else
scriptResult+="Success! The \"${jamfProPolicyName}\" policy has completed; "
scriptResult+="Updating inventory; "
/usr/local/bin/jamf recon
scriptResult+="Goodbye!"
echo "${scriptResult}"
exit 0
fi
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Exit
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
scriptResult+="Catch-all exit; thank you!"
echo "${scriptResult}"
exit 0