Last active
January 30, 2023 18:14
-
-
Save jcefoli/36ed07c08dc3795648b3f45185f721c5 to your computer and use it in GitHub Desktop.
RDP Keepalive using Kernel SetThreadExecutionState
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$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" | |
$ste::SetThreadExecutionState($ES_CONTINUOUS -bor $ES_SYSTEM_REQUIRED -bor $ES_DISPLAY_REQUIRED) | |
read-host "Keepalive active. Press any key to quit" | |
Write-Verbose "Power Plan suspension ended" | |
$ste::SetThreadExecutionState($ES_CONTINUOUS) | |
#powercfg -requests |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment