Skip to content

Instantly share code, notes, and snippets.



Last active Jan 5, 2021
What would you like to do?
RDP Keepalive using Kernel SetThreadExecutionState
$host.ui.RawUI.WindowTitle = "Idle Keepalive"
$dotNetCode = @'
[DllImport("kernel32.dll", CharSet = CharSet.Auto,SetLastError = true)]
public static extern void SetThreadExecutionState(uint esFlags);
$ste = Add-Type -memberDefinition $dotNetCode -name System -namespace Win32 -passThru
$ES_CONTINUOUS = [uint32]"0x80000000" #Requests that the other EXECUTION_STATE flags set remain in effect until SetThreadExecutionState is called again with the ES_CONTINUOUS flag set and one of the other EXECUTION_STATE flags cleared.
$ES_AWAYMODE_REQUIRED = [uint32]"0x00000040" #Requests Away Mode to be enabled.
$ES_DISPLAY_REQUIRED = [uint32]"0x00000002" #Requests display availability (display idle timeout is prevented).
$ES_SYSTEM_REQUIRED = [uint32]"0x00000001" #Requests system availability (sleep idle timeout is prevented).
Write-Verbose "Power Plan suspended with option: $option"
read-host "Keepalive active. Press any key to quit"
Write-Verbose "Power Plan suspension ended"
#powercfg -requests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment