PortValhalla/scripts/Windows/Scripts/PowerManagement.ps1

57 lines
1.5 KiB
PowerShell
Raw Normal View History

using namespace Microsoft.Win32;
$null = New-Module {
2024-08-07 19:05:32 +00:00
. "$PSScriptRoot/../../Common/Scripts/Scripting.ps1";
[RegistryKey] $key = $null;
$runOncePath = "HKLM:\Software\Microsoft\Windows\CurrentVersion\RunOnce";
$runOnceName = "PortValhalla";
<#
.SYNOPSIS
Gets the reghistry key containing the `RunOnce` commands.
#>
function Get-RunOnceKey {
Get-Item $runOncePath;
}
<#
.SYNOPSIS
Registers a task to run the setup once after the next reboot.
.PARAMETER UserKey
The regtistry key of the user to register the task for.
#>
function Register-Setup {
param(
[RegistryKey] $UserKey
)
$key = Get-RunOnceKey $UserKey;
Set-ItemProperty -Path $key.PSPath -Name $runOnceName -Type "ExpandString" -Value (
"pwsh -Command " +
2024-08-09 02:08:22 +00:00
(& {
if ($env:PWSH_PATH) {
"`$env:PWSH_PATH = $(ConvertTo-Injection $env:PWSH_PATH);"
} else {
""
}
}) +
"`$env:INSTALLER_SCRIPT = $(ConvertTo-Injection $env:INSTALLER_SCRIPT);" +
"`$env:CONFIG_MODULE = $(ConvertTo-Injection $env:CONFIG_MODULE);" +
"& `$env:INSTALLER_SCRIPT;"
);
$key.Handle.Close();
}
<#
.SYNOPSIS
Reboots the machine intermediately and restarts the setup after the next login.
#>
function Restart-Intermediate {
Register-Setup;
Restart-Computer;
}
}