Skip to content

Instantly share code, notes, and snippets.

@NatLee
Created May 15, 2025 07:06
Show Gist options
  • Select an option

  • Save NatLee/7c53d73c0ff71b420aae2bb56bc74059 to your computer and use it in GitHub Desktop.

Select an option

Save NatLee/7c53d73c0ff71b420aae2bb56bc74059 to your computer and use it in GitHub Desktop.
Bypass group rules to avoid your Windows get sleeping. 忽略群組規則避免你的Windows自己睡眠
echo "[+] Script start"
# Add-Type for PowerManagement to prevent sleep
Add-Type -TypeDefinition @"
using System;
using System.Runtime.InteropServices;
public static class PowerManagement {
[DllImport("kernel32.dll", CharSet = CharSet.Auto, 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;
}
"@
# Function to write messages with timestamp
function Write-TimestampedMessage {
param([string]$Message)
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
echo "[$timestamp] $Message"
}
# Function to enable or disable sleep prevention
function Prevent-Sleep {
param([bool]$Enable)
if ($Enable) {
[PowerManagement]::SetThreadExecutionState([PowerManagement]::ES_CONTINUOUS -bor [PowerManagement]::ES_SYSTEM_REQUIRED -bor [PowerManagement]::ES_DISPLAY_REQUIRED)
Write-TimestampedMessage "Sleep prevention activated."
} else {
[PowerManagement]::SetThreadExecutionState([PowerManagement]::ES_CONTINUOUS)
Write-TimestampedMessage "Sleep prevention deactivated."
}
}
# Prevent sleep initially
Prevent-Sleep -Enable $true
try {
while ($true) {
Start-Sleep -Seconds 30
}
} finally {
Prevent-Sleep -Enable $false
Write-TimestampedMessage "Sleep prevention disabled."
echo "[+] Script end"
Write-Host "Press any key to continue..."
$Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment