Skip to content

Commit

Permalink
Merge pull request #41 from hic-infra/autoshutdown-windows
Browse files Browse the repository at this point in the history
Autoshutdown windows
  • Loading branch information
AaronJackson authored Jul 31, 2024
2 parents fa4eb3a + 2d13107 commit af235a2
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 38 deletions.
50 changes: 18 additions & 32 deletions modules/autohibernate.ps1
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@

$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 @"

$idleScript = "C:\Tools\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" }}
Expand All @@ -30,26 +25,17 @@ if (`$idleTime.TotalMinutes -ge $Env:AUTOHIBERNATE_TIME) {
}
"@

$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()
}
$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
4 changes: 2 additions & 2 deletions modules/libreoffice.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ $ProgressPreference = 'SilentlyContinue' # Disable (slow) progress bar

Set-Location C:\Tools

$LIBRE_VERSION = "24.2.3"
$LIBRE_HASH = "12b988318452120d31a4c618bc6ac314ad3bf99157584d0c2bf6029801a4cf31"
$LIBRE_VERSION = "24.2.5"
$LIBRE_HASH = "cad625d119db4720df5270fa2d860e0ed56d6e374b878e19a3cab4af68f1f54f"

$LIBRE_URL = "https://mirrors.ukfast.co.uk/sites/documentfoundation.org/tdf/libreoffice/stable/${LIBRE_VERSION}/win/x86_64/LibreOffice_${LIBRE_VERSION}_Win_x86-64.msi"

Expand Down
8 changes: 4 additions & 4 deletions modules/rstudio.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ $ProgressPreference = "SilentlyContinue" # PS progress bar is slow
$ErrorActionPreference = "Stop"

# R environment
Invoke-WebRequest -Uri "https://cloud.r-project.org/bin/windows/base/R-4.4.0-win.exe" -OutFile C:\Tools\R-installer.exe
Invoke-WebRequest -Uri "https://cloud.r-project.org/bin/windows/base/R-4.4.1-win.exe" -OutFile C:\Tools\R-installer.exe
Start-Process C:\Tools\R-installer.exe -ArgumentList "/VERYSILENT","/NORESTART" -NoNewWindow -Wait -PassThru

Invoke-WebRequest -Uri "https://download1.rstudio.org/electron/windows/RStudio-2024.04.1-748.exe" -OutFile C:\Tools\RStudio-installer.exe
Invoke-WebRequest -Uri "https://download1.rstudio.org/electron/windows/RStudio-2024.04.2-764.exe" -OutFile C:\Tools\RStudio-installer.exe
Start-Process C:\Tools\RStudio-installer.exe -ArgumentList "/S" -NoNewWindow -Wait -PassThru

Invoke-WebRequest -Uri "https://cran.r-project.org/bin/windows/Rtools/rtools44/files/rtools44-6104-6039.exe" -OutFile C:\Tools\RTools.exe
Expand All @@ -17,7 +17,7 @@ Start-Process C:\Tools\RTools.exe -ArgumentList "/VERYSILENT" -NoNewWindow -Wait
$pkgs = "tidyverse","odbc","dbi","qqman","metafor","tidyr","ggplot2",`
"hmisc","data.table","dplyr","lubridate","survival","survminer"
foreach ($pkg in $pkgs) {
& "C:\Program Files\R\R-4.4.0\bin\Rscript.exe" `
& "C:\Program Files\R\R-4.4.1\bin\Rscript.exe" `
-e "install.packages('$pkg', repos='http://cran.uk.r-project.org')"
}

Expand All @@ -34,5 +34,5 @@ local({r <- getOption("repos")
# Set timezone
Sys.setenv(TZ='Europe/London')
"@
Set-Content "C:\Program Files\R\R-4.4.0\etc\Rprofile.site" $RConfig
Set-Content "C:\Program Files\R\R-4.4.1\etc\Rprofile.site" $RConfig
Set-Content "C:\Users\Administrator\Documents\.Renviron" "RSTUDIO_DISABLE_SECURE_DOWNLOAD_WARNING=1"

0 comments on commit af235a2

Please sign in to comment.