Created
April 13, 2021 17:21
-
-
Save royashbrook/89dd186c5beca90f3ba4d64656517013 to your computer and use it in GitHub Desktop.
Set STES so machine won't sleep
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
# 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