Skip to content

Instantly share code, notes, and snippets.

@FriedrichWeinmann
Created December 17, 2019 09:19
Show Gist options
  • Save FriedrichWeinmann/c293c1305a399975e53f29bafecefc6d to your computer and use it in GitHub Desktop.
Save FriedrichWeinmann/c293c1305a399975e53f29bafecefc6d to your computer and use it in GitHub Desktop.
Makes PowerShell claim to be an active video player & backup software - no screensaver, no standby
$source = @'
using System;
using System.Runtime.InteropServices;
namespace Shell
{
public static class Host
{
public static void DisableScreensaver()
{
NativeMethods.SetThreadExecutionState(ExecutionState.DisplayRequired);
NativeMethods.SetThreadExecutionState(ExecutionState.SystemRequired | ExecutionState.AwayModeRequired | ExecutionState.Continuous);
}
public static void EnableScreensaver()
{
NativeMethods.SetThreadExecutionState(ExecutionState.Continuous);
}
}
internal static class NativeMethods
{
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
internal static extern ExecutionState SetThreadExecutionState(ExecutionState asFlags);
}
[Flags]
public enum ExecutionState : uint
{
SystemRequired = 1,
DisplayRequired = 2,
UserPresent = 4,
AwayModeRequired = 0x40,
Continuous = 0x80000000
}
}
'@
Add-Type $source
$limit = (Get-Date).AddHours(9)
while ((Get-Date) -lt $limit) {
[Shell.Host]::DisableScreensaver()
Start-Sleep -Seconds 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment