From 3dcf228ca72118ca1191ec7ac38d54e2a7b4b03e Mon Sep 17 00:00:00 2001 From: Manuel Thalmann Date: Sat, 21 Sep 2024 02:10:20 +0200 Subject: [PATCH] Add scripts for backing up energy plan --- scripts/Common/Scripts/Operations.ps1 | 2 + scripts/Windows/Scripts/Deployment.ps1 | 8 +++- scripts/Windows/Scripts/PowerManagement.ps1 | 51 +++++++++++++++++++++ 3 files changed, 60 insertions(+), 1 deletion(-) diff --git a/scripts/Common/Scripts/Operations.ps1 b/scripts/Common/Scripts/Operations.ps1 index d954b457..169e9391 100644 --- a/scripts/Common/Scripts/Operations.ps1 +++ b/scripts/Common/Scripts/Operations.ps1 @@ -83,6 +83,7 @@ $null = New-Module { if ($IsWindows) { $env:WSLENV = "CONFIG_NAME"; New-Alias -Force "sudo" gsudo; + Backup-PowerScheme; } if (-not $NoImplicitCleanup.IsPresent) { @@ -418,6 +419,7 @@ $null = New-Module { $null = Uninstall-Package Selenium.WebDriver -ErrorAction Continue; Uninstall-ChocoPackage 7zip.portable gsudo selenium-gecko-driver yq; Uninstall-WingetPackage AutoHotkey.AutoHotkey; + Restore-PowerScheme; } foreach ($module in (Get-RequiredModules)) { diff --git a/scripts/Windows/Scripts/Deployment.ps1 b/scripts/Windows/Scripts/Deployment.ps1 index 72fcb36b..374fd86f 100644 --- a/scripts/Windows/Scripts/Deployment.ps1 +++ b/scripts/Windows/Scripts/Deployment.ps1 @@ -1,3 +1,4 @@ +. "$PSScriptRoot/PowerManagement.ps1"; . "$PSScriptRoot/../../Common/Scripts/Software.ps1"; . "$PSScriptRoot/../../Common/Scripts/SoftwareManagement.ps1"; . "$PSScriptRoot/../../Common/Types/InstallerAction.ps1"; @@ -61,7 +62,12 @@ function Deploy-SoftwareAction { if ($install) { if ($hardware.amdCPU) { Install-ChocoPackage amd-ryzen-master; - # ToDo: backup Ryzen energy saving plan + $ryzenPlan = [regex]::Match("" + ((powercfg /LIST) | Where-Object { $_ -like "*(AMD Ryzen*"; }), "Power Scheme GUID: ([0-9a-f-]+) ").Groups[1].Value; + + if ($ryzenPlan) { + powercfg /S $ryzenPlan; + Backup-PowerScheme; + } } if ($hardware.nvidiaGPU) { diff --git a/scripts/Windows/Scripts/PowerManagement.ps1 b/scripts/Windows/Scripts/PowerManagement.ps1 index 91846195..45e003a3 100644 --- a/scripts/Windows/Scripts/PowerManagement.ps1 +++ b/scripts/Windows/Scripts/PowerManagement.ps1 @@ -9,6 +9,7 @@ $null = New-Module { $systemRunOncePath = "HKLM:\$runOncePath"; $logonPath = "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" $runOnceName = "PortValhalla"; + $powerSchemeOption = "PowerScheme"; $autologinOption = "AutoAdminLogon"; $domainOption = "DefaultDomainName"; $userOption = "DefaultUserName"; @@ -59,6 +60,56 @@ $null = New-Module { "& `$env:INSTALLER_SCRIPT;"; } + <# + .SYNOPSIS + Gets the GUID of the current power scheme. + #> + function Get-PowerScheme { + [regex]::Match((powercfg /GETACTIVESCHEME), "Power Scheme GUID: ([0-9a-f-]+) ").Groups[1].Value; + } + + <# + .SYNOPSIS + Sets the current power scheme. + + .PARAMETER SchemeID + The ID of the scheme to set. + #> + function Set-PowerScheme { + param( + [string] $SchemeID + ) + + powercfg /S $SchemeID; + } + + <# + .SYNOPSIS + Backs up and overrides the current power scheme if it might cause automatic standby. + #> + function Backup-PowerScheme { + $performanceScheme = "8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c"; + $currentScheme = Get-PowerScheme; + + if ($currentScheme -ne $performanceScheme) { + Write-Host "Disabling Power Save mode"; + Set-SetupOption $powerSchemeOption $currentScheme; + powercfg /S $performanceScheme; + } + } + + <# + .SYNOPSIS + Restores the backed up power scheme. + #> + function Restore-PowerScheme { + $scheme = Get-SetupOption $powerSchemeOption; + + if ($scheme) { + powercfg /S $scheme; + } + } + <# .SYNOPSIS Registers a task to run the setup once after the next reboot.