-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhealth.ps1
128 lines (109 loc) · 4.3 KB
/
health.ps1
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
param (
[int] $creditMinutes=30,
$maxDate="20:00",
[int] $shutSeconds=900,
[bool] $startup=0
)
#run on at startup with C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -WindowStyle Hidden -executionpolicy bypass -command "& .\health.ps1" -creditMinutes 90 -maxDate "21:00" -start 1
#run periodically with C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -WindowStyle Hidden -executionpolicy bypass -command "& .\health.ps1" -creditMinutes 90 -maxDate "21:00"
function Show-Notification {
[cmdletbinding()]
Param (
[string]
$ToastTitle,
[string]
[parameter(ValueFromPipeline)]
$ToastText
)
[Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime] > $null
$Template = [Windows.UI.Notifications.ToastNotificationManager]::GetTemplateContent([Windows.UI.Notifications.ToastTemplateType]::ToastText02)
$RawXml = [xml] $Template.GetXml()
($RawXml.toast.visual.binding.text|where {$_.id -eq "1"}).AppendChild($RawXml.CreateTextNode($ToastTitle)) > $null
($RawXml.toast.visual.binding.text|where {$_.id -eq "2"}).AppendChild($RawXml.CreateTextNode($ToastText)) > $null
$SerializedXml = New-Object Windows.Data.Xml.Dom.XmlDocument
$SerializedXml.LoadXml($RawXml.OuterXml)
$Toast = [Windows.UI.Notifications.ToastNotification]::new($SerializedXml)
$Toast.Tag = $ToastTitle
$Toast.Group = $ToastTitle
$Toast.ExpirationTime = [DateTimeOffset]::Now.AddMinutes(1)
$Notifier = [Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier($ToastTitle)
$Notifier.Show($Toast);
}
function Show-Notification-And-Popup {
[cmdletbinding()]
Param (
[string]
$ToastTitle,
[string]
[parameter(ValueFromPipeline)]
$ToastText
)
Show-Notification $ToastTitle $ToastText
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[System.Windows.Forms.MessageBox]::Show($ToastText, $ToastTitle)
}
#Import-LocalizedData -BindingVariable LocalText
$LocalText = Data {
#culture="fr-FR"
ConvertFrom-StringData -StringData @'
title = Controle parental
expiredMaxdate = L'heure limite {0} est atteinte
expiredDuration = {0:hh}:{0:mm} dépasse la duree maximale de {1:hh}:{1:mm}
welcome = Bienvenue, Il reste {0:hh}:{0:mm} avant {1}
'@
}
$todayMinutesFile = (Get-Date -Format "yyyy-MM-dd")+'.cnt'
if (Test-Path $todayMinutesFile -PathType leaf)
{
#get previous time from file
[int] $previousCount = Get-Content $todayMinutesFile
#add time from previous file creation
if($startup)
{
#ignore time elapsed from previous count
[int] $todayElapsedMinutes = [math]::round($previousCount)
[bool] $greet = 1
}
else
{
#command tick, update ellapsed time from previous time dump
[int] $elapsedMinutesSinceFileWritten = [math]::round((New-TimeSpan -start (Get-Item $todayMinutesFile).LastWriteTime).totalminutes)
[int] $todayElapsedMinutes = $elapsedMinutesSinceFileWritten + $previousCount
}
}
else
{
#first time of day
[bool] $greet = 1
#initialize elapsed duration to 0
[int] $todayElapsedMinutes=0
}
#set current time modification and remaining credit
Write $todayElapsedMinutes > $todayMinutesFile
#check $creditMinutes
[int] $remainingMinutes=$creditMinutes - $todayElapsedMinutes
[bool] $expiredFromDuration = $remainingMinutes -le 0;
#check $maxDate
[int] $minutesToMaxDate = (new-timespan -end (get-date $maxDate ) ).totalminutes
[bool] $expiredFromMaxDate = $minutesToMaxDate -le 0;
#actual shutdown
if ( $expiredFromMaxDate -or $expiredFromDuration )
{
& shutdown /s /t $shutSeconds
}
#notification messages
if ( $expiredFromMaxDate )
{
Show-Notification-And-Popup -ToastTitle $LocalText.title -ToastText ($LocalText.expiredMaxDate -f $maxDate)
}
elseif ( $expiredFromDuration )
{
[TimeSpan] $todayElapsedTime = new-timespan -minutes $todayElapsedMinutes
[TimeSpan] $maxTimeDuration = new-timespan -minutes $creditMinutes
Show-Notification-And-Popup -ToastTitle $LocalText.title -ToastText ($LocalText.expiredDuration -f $todayElapsedTime, $maxTimeDuration)
}
elseif( $greet )
{
[TimeSpan] $remainingTime = new-timespan -minutes $remainingMinutes
Show-Notification -ToastTitle $LocalText.title -ToastText ($LocalText.welcome -f $remainingTime,$maxDate)
}