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
}

chrome_and_teams_loop

while(1) { # Loop forever
#
#CHROME
$wshell = New-Object -ComObject wscript.shell
if($wshell.AppActivate(‘Chrome’)) { # Switch to Chrome
Sleep 1 # Wait for Chrome to “activate”
$wshell.SendKeys(‘{F5}’) # Send F5 (Refresh)
Sleep 1
$wshell.SendKeys(‘^{TAB}’) #Go to next tab
Sleep 1
$wshell.SendKeys(‘{F5}’) # Send F5 (Refresh)
Sleep 1
$wshell.SendKeys(‘^{TAB}’) #Go to next tab
Write-Host (“{0} – Refresh of Chrome tabs done, wait 10 mins” -f $(get-date)) -ForegroundColor Green
sleep -Seconds 599 # Wait 10 minutes
} else { break; } # Chrome not open, exit the loop
#
#
#TEAMS
$teamsMainWindowTitle = Get-Process -name “ms-teams” | Where-Object { $_.MainWindowTitle } | Select-Object MainWindowTitle
try {
Get-Process -Name ‘ms-teams’ -ErrorAction stop | Out-Null
Write-Host (“{0} – Microsoft Teams is running…” -f $(get-date)) -ForegroundColor Green
$wshell = New-Object -ComObject wscript.shell
$wshell.AppActivate(“Activity | “) # Switch to Teams
$wshell.sendkeys(“{NUMLOCK}{NUMLOCK}”)
Write-Host (“{0} – Pressed NUMLOCK twice and waiting for 10 seconds” -f $(get-date)) -ForegroundColor Green
Start-Sleep -Seconds 10 #60

}
catch {
    Write-Warning ("{0} - Microsoft Teams is not running, sleeping for 15 seconds..." -f $(get-date))
    Start-Sleep -Seconds 15
}

}