Skip to content

Commit

Permalink
Merge pull request #40 from hic-infra/autoshutdown-windows
Browse files Browse the repository at this point in the history
Autoshutdown for large Windows workspaces
  • Loading branch information
AaronJackson authored Jun 27, 2024
2 parents cf9cd91 + 9c61a47 commit fa4eb3a
Showing 1 changed file with 54 additions and 6 deletions.
60 changes: 54 additions & 6 deletions modules/autohibernate.ps1
Original file line number Diff line number Diff line change
@@ -1,7 +1,55 @@
powercfg /change hibernate-timeout-ac $Env:AUTOHIBERNATE_TIME

$shell = New-Object -ComObject WScript.Shell
$shortcut = $shell.CreateShortcut([Environment]::GetFolderPath("Desktop") + "\Hibernate.lnk")
$shortcut.TargetPath = "C:\Windows\System32\shutdown.exe"
$shortcut.Arguments = "/h"
$shortcut.Save()
$ramGB = (Get-CimInstance Win32_PhysicalMemory).Capacity / 1024 / 1024 / 1024

if ($null -eq $Env:AUTOHIBERNATE_TIME) {
$Env:AUTOHIBERNATE_TIME = 120
}

# Windows cannot hibernate (at least in AWS) if it has more than 16GB
# of RAM. Instead we'll forcefully shutdown the instance because EC2
# is expensive!
if ($ramGB -gt 16) {
$idleScript = "C:\workdir\idleshutdown.ps1"
New-Item $idleScript
Set-Content $idleScript @"
`$quser = quser | ForEach-Object -Process { `$_ -replace '\s{2,21}',',' } | ConvertFrom-Csv
`$session = `$quser | Where-Object {{ `$_.SessionName -like "rdp-tcp*" -or `$_.State -eq "Disc" }}
# quser time is in the following formats, depending on duration.
# "days+hours:minutes" or "hours:minutes" or "minutes" or, if
# less than a minute "."
# We need to convert it to something we can cast to TimeSpan,
# i.e. "days.hours:minutes" or just "hours:minutes"
`$idleTime = (`$session.'IDLE TIME').Replace('+', '.')
if (`$idleTime -eq ".") { `$idleTime = 0 }
if (`$idleTime -notmatch "\.") { `$idleTime = "0.0:`$idleTime" }
`$idleTime = [TimeSpan]`$idleTime
if (`$idleTime.TotalMinutes -ge $Env:AUTOHIBERNATE_TIME) {
Stop-Computer -ComputerName localhost -Force
}
"@

$action = New-ScheduledTaskAction -Execute powershell.exe `
-Argument "-File $idleScript"
$trigger = New-ScheduledTaskTrigger -Once -At (Get-Date) `
-RepetitionInterval (New-TimeSpan -Minutes 1) `
-RepetitionDuration (New-TimeSpan -Days 3650)
$principal = New-ScheduledTaskPrincipal `
-UserID "NT AUTHORITY\SYSTEM" `
-LogonType ServiceAccount

Register-ScheduledTask "ShutdownIdle" `
-Action $action `
-Principal $principal `
-Trigger $trigger `
-Force
} else {
powercfg /change hibernate-timeout-ac $Env:AUTOHIBERNATE_TIME

$shell = New-Object -ComObject WScript.Shell
$shortcut = $shell.CreateShortcut([Environment]::GetFolderPath("Desktop") + "\Hibernate.lnk")
$shortcut.TargetPath = "C:\Windows\System32\shutdown.exe"
$shortcut.Arguments = "/h"
$shortcut.Save()
}

0 comments on commit fa4eb3a

Please sign in to comment.