-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWorkTimer.ps1
More file actions
89 lines (69 loc) · 2.09 KB
/
WorkTimer.ps1
File metadata and controls
89 lines (69 loc) · 2.09 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
# -------------------------
# WorkTimer.ps1
# Purpose: 1-hour countdown + alerts + auto-logoff
# -------------------------
[CmdletBinding()]
param(
[switch] $Test,
[switch] $Shutdown,
[int[]] $Thresholds = @(600,300,60)
)
# --- Helper: show balloon tip + speak text ---
function Show-Alert {
param(
[string]$Text,
[int]$DurationMs = 5000
)
Write-Verbose "Preparing balloon tip: '$Text'"
# Load WinForms for ballon tip
Add-Type -AssemblyName System.Windows.Forms, System.Drawing
# Create Notification
$ni = New-Object System.Windows.Forms.NotifyIcon
$ni.Icon = [System.Drawing.SystemIcons]::Information
$ni.BalloonTipTitle = 'Session Timer'
$ni.BalloonTipText = $Text
$ni.Visible = $true
# Show it
$ni.ShowBalloonTip($DurationMs)
# Speak via SAPI
$voice = New-Object -ComObject SAPI.SpVoice
$voice.Speak($Text) | Out-Null
Write-Verbose "Spoken via SAPI.SpVoice"
# Clean up after a bit
Start-Sleep -Milliseconds ($DurationMs + 500)
$ni.Dispose()
}
if ($Test) {
# 10s, 5s, 1s instead of minutes
$Thresholds = @(10,5,1)
Write-Host '[TEST MODE] Using thresholds:' $Thresholds
}
# --- Main countdown logic ---
$totalSec = if ($Test) { 15 } else { 4200 }
# Track remaining time
$prev = $totalSec
foreach ($t in $thresholds) {
# Sleep until we hit this threshold
Write-Verbose "Sleeping for $($prev - $t) seconds until next threshold"
try {
Start-Sleep -Seconds ($prev - $t)
}
catch {
Write-Error "Sleep failed: $_"
break
}
if ($Test) {
Show-Alert -Text ("До выключения осталось {0} секунд{1}!" -f ($t), $( if ($t -le 2) { 'а' } else { '' } ))
}
else {
Show-Alert -Text ("До выключения осталось {0} минут{1}!" -f ($t/60), $( if ($t -le 65) { 'а' } else { '' } ))
}
$prev = $t
}
# Final sleep until zero
Start-Sleep -Seconds $prev
# Time's up-force logoff
Show-Alert -Text 'Время вышло, выключение компьютера...' -DurationMs 3000
if (!$Test -or $Shutdown) {
Start-Process -FilePath shutdown.exe -ArgumentList '/l','/f' -NoNewWindow
}