Functions/FileSystem/Invoke-Awake.ps1
Function Invoke-Awake { [CmdletBinding()] Param ( # Number of minutes to Stay Awake [Parameter(Mandatory=$true)] [INT] $Minutes = 60, # Number of Minutes to Update Progress [Parameter(Mandatory=$false)] [ValidateRange(1,60)] [INT] $Cycle = 10, # Key to use in SendKeys Buffer [Parameter(Mandatory=$false)] [ValidateSet('{F15}','+{F15}','{CAPSLOCK}','{SCROLLLOCK}','{NUMLOCK}')] [string] $StallKey = '{F15}' ) Begin { # Send Notification Message to Environment write-host "The screen will stay awake for $minutes minutes" -ForegroundColor Cyan } Process { # Establish Starting State and Time $Start = Get-Date $Complete = $false # Build new Wscript Shell Object $Wscript = New-Object -ComObject "Wscript.Shell" # Begin While Loop While (!$Complete) { # Get Current Time and Write Progress $Now = Get-Date $Elapsed = $NOW - $Start Write-Progress -Activity "Staying Awake using `"$StallKey`" (with $cycle second cycles)" -CurrentOperation "$($Minutes-$Elapsed.Minutes-1) Minutes and $(60 - $Elapsed.Seconds) Seconds Remaining" -SecondsRemaining (($Minutes*60)-$Elapsed.TotalSeconds) # Check for Completion IF ($Elapsed.TotalSeconds -ge ($Minutes*60)) {$Complete = $true} # LOOP ELSE { # SendKeys to Keep Script Awake $Wscript.SendKeys($StallKey) $Wscript.SendKeys($StallKey) Write-Verbose -Message "sending `'$Stallkey`"" # Sleep for a count of Seconds Start-Sleep -Seconds $Cycle } } # Destroy Wscript Shell $Wscript = $NULL # Send Completion $End = get-date Write-Host "The screen stayed awake for $Minutes Minutes (from $Start to $END)" -ForegroundColor Cyan } } |