-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimer.ps1
More file actions
331 lines (281 loc) · 10.1 KB
/
Timer.ps1
File metadata and controls
331 lines (281 loc) · 10.1 KB
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
# Simple PowerShell Timer
# Originally written by Ivy Slick (@queery420) 6/2/21
# Adapted from @avredd's sitstand.sh
#
# Displays current time, end time, remaining time.
# Plays a sound and pushes a notification when the time is up.
# Parameters
param(
# Length in minutes of the first timer, 30 minutes by default.
[Parameter( Mandatory = $false,
HelpMessage = "First timer")]
[int]$timerA = 30,
# Length in minutes of the second timer, turned off (0 minutes) by default
[Parameter( Mandatory = $false,
HelpMessage = "Second timer")]
[int]$timerB = 0,
# Number of times you wish the timer(s) to repeat, no repetition by default
[Parameter( Mandatory = $false,
HelpMessage = "Number of Cycles")]
[int]$timerCycles = 1
)
# Windows 10 Notification support
import-module BurntToast
# IFTTT Notification support
# Code by Dennis Rye, May 12, 2019
# https://www.dennisrye.com/post/send-smartphone-notifications-powershell-ifttt/
function Send-IftttAppNotification {
[cmdletbinding()]
param(
[Parameter(Mandatory)]
[string]$EventName,
[Parameter(Mandatory)]
[string]$Key,
# A String stating your timer has finished
[string]$Value1,
# The length of the timer that just finished
[string]$Value2,
# Total time remaining across all cycles
[string]$Value3
)
$webhookUrl = "https://maker.ifttt.com/trigger/{0}/with/key/{1}" -f $EventName, $Key
$body = @{
value1 = $Value1
value2 = $Value2
value3 = $Value3
}
Invoke-RestMethod -Method Get -Uri $webhookUrl -Body $body
}
# IFTTT INTEGRATION VARIABLES
# These must be set in order to use the optional IFTTT feature
# IFTTT integration flag. Set to $true in order to enable the integration
$IFTTT = $false
# Your Event Name
$YourEventName = "PUT YOUR EVENT NAME HERE AND DELETE THIS OR THE SCRIPT WON'T WORK"
# Your Key from https://ifttt.com/maker_webhooks/settings
$YourKey = "PUT YOUR KEY HERE AND DELETE THIS OR THIS SCRIPT WON'T WORK"
$IFTTTmsg = "Your timer has finished!"
# Initialize Variables
# ***Str is the version of the variable for concatinating
# startTime is the time the script is initialized
$startTime = $(get-date)
$startTimeStr = $startTime.ToShortTimeString()
# currentTime is the variable to be updated with the current time
$currentTime = $(get-date)
$currentTimeStr = $currentTime.ToShortTimeString()
# totalMinutes is a countdown to the end of all cycles
$totalMinutes = $TimerCycles * ($timerA + $timerB)
$totalMinutesStr = $totalMinutes
# endTime is when the final alarm will sound and the script will complete
$endTime = $(get-date).AddMinutes($totalMinutes)
$endTimeStr = $endTime.ToShortTimeString()
# current cycle
$cycle = 1
# Core Loop. Updates every 60 seconds.
clear-host
While ($cycle -le $timerCycles)
{
# Timer A
# minutesA is the local countdown for the first timerA
$minutesA = $timerA
# startTimeA is when the first time starts
$startTimeA = $(get-date)
$startTimeAStr = $startTimeA.ToShortTimeString()
# endTimeA is when the first timer ends
$endTimeA = $(get-date).AddMinutes($minutesA)
$endTimeAStr = $endTimeA.ToShortTimeString()
# endTimeB is when the second timer ends
$endMinutes = $timerA + $timerB
$endTimeB = $(get-date).AddMinutes($endMinutes)
$endTimeBStr = $endTimeB.ToShortTimeString()
if($endMinutes -gt 60)
{
$eM = $endMinutes
$eM = [timeSpan]::fromminutes($eM)
$eM = $eM.ToString("hh\:mm")
}
else {$eM = "$endMinutes minutes"}
While ($currentTime -le $endTimeA)
{
# Overall time display
write-host " ::OVERALL::"
write-host " Overall Start: $startTimeStr"
write-host " Overall End: $endTimeStr"
# Formats total times over 1 hour
# tM = string for Total Minutes
$tM = $totalMinutes
if($totalMinutes -gt 60)
{
$tM = [timeSpan]::fromminutes($totalMinutes)
$tM = $tM.ToString("hh\:mm")
}
elseif ($totalMinutesStr -gt 60) {$tM = "$totalMinutes minutes"}
# tMs = string for totalMinutesStr. Why yes, I am aware of that thing, thank you.
$tMs = "$totalMinutesStr minutes"
if($totalMinutesStr -gt 60)
{
$tMs = [timeSpan]::fromminutes($totalMinutesStr)
$tMs = $tMs.ToString("hh\:mm")
}
#Totals Summary
# Displays 1 minute remaining at 1 minute; otherwise displays "minutes"
if($totalMinutes -eq 1) {write-host " 1 minute of $tMs remaining overall."}
else {write-host " $tM of $tMs remaining overall."}
write-host ""
# Cycle Display (only if timerB is on)
if ($timerB -ge 1)
{
$cycleTime = $minutesA + $timerB
if($cycleTime -gt 60)
{
$cycleTime = [timeSpan]::fromminutes($cycleTime)
$cycleTime = $cycleTime.ToString("hh\:mm")
}
elseif ($endMinutes -gt 60)
{$cycleTime = "$cycleTime minutes"}
write-host " ::CYCLE::"
write-host " Cycle $cycle of $timerCycles"
write-host " Cycle Start: $startTimeAStr"
write-host " Cycle End: $endTimeBStr"
write-host " $cycleTime of $eM remaining this cycle."
write-host ""
}
# Timer display
# Formatting for times over 60 minutes
if($minutesA -gt 60)
{
$mA = [timeSpan]::fromminutes($minutesA)
$mA = $mA.ToString("hh\:mm")
}
else {$mA = "$minutesA"}
if($timerA -gt 60)
{
if($minutesA -le 60) {$mA = "$minutesA minutes"}
$tA = [timeSpan]::fromminutes($timerA)
$tA = $tA.ToString("hh\:mm")
}
elseif ($timerA -eq 1) {$tA = "1 minute"}
else {$tA = "$timerA minutes"}
write-host " ::TIMER::"
write-host " Timer Start: $startTimeAStr"
write-host " Timer End: $endTimeAStr"
write-host " $mA of $tA remaining this timer."
write-host ""
write-host " Now: $currentTimeStr"
# wait 1 minute
Start-Sleep -s 60
# Update currentTime and check to see if the timer is finished.
$currentTime = $(get-date)
$currentTimeStr = $currentTime.ToShortTimeString()
$minutesA = $minutesA - 1
$totalMinutes = $totalMinutes - 1
clear-host
}
# Timer A alarm & push notifications
write-host " A $timerA minute timer has finished."
New-BurntToastNotification -Text "PowerShell Timer", "Time's up!" -sound "Call3"
#IFTTT notification
if ($IFTTT)
{
# $shutup because if you leave it alone it outputs a statement.
$shutup = Send-IftttAppNotification -EventName $YourEventName -Key $YourKey -Value1 $IFTTTmsg -Value2 $timerA -Value3 $totalMinutes
}
# Timer B
# minutesB is the local countdown for the second timer, timerB
$minutesB = $timerB
# startTimeB is when the second timer starts
$startTimeB = $(get-date)
$startTimeBStr = $startTimeB.ToShortTimeString()
# endTimeB is when the second timer ends
$endTimeB = $(get-date).AddMinutes($minutesB)
$endTimeBStr = $endTimeB.ToShortTimeString()
While ($($currentTime -le $endTimeB) -and ($timerB -gt 0))
{
# Overall time display
write-host " ::OVERALL::"
write-host " Overall Start: $startTimeStr"
write-host " Overall End: $endTimeStr"
# Formats total times over 1 hour
# tM = string for Total Minutes
$tM = $totalMinutes
if($totalMinutes -gt 60)
{
$tM = [timeSpan]::fromminutes($totalMinutes)
$tM = $tM.ToString("hh\:mm")
}
elseif ($totalMinutesStr -gt 60) {$tM = "$totalMinutes minutes"}
# tMs = string for totalMinutesStr. Why yes, I am aware of that thing, thank you.
$tMs = "$totalMinutesStr minutes"
if($totalMinutesStr -gt 60)
{
$tMs = [timeSpan]::fromminutes($totalMinutesStr)
$tMs = $tMs.ToString("hh\:mm")
}
#Totals Summary
# Displays 1 minute remaining at 1 minute; otherwise displays "minutes"
if($totalMinutes -eq 1) {write-host " 1 minute of $tMs remaining overall."}
else {write-host " $tM of $tMs remaining overall."}
write-host ""
# Cycle Display
$cycleTime = $minutesB
if($minutesB -gt 60)
{
$cycleTime = [timeSpan]::fromminutes($minutesB)
$cycleTime = $cycleTime.ToString("hh\:mm")
}
else {$cycleTime = "$cycleTime minutes"}
write-host " ::CYCLE::"
write-host " Cycle $cycle of $timerCycles"
write-host " Cycle Start: $startTimeAStr"
write-host " Cycle End: $endTimeBStr"
write-host " $cycleTime of $eM remaining this cycle."
write-host ""
# Timer Display
# Formatting for times over 60 minutes
if($minutesB -gt 60)
{
$mB = [timeSpan]::fromminutes($minutesB)
$mB = $mB.ToString("hh\:mm")
}
else {$mB = "$minutesB"}
if($timerB -gt 60)
{
if($minutesB -le 60) {$mB = "$minutesB minutes"}
$tB = [timeSpan]::fromminutes($timerB)
$tB = $tB.ToString("hh\:mm")
}
elseif ($timerB -eq 1) {$tB = "1 minute"}
else {$tB = "$timerB minutes"}
write-host " ::TIMER::"
write-host " Timer Start: $startTimeBStr"
write-host " Timer End: $endTimeBStr"
write-host " $mB of $tB remaining this timer."
write-host ""
write-host " Now: $currentTimeStr"
# wait 1 minute
Start-Sleep -s 60
# Update currentTime and check to see if the timer is finished.
$currentTime = $(get-date)
$currentTimeStr = $currentTime.ToShortTimeString()
$minutesB = $minutesB - 1
$totalMinutes = $totalMinutes - 1
clear-host
}
# Timer B alarm & push notifications. Does not execute for a 0 minute timer.
if ($timerB -gt 0)
{
write-host " A $timerB minute timer has finished."
New-BurntToastNotification -Text "PowerShell Timer", "Time's up!" -sound "Call3"
#IFTTT notification
if ($IFTTT)
{
# $shutup because if you leave it alone it outputs a statement.
$shutup = Send-IftttAppNotification -EventName $YourEventName -Key $YourKey -Value1 $IFTTTmsg -Value2 $timerB -Value3 $totalMinutes
}
}
# Update Cycles
write-host " $cycle cycle(s) out of $timerCycles have completed."
$cycle = $cycle + 1
}
# Final time announcement
write-host " Time's up! All Cycles completed at $currentTimeStr."