-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBatteryNotifier.ps1
More file actions
92 lines (79 loc) · 2.33 KB
/
BatteryNotifier.ps1
File metadata and controls
92 lines (79 loc) · 2.33 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
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
# Lightweight battery notifier - basic monitor
$g1 = $false
$d1 = $false
$t1 = Get-Date
$f1 = $null
$s1 = $false
function pB {
$x1 = [System.Windows.Forms.SystemInformation]::PowerStatus
$p1 = [math]::Round($x1.BatteryLifePercent * 100)
$b1 = $x1.PowerLineStatus
return @{ Percent = $p1; Status = $b1 }
}
function yZ {
if ($s1) { return }
$s1 = $true
$u = New-Object System.Windows.Forms.Form
$u.Text = "Battery Notice"
$u.Size = New-Object System.Drawing.Size(300, 150)
$u.StartPosition = "CenterScreen"
$u.FormBorderStyle = "FixedDialog"
$u.TopMost = $true
$u.MaximizeBox = $false
$u.MinimizeBox = $false
$u.BackColor = [System.Drawing.Color]::White
$l = New-Object System.Windows.Forms.Label
$l.Text = "Battery is full. Please unplug the charger."
$l.Size = New-Object System.Drawing.Size(260, 60)
$l.Location = New-Object System.Drawing.Point(20, 20)
$l.Font = New-Object System.Drawing.Font("Segoe UI", 10)
$z = New-Object System.Windows.Forms.Button
$z.Text = "OK"
$z.Location = New-Object System.Drawing.Point(190, 85)
$z.Size = New-Object System.Drawing.Size(75, 25)
$z.Add_Click({
$t1 = Get-Date
$s1 = $false
$u.Close()
})
$u.Controls.Add($l)
$u.Controls.Add($z)
$u.Add_FormClosing({
$s1 = $false
$t1 = Get-Date
})
$u.ShowDialog() | Out-Null
$u.Dispose()
}
function zR {
$m = New-Object System.Windows.Forms.Timer
$m.Interval = 5000
$m.Add_Tick({
try {
$d = pB
$c = $d.Percent
$a = $d.Status
if ($a -eq "Online" -and $c -ge 100 -and -not $g1 -and -not $s1) {
$diff = (Get-Date) - $t1
if ($diff.TotalMinutes -ge 5 -or $t1 -eq (Get-Date).Date) {
yZ
}
}
} catch {}
})
$m.Start()
}
# App entry
try {
$initial = pB
if ($initial.Status -ne "Online") {
[System.Windows.Forms.MessageBox]::Show("No charger detected. Exiting.", "Notice", "OK", "Information")
exit
}
zR
[System.Windows.Forms.Application]::Run()
} catch {
[System.Windows.Forms.MessageBox]::Show("Error: $($_.Exception.Message)", "Fatal", "OK", "Error")
}