Skip to content

Instantly share code, notes, and snippets.

@joe-scalise
Created March 15, 2022 13:38
Show Gist options
  • Select an option

  • Save joe-scalise/5fbbdf64ad307778f9147ee17ba40d44 to your computer and use it in GitHub Desktop.

Select an option

Save joe-scalise/5fbbdf64ad307778f9147ee17ba40d44 to your computer and use it in GitHub Desktop.
Simulates pressing scroll lock to prevent idle (screensaver etc.)
<#
.SYNOPSIS
Simulates pressing scroll lock to prevent idle.
.NOTES
Gathered from another source originally, unsure where
#>
[CmdletBinding()]
param (
[Int]$Sleep = 240,
[Int]$Index = 0,
[Int]$announcementInterval = 10
)
function Log {
param($log)
Write-Information -MessageData $log -InformationAction Continue
}
try {
$stopWatch = [system.diagnostics.stopwatch]::StartNew()
$wShell = New-Object -com "Wscript.Shell"
} catch {
Log "Could not start stopwatch or wscript.shell, exiting"
exit 1
}
Clear-Host
Log "Starting Scroll Lock key toggle $(Get-Date)"
try {
while ( $true )
{
Write-Host -NoNewLine "." -fore red
$wShell.sendkeys("{SCROLLLOCK}")
Start-Sleep -Milliseconds 200
$wShell.sendkeys("{SCROLLLOCK}")
Start-Sleep -Seconds $sleep
# Announce runtime on an interval
if ($stopWatch.IsRunning -and ((++$index % $announcementInterval) -eq 0))
{
Log "`nElapsed: $($stopwatch.Elapsed.ToString('dd\d\ hh\h\ mm\m\ ss\s'))"
}
}
} catch {
Log "Error in while loop, exiting"
exit 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment