Created
December 11, 2024 10:53
-
-
Save devops-hacks/8fe2bd417e287f66e147553e119c8a69 to your computer and use it in GitHub Desktop.
Keep windows system busy (as if user working) to prevent it from sleeping, auto-logoff, powering down etc.
This file contains hidden or 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
# Prevent the system from sleeping | |
Add-Type -TypeDefinition @" | |
using System; | |
using System.Runtime.InteropServices; | |
public class PowerUtilities { | |
[DllImport("kernel32.dll", SetLastError = true)] | |
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; | |
} | |
"@ | |
# Prevents the system from sleeping and the display from turning off | |
[void] [PowerUtilities]::SetThreadExecutionState([PowerUtilities]::ES_CONTINUOUS -bor [PowerUtilities]::ES_SYSTEM_REQUIRED -bor [PowerUtilities]::ES_DISPLAY_REQUIRED) | |
# Keep script running indefinitely (or for a desired duration) | |
Write-Host "PC is awake. Press Ctrl + C to stop." | |
while ($true) { | |
Start-Sleep -Seconds 360 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment