Skip to content

Instantly share code, notes, and snippets.

@CannoHarito
Last active July 22, 2024 23:53
Show Gist options
  • Save CannoHarito/cb5f14b2ea45bd45807e8414dce906d9 to your computer and use it in GitHub Desktop.
Save CannoHarito/cb5f14b2ea45bd45807e8414dce906d9 to your computer and use it in GitHub Desktop.
放置時のデバイスのスリープや画面の電源オフを無効にするPoweshell
# @stringTrimmer https://gist.github.com/jamesfreeman959/231b068c3d1ed6557675f21c0e346a9c
Param([int]$seconds = -1, [switch]$display)
$Signature = @"
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern void SetThreadExecutionState(uint esFlags);
"@
$ES_SYSTEM_REQUIRED = [uint32]"0x00000001"
$ES_DISPLAY_REQUIRED = [uint32]"0x00000002"
$ES_CONTINUOUS = [uint32]"0x80000000"
$stes = Add-Type -MemberDefinition $Signature -Name System -Namespace Win32 -PassThru
if ($display) {
$stes::SetThreadExecutionState($ES_CONTINUOUS -bor $ES_DISPLAY_REQUIRED)
Write-Host "Keeping display awake"
}
else {
$stes::SetThreadExecutionState($ES_CONTINUOUS -bor $ES_SYSTEM_REQUIRED)
Write-Host "Keeping computer awake"
}
if ($seconds -ge 0) {
Write-Host "until" (Get-Date).AddSeconds($seconds)
Start-Sleep -Seconds $seconds
}
else {
Pause
}
rem ターミナルにEnterを入力するまで、スリープを無効
powershell -ExecutionPolicy RemoteSigned ./Keep-Awake.ps1
rem ターミナルにEnterを入力するまで、画面の電源オフやスクリーンセーバ起動を無効
powershell -ExecutionPolicy RemoteSigned ./Keep-Awake.ps1 -display
rem 3600秒間、スリープを無効
powershell -ExecutionPolicy RemoteSigned ./Keep-Awake.ps1 3600
rem 3600秒間、画面の電源オフやスクリーンセーバ起動を無効
powershell -ExecutionPolicy RemoteSigned ./Keep-Awake.ps1 (60*60) -display
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment