-
-
Save rbm01/a91d7cf04e86e2e558d482457fe86315 to your computer and use it in GitHub Desktop.
Suspend your workstation after Idle-Timeout with PowerShell
This file contains 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
# | |
# This background job automatically suspends your Workstation after a specified amount of | |
# time. It will come in handy if Windows sleep feature seems to go AWOL. | |
# | |
# start with | |
# powershell.exe -windowstyle hidden -executionpolicy Unrestricted P:\ATH\TO\logoff.ps1 | |
# | |
# `-windowstyle hidden` will make your PowerShell disappear/run in background | |
# `-executionpolicy Unrestricted` will enable this PowerShell process to allow non-signed scripts | |
# | |
# This is the only setting: How long before suspending? | |
# Alternative Options: | |
# * -Seconds 10 ( = 10 Seconds) | |
# * -Minutes 10 ( = 10 Minutes) | |
# * -Hours 10 ( = 10 Hours) | |
# | |
$idle_timeout = New-TimeSpan -Minutes 45 | |
# DO NOT CHANGE ANYTHING BELOW THIS LINE | |
#################################################################################################################################################################### | |
# This snippet is from http://stackoverflow.com/a/15846912 | |
Add-Type @' | |
using System; | |
using System.Diagnostics; | |
using System.Runtime.InteropServices; | |
namespace PInvoke.Win32 { | |
public static class UserInput { | |
[DllImport("user32.dll", SetLastError=false)] | |
private static extern bool GetLastInputInfo(ref LASTINPUTINFO plii); | |
[StructLayout(LayoutKind.Sequential)] | |
private struct LASTINPUTINFO { | |
public uint cbSize; | |
public int dwTime; | |
} | |
public static DateTime LastInput { | |
get { | |
DateTime bootTime = DateTime.UtcNow.AddMilliseconds(-Environment.TickCount); | |
DateTime lastInput = bootTime.AddMilliseconds(LastInputTicks); | |
return lastInput; | |
} | |
} | |
public static TimeSpan IdleTime { | |
get { | |
return DateTime.UtcNow.Subtract(LastInput); | |
} | |
} | |
public static int LastInputTicks { | |
get { | |
LASTINPUTINFO lii = new LASTINPUTINFO(); | |
lii.cbSize = (uint)Marshal.SizeOf(typeof(LASTINPUTINFO)); | |
GetLastInputInfo(ref lii); | |
return lii.dwTime; | |
} | |
} | |
} | |
} | |
'@ | |
#End snippet | |
do { | |
# 1st: How long is your computer currently idle? | |
$idle_time = [PInvoke.Win32.UserInput]::IdleTime; | |
#Write-Host ("Idle for " + $idle_time); | |
# If idle time is longer than allowed? -> suspend it! | |
if ($idle_time -gt $idle_timeout) { | |
# Suspend the PC | |
rundll32.exe powrprof.dll,SetSuspendState 0,1,0 | |
} | |
# Save the environment. Don't use 100% of a single CPU just for idle checking :) | |
Start-Sleep -Seconds 60 | |
} | |
while (1 -eq 1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment