Skip to content

Instantly share code, notes, and snippets.

@royashbrook
Created April 13, 2021 17:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save royashbrook/89dd186c5beca90f3ba4d64656517013 to your computer and use it in GitHub Desktop.
Save royashbrook/89dd186c5beca90f3ba4d64656517013 to your computer and use it in GitHub Desktop.
Set STES so machine won't sleep
# import kernel32 calls we need
# note screen saver will not enable either
# can remove the display required flag to remove that
Add-Type -Name n -Namespace ns -MemberDefinition @'
[DllImport("kernel32.dll")]
public static extern uint SetThreadExecutionState(uint esFlags);
public const uint ES_CONTINUOUS = 0x80000000;
public const uint ES_SYSTEM_REQUIRED = 0x00000001;
public const uint ES_DISPLAY_REQUIRED = 0x00000002;
public static string StayAwake(){
var flags = ES_CONTINUOUS | ES_SYSTEM_REQUIRED | ES_DISPLAY_REQUIRED;
var retval = SetThreadExecutionState(flags);
return retval.ToString();
}
'@
# continually enables stes every 90 seconds for this process
function Enable-STES(){
while($true)
{
$fPreviousExecutionState = [ns.n]::StayAwake();
"`$fPreviousExecutionState = $fPreviousExecutionState"
Start-Sleep -Seconds 90
}
}
# don't go to sleep!
Enable-STES
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment