-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPromptWithSnooze.sh
297 lines (239 loc) · 11.5 KB
/
PromptWithSnooze.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
#!/bin/bash
####################################################################################################
#
# Copyright (c) 2013, JAMF Software, LLC. All rights reserved.
#
# This script was written by the JAMF Software Profesional Services Team
#
# THIS SOFTWARE IS PROVIDED BY JAMF SOFTWARE, LLC "AS IS" AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL JAMF SOFTWARE, LLC BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
#####################################################################################################
#
# SUPPORT FOR THIS PROGRAM
#
# This program is distributed "as is" by JAMF Software, Professional Services Team. For more
# information or support for this script, please contact your JAMF Software Account Manager.
#
#####################################################################################################
#
# ABOUT THIS PROGRAM
#
# NAME
# jamfHelperByPolicy.sh
#
# SYNOPSIS - How to use
# Run via a policy to populate JAMF Helper with values to present messages to the user.
#
# DESCRIPTION
#
# Populate script parameters to match the variables below.
# Pass in values into these parameters during a policy.
#
####################################################################################################
#
# HISTORY
#
# Version: 1.0
#
# - Created by Douglas Worley, Professional Services Engineer, JAMF Software on May 10, 2013
#
# Version: 1.5
#
# - Modified by Matthew Durante & Michael Lawson. Nov 23, 2022
#
# -
#
####################################################################################################
# The recursively named JAMF Helper help file is accessible at:
# /Library/Application\ Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper -help
##Try to get device uptime
dayCount=$( uptime | awk -F "(up | days)" '{ print $2 }' )
if ! [ "$dayCount" -eq "$dayCount" ] 2> /dev/null ; then
dayCount="0"
fi
##Get Logged in user
LoggedInUser=$(who | awk '/console/{print $1}')
#currentUser=$(osascript -e "long user name of (system info)")
#Jamf Variable currentUser=$(ls -l /dev/console | awk '{ print $3}')
#echo $currentUser
windowType="utility" # [hud | utility | fs]
#windowPosition="" # [ul | ll | ur | lr]
title="Computer Health Check" # "string"
heading="Your computer requires a restart" # "string" macOS Monterey Upgrade
description="$LoggedInUser Your computer has not been restarted in over $dayCount days. Please restart your machine."
icon="/Library/<img.png>" # path
#iconSize="" # pixels
#timeout="" # seconds 120
[ "$4" != "" ] && [ "$windowType" == "" ] && windowType=$4
#[ "$5" != "" ] && [ "$windowPosition" == "" ] && windowPosition=$5
[ "$6" != "" ] && [ "$title" == "" ] && title=$6
[ "$7" != "" ] && [ "$heading" == "" ] && heading=$7
[ "$8" != "" ] && [ "$description" == "" ] && description=$8
#[ "$9" != "" ] && [ "$icon" == "" ] && icon=$9
#[ "${10}" != "" ] && [ "$iconSize" == "" ] && iconSize=$10
#[ "${11}" != "" ] && [ "$timeout" == "" ] && timeout=${11}
function setSnooze ()
{
#Get the time right now in Unix seconds
timeNow=$(date +"%s")
echo "$timeNow"
#Calculate the time for the next available display of the prompt (adds the time now and time chosen together in seconds)
timeNextRun=$((timeNow+SnoozeVal))
echo "$timeNextRun"
## Create or update a plist value containing the above next time to run value
/usr/bin/defaults write /Library/Preferences/com.acme.policy_001.snooze.plist DelayUntil -integer $timeNextRun
exit 0
}
function showPrompt1 ()
{
## Prompt, and capture the output
HELPER=$("/Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfhelper" -windowType "$windowType" -title "$title" -heading "$heading" -description "$description" -icon "$icon" -button1 "Restart" -button2 "Snooze" -cancelButton "2" -countdown "$timeout" -timeout "1800" -showDelayOptions "1800, 2700")
##OG
#HELPER=$("/Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfhelper" -windowType "$windowType" -windowPosition "$windowPosition" -title "$title" -heading "$heading" -description "$description" -icon "$icon" -iconSize "$iconSize" -button1 Install Now -button2 Snooze -defaultButton 2 -cancelButton "2" -countdown "$timeout" -timeout "$timeout" -showDelayOptions "900, 1800, 3600, 7200, 14400")
echo "jamf helper result was $HELPER"
## Dissect the response to get just the button clicked and the value selected from the drop down menu
ButtonClicked="${HELPER: -1}"
SnoozeVal="${HELPER%?}"
echo "$ButtonClicked"
echo "$SnoozeVal"
if [ "$ButtonClicked" == "1" ]; then
echo "User chose restart"
/usr/local/bin/jamf recon -verbose
/usr/local/bin/jamf reboot -minutes 1 -message "Your device will restart in 1 minute" -startTimerImmediately
elif [ "$ButtonClicked" == "2" ]; then
echo "User chose Snooze"
touch /tmp/1
setSnooze
fi
}
function showPrompt2 ()
{
## Prompt, and capture the output
HELPER=$("/Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfhelper" -windowType "$windowType" -title "$title" -heading "$heading" -description "$description" -icon "$icon" -button1 "Restart" -button2 "Snooze" -cancelButton "2" -countdown "$timeout" -timeout "1800" -showDelayOptions "1800, 2700")
##OG
#HELPER=$("/Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfhelper" -windowType "$windowType" -windowPosition "$windowPosition" -title "$title" -heading "$heading" -description "$description" -icon "$icon" -iconSize "$iconSize" -button1 Install Now†-button2 Snooze -defaultButton 2 -cancelButton "2" -countdown "$timeout" -timeout "$timeout" -showDelayOptions "900, 1800, 3600, 7200, 14400")
echo "jamf helper result was $HELPER"
## Dissect the response to get just the button clicked and the value selected from the drop down menu
ButtonClicked="${HELPER: -1}"
SnoozeVal="${HELPER%?}"
echo "$ButtonClicked"
echo "$SnoozeVal"
if [ "$ButtonClicked" == "1" ]; then
echo "User chose Restart"
/usr/local/bin/jamf recon -verbose
/usr/local/bin/jamf reboot -minutes 1 -message "Your device will restart in 1 minute" -startTimerImmediately
elif [ "$ButtonClicked" == "2" ]; then
echo "User chose Snooze twice"
touch /tmp/2
setSnooze
fi
}
function showPrompt3 ()
{
## Prompt, and capture the output
HELPER=$("/Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfhelper" -windowType "$windowType" -title "$title" -heading "$heading" -description "$description" -icon "$icon" -button1 "Restart" -button2 "Snooze" -cancelButton "2" -countdown "$timeout" -timeout "1800" -showDelayOptions "1800, 2700")
##OG
#HELPER=$("/Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfhelper" -windowType "$windowType" -windowPosition "$windowPosition" -title "$title" -heading "$heading" -description "$description" -icon "$icon" -iconSize "$iconSize" -button1 Install Now†-button2 Snooze -defaultButton 2 -cancelButton "2" -countdown "$timeout" -timeout "$timeout" -showDelayOptions "900, 1800, 3600, 7200, 14400")
echo "jamf helper result was $HELPER"
## Dissect the response to get just the button clicked and the value selected from the drop down menu
ButtonClicked="${HELPER: -1}"
SnoozeVal="${HELPER%?}"
echo "$ButtonClicked"
echo "$SnoozeVal"
if [ "$ButtonClicked" == "1" ]; then
echo "User chose Restart"
/usr/local/bin/jamf recon -verbose
/usr/local/bin/jamf reboot -minutes 1 -message "Your device will restart in 1 minute" -startTimerImmediately
elif [ "$ButtonClicked" == "2" ]; then
echo "User Chose snooze - last run"
touch /tmp/3
setSnooze
fi
}
function showPrompt4 ()
{
## Prompt, and capture the output
HELPER=$("/Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfhelper" -windowType "$windowType" -title "$title" -heading "Device restart will be enforced" -description "Your machine will restart in a couple minutes. Please save all of your work and plug device into power now." -icon "/Library/Northern/NT_Logo.png" -button1 "Restart" -countdown -timeout "60" -defaultButton "2" ) #-button2 "Snooze" -countdown "$timeout" -timeout "$timeout" -showDelayOptions "90, 180, 3600, 7200, 14400")
##OG
#HELPER=$("/Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfhelper" -windowType "$windowType" -windowPosition "$windowPosition" -title "$title" -heading "$heading" -description "$description" -icon "$icon" -iconSize "$iconSize" -button1 Install Now†-button2 Snooze -defaultButton 2 -cancelButton "2" -countdown "$timeout" -timeout "$timeout" -showDelayOptions "900, 1800, 3600, 7200, 14400")
echo "jamf helper result was $HELPER"
## Dissect the response to get just the button clicked and the value selected from the drop down menu
ButtonClicked="${HELPER: -1}"
SnoozeVal="${HELPER%?}"
echo "$ButtonClicked"
echo "$SnoozeVal"
###Buttons not used here- just clean up after yourself!
if [ "$ButtonClicked" == "1" ]; then
echo "Device is being restarted"
#/usr/local/bin/jamf recon -verbose
/usr/local/bin/jamf reboot -message "Your device will restart in a moment" -immediately
rm -R /tmp/1
rm -R /tmp/2
rm -R /tmp/3
elif [ "$ButtonClicked" == "2" ]; then
echo "Device is being restarted"
#/usr/local/bin/jamf recon -verbose
/usr/local/bin/jamf reboot -message "Your device will restart in a moment" -immediately
rm -R /tmp/1
rm -R /tmp/2
rm -R /tmp/3
setSnooze
fi
}
function FunctionCallTimer ()
{
##Check if the Prompt 1 has been run and tagged
if [ -e "/tmp/1" ]; then
echo "User Snoozed Once"
else
## If none of the txt files exist we'll assume the policy has never started
echo "Process not started yet, call restart script"
showPrompt1
fi
echo "Check for Flag 2"
##Check for Flag 2
if [ -e "/tmp/2" ]; then
echo "User Snoozed twice"
else
echo "Missing Tag 2 calling Prompt 2"
showPrompt2
fi
##Enforce this OS upgrade!
echo "Check for Flag 3"
if [ -e "/tmp/3" ]; then
echo "Flag 3 present, enforcing restart"
showPrompt4
else
echo "Calling final prompt missing Flag 3"
showPrompt3
fi
}
## Get a value (if possible) from a plist of the next valid time we can prompt the user
SnoozeValueSet=$(/usr/bin/defaults read /Library/Preferences/com.acme.policy_001.snooze.plist DelayUntil 2>/dev/null)
## If we got something from the plist...
if [ -n "$SnoozeValueSet" ]; then
## See what time it is now, and compare it to the value in the plist.
## If the time now is greater or equal to the value in the plist, enough time has elapsed, so...
timeNow=$(date +"%s")
if [[ "$timeNow" -ge "$SnoozeValueSet" ]]; then
## Display the prompt to the user again
FunctionCallTimer
else
## If the time now is less than the value in the plist, exit
echo "Not enough time has elapsed. Exiting..."
exit 0
fi
else
## If no value was in the plist or the plist wasn't there, assume it is the first run of the script and prompt them
#getdeviceUpTime
FunctionCallTimer
fi