Keep Alive script – aaa-awake

A powershell script wrapped inside a bat file to keep a PC or server session active, place them in the same directory and run it by double clicking the bat file.

aaa-awake-ps.bat

@echo off
powershell -NoProfile -ExecutionPolicy Bypass -File "%~dp0aaa-awake-ps.ps1"

REM DEBUGGING SECTION - Just remove the REM at the start of the line
REM echo.
REM echo Script finished. Exit code: %ERRORLEVEL%
REM pause

aaa-awake-ps.ps1

Add-Type @"
using System;
using System.Runtime.InteropServices;
public static class IdleReset {
[DllImport("kernel32.dll")]
public static extern uint SetThreadExecutionState(uint esFlags);
}
"@

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

$cycles = 57  # ~9.5 hours at 10 min intervals

try {
    for ($i = 1; $i -le $cycles; $i++) {

        # 1. System-level idle reset
        [IdleReset]::SetThreadExecutionState([uint32]2147483650) | Out-Null

        # 2. Tiny mouse movement
        $pos = [System.Windows.Forms.Cursor]::Position
        [System.Windows.Forms.Cursor]::Position = New-Object System.Drawing.Point ($pos.X + 1), $pos.Y
        Start-Sleep -Milliseconds 200
        [System.Windows.Forms.Cursor]::Position = $pos

        # 3. ScrollLock toggle
        [System.Windows.Forms.SendKeys]::SendWait("{SCROLLLOCK}")
        Start-Sleep -Milliseconds 200
        [System.Windows.Forms.SendKeys]::SendWait("{SCROLLLOCK}")

        # Status
        Write-Host "$(( $cycles - $i ) * 10) minutes left"

        Start-Sleep -Seconds 600
    }
}
finally {
    # Restore normal Windows power-management behaviour
    [IdleReset]::SetThreadExecutionState([uint32]2147483648) | Out-Null
}