| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- # PowerShell script to prevent system sleep/hibernate
- # This script uses Windows API to prevent system from sleeping
- # Run with: powershell -ExecutionPolicy Bypass -File prevent-sleep.ps1
- try {
- Add-Type @"
- using System;
- using System.Runtime.InteropServices;
- public class PowerManagement {
- [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 const uint ES_AWAYMODE_REQUIRED = 0x00000040;
-
- public static void PreventSleep() {
- // Prevent system from sleeping, but allow display to turn off
- SetThreadExecutionState(ES_CONTINUOUS | ES_SYSTEM_REQUIRED | ES_DISPLAY_REQUIRED);
- }
-
- public static void AllowSleep() {
- // Allow system to sleep normally
- SetThreadExecutionState(ES_CONTINUOUS);
- }
- }
- "@
- # Prevent sleep
- [PowerManagement]::PreventSleep()
-
- # Exit successfully
- exit 0
- } catch {
- # If error occurs, exit with error code
- Write-Error $_.Exception.Message
- exit 1
- }
|