Skip to content

Instantly share code, notes, and snippets.

@paveltimofeev
Last active September 10, 2015 11:58
Show Gist options
  • Save paveltimofeev/2cf4f005fe0cbf5d2d01 to your computer and use it in GitHub Desktop.
Save paveltimofeev/2cf4f005fe0cbf5d2d01 to your computer and use it in GitHub Desktop.
Locking PC for 5 min every 1.5 hours (powershell / win)
cls
function ConvertTo-Date
{
param( [string]$time )
return Get-Date -Hour ($time -split ":")[0] -Minute ($time -split ":")[1] -Second 0
}
function Get-WhatSooner
{
param( $date1, $date2 )
$now = Get-Date
if( ($date1 - $now).TotalMilliseconds -lt 0 ){ $date1 = $date2 }
if( ($date2 - $now).TotalMilliseconds -lt 0 ){ $date2 = $date1 }
if( ($date1 - $date2).TotalMilliseconds -lt 0 )
{
return $date1
}
else
{
return $date2
}
}
function Wait-For
{
param(
[Parameter(Mandatory=$true)]
[int]
$minutes,
[Parameter(Mandatory=$true)]
$callback )
$untill = (Get-Date).AddMinutes( $minutes )
Wait-Untill $untill.Hour $untill.Minute $callback
}
function Wait-ForOr
{
param(
[Parameter(Mandatory=$true)]
[int]$minutes,
[Parameter(Mandatory=$true)]
[string]
$or,
[Parameter(Mandatory=$true)]
$callback )
$time1 = (Get-Date).AddMinutes( $minutes )
$time2 = ConvertTo-Date $or
$untill = Get-WhatSooner $time1 $time2
Wait-Untill $untill.Hour $untill.Minute $callback
}
function Wait-Untill
{
param( $wait_Hour, $wait_Minute, $callback )
"Wait untill $($wait_Hour):$($wait_Minute)"
$now = Get-Date
while($now.Hour -ne $wait_Hour -or $now.Minute -ne $wait_Minute)
{
$now = Get-Date
Sleep -s 1
}
& $callback
}
function Lock-PC
{
rundll32.exe user32.dll,LockWorkStation
}
function Show-BalloonTip
{
[CmdletBinding(SupportsShouldProcess = $true)]
param
(
[Parameter(Mandatory=$true)] $Text,
[Parameter(Mandatory=$true)] $Title,
[ValidateSet('None', 'Info', 'Warning', 'Error')] $Icon = 'Info',
$Timeout = 10000
)
Add-Type -AssemblyName System.Windows.Forms
if ($script:balloon -eq $null)
{
$script:balloon = New-Object System.Windows.Forms.NotifyIcon
}
$path = Get-Process -id $pid | Select-Object -ExpandProperty Path
$balloon.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon($path)
$balloon.BalloonTipIcon = $Icon
$balloon.BalloonTipText = $Text
$balloon.BalloonTipTitle = $Title
$balloon.Visible = $true
$balloon.ShowBalloonTip($Timeout)
}
function Lock-Me
{
param( [int]$after, [int]$for )
Wait-ForOr ($after - 5) -or "14:00" {
Show-BalloonTip "Don't spend time beating on a wall, hoping to transform it into a door" "5 minutes left" Info
Wait-For 4 { "4 min left" }
Show-BalloonTip "Take a rest to recharge your mind" "1 minute to rest" Info
Wait-For 1 { "Locking" }
Lock-PC
}
# 5 min locked
1..$for | % { Wait-For 1 { Lock-PC } }
}
while( $true )
{
Lock-Me -after 90 -for 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment