PortValhalla/scripts/Windows/Scripts/PowerManagement.ps1

166 lines
4.3 KiB
PowerShell
Raw Normal View History

using namespace Microsoft.Win32;
$null = New-Module {
. "$PSScriptRoot/../Scripts/Registry.ps1";
2024-08-09 02:19:49 +00:00
. "$PSScriptRoot/../../Common/Scripts/Config.ps1";
2024-08-07 19:05:32 +00:00
. "$PSScriptRoot/../../Common/Scripts/Scripting.ps1";
[RegistryKey] $key = $null;
$runOncePath = "Software\Microsoft\Windows\CurrentVersion\RunOnce";
$systemRunOncePath = "HKLM:\$runOncePath";
2024-08-09 02:19:49 +00:00
$logonPath = "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon"
$runOnceName = "PortValhalla";
2024-08-09 02:19:49 +00:00
$autologinOption = "AutoAdminLogon";
$domainOption = "DefaultDomainName";
$userOption = "DefaultUserName";
$passwordOption = "DefaultPassword";
<#
.SYNOPSIS
Gets the reghistry key containing the `RunOnce` commands.
#>
function Get-RunOnceKey {
param(
[RegistryKey] $UserKey
)
[string] $path = $null;
if ($UserKey) {
$path = "$($UserKey.PSPath)\$runOncePath";
} else {
$path = $systemRunOncePath;
}
if (-not (Test-Path $path)) {
New-Item $path;
} else {
Get-Item $path;
}
}
<#
.SYNOPSIS
Generates a script for executing the installer.
#>
function Get-StartupScript {
"pwsh " + (Get-StartupArguments);
}
<#
.SYNOPSIS
Generates arguments for running the installer using `pwsh`.
#>
function Get-StartupArguments {
"-Command " +
(& {
if ($env:PWSH_PATH) {
"`$env:PWSH_PATH = $(ConvertTo-Injection $env:PWSH_PATH);"
} else {
""
}
}) +
"`$env:INSTALLER_SCRIPT = $(ConvertTo-Injection (Resolve-Path $env:INSTALLER_SCRIPT));" +
2024-08-09 21:23:55 +00:00
"`$env:CONFIG_MODULE = $(ConvertTo-Injection (Resolve-Path $env:CONFIG_MODULE));" +
"& `$env:INSTALLER_SCRIPT;";
}
<#
.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(
[Parameter(ParameterSetName="System")]
[switch] $System,
2024-08-10 01:40:37 +00:00
[Parameter(ParameterSetName="User", Mandatory)]
[switch] $User,
2024-08-09 23:50:52 +00:00
[Parameter(ParameterSetName="User")]
2024-08-10 01:40:37 +00:00
[Parameter(ParameterSetName="SpecificUser", Mandatory)]
[RegistryKey] $UserKey
)
2024-08-10 01:40:37 +00:00
if ($User.IsPresent -or $UserKey) {
if (-not $UserKey) {
$UserKey = Get-Item "HKCU:\";
}
$key = Get-RunOnceKey $UserKey;
} else {
$key = Get-RunOnceKey;
}
Set-ItemProperty -Path $key.PSPath -Name $runOnceName -Type "ExpandString" -Value (Get-StartupScript);
$key.Handle.Close();
}
2024-08-09 02:19:49 +00:00
<#
.SYNOPSIS
Sets the user to login automatically on boot.
.PARAMETER Name
The name of the user to login automatically.
#>
function Set-AutologinUser {
param(
[string] $Name
)
2024-08-10 01:57:37 +00:00
Set-ItemProperty $logonPath -Name $autologinOption "1";
2024-08-09 02:19:49 +00:00
if (-not $Name) {
$Name = Get-SetupUser;
}
$options = @{
$domainOption = "";
$userOption = $Name;
$passwordOption = "";
};
foreach ($key in $options.Keys) {
2024-08-10 01:57:26 +00:00
Set-ItemProperty $logonPath -Name $key -Value $options[$key];
2024-08-09 02:19:49 +00:00
}
}
<#
.SYNOPSIS
Disables the automatic login.
#>
function Disable-Autologin {
Set-ItemProperty $logonPath -Name $autologinOption "0";
foreach ($key in @($domainOption, $userOption, $passwordOption)) {
Remove-ItemProperty $logonPath -Name $key -ErrorAction SilentlyContinue;
}
}
<#
.SYNOPSIS
Reboots the machine intermediately and restarts the setup after the next login.
#>
function Restart-Intermediate {
param(
[switch] $DefaultUser
)
2024-08-09 13:49:36 +00:00
$register = { param($UserKey) Register-Setup @PSBoundParameters; };
if ($DefaultUser) {
Edit-DefaultUserKey {
param(
[RegistryKey] $Key
)
2024-08-09 13:49:36 +00:00
& $register $Key;
}
2024-08-09 13:49:36 +00:00
} else {
& $register;
}
2024-08-09 02:42:55 +00:00
Restart-Computer -Force;
}
}