2024-07-31 13:41:45 +00:00
|
|
|
using namespace Microsoft.Win32;
|
|
|
|
|
|
|
|
$null = New-Module {
|
2024-08-09 12:44:57 +00:00
|
|
|
. "$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";
|
2024-07-31 13:41:45 +00:00
|
|
|
[RegistryKey] $key = $null;
|
2024-08-09 12:44:57 +00:00
|
|
|
$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"
|
2024-08-01 00:19:05 +00:00
|
|
|
$runOnceName = "PortValhalla";
|
2024-08-09 02:19:49 +00:00
|
|
|
$autologinOption = "AutoAdminLogon";
|
|
|
|
$domainOption = "DefaultDomainName";
|
|
|
|
$userOption = "DefaultUserName";
|
|
|
|
$passwordOption = "DefaultPassword";
|
2024-07-31 13:41:45 +00:00
|
|
|
|
|
|
|
<#
|
|
|
|
.SYNOPSIS
|
|
|
|
Gets the reghistry key containing the `RunOnce` commands.
|
|
|
|
#>
|
|
|
|
function Get-RunOnceKey {
|
2024-08-09 12:44:57 +00:00
|
|
|
param(
|
|
|
|
[RegistryKey] $UserKey
|
|
|
|
)
|
|
|
|
|
|
|
|
[string] $path = $null;
|
|
|
|
|
|
|
|
if ($UserKey) {
|
2024-08-18 23:38:25 +00:00
|
|
|
$path = Join-Path ($UserKey.PSPath) $runOncePath;
|
2024-08-09 12:44:57 +00:00
|
|
|
} else {
|
|
|
|
$path = $systemRunOncePath;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (-not (Test-Path $path)) {
|
|
|
|
New-Item $path;
|
|
|
|
} else {
|
|
|
|
Get-Item $path;
|
|
|
|
}
|
2024-07-31 13:41:45 +00:00
|
|
|
}
|
|
|
|
|
2024-08-09 21:23:29 +00:00
|
|
|
<#
|
|
|
|
.SYNOPSIS
|
|
|
|
Generates a script for executing the installer.
|
|
|
|
#>
|
|
|
|
function Get-StartupScript {
|
2024-08-21 16:26:58 +00:00
|
|
|
"pwsh -Command " + (Get-StartupCommand);
|
2024-08-09 21:23:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
<#
|
|
|
|
.SYNOPSIS
|
2024-08-21 16:26:58 +00:00
|
|
|
Generates a command for running the installer using `pwsh`.
|
2024-08-09 21:23:29 +00:00
|
|
|
#>
|
2024-08-21 16:26:58 +00:00
|
|
|
function Get-StartupCommand {
|
2024-08-16 13:16:56 +00:00
|
|
|
($env:PWSH_PATH ? "`$env:PWSH_PATH = $(ConvertTo-Injection $env:PWSH_PATH);" : "") +
|
2024-08-16 14:10:09 +00:00
|
|
|
($env:DEBUG ? "`$env:DEBUG = $([int]$env:DEBUG);" : "") +
|
2024-09-09 22:37:13 +00:00
|
|
|
($env:BACKUP_ARCHIVE ? "`$env:BACKUP_ARCHIVE = $(ConvertTo-Injection ([System.IO.Path]::GetFullPath($env:BACKUP_ARCHIVE)));" : "") +
|
2024-08-09 21:23:29 +00:00
|
|
|
"`$env:INSTALLER_SCRIPT = $(ConvertTo-Injection (Resolve-Path $env:INSTALLER_SCRIPT));" +
|
2024-09-08 15:09:01 +00:00
|
|
|
"`$env:CONFIG_NAME = $(ConvertTo-Injection $env:CONFIG_NAME);" +
|
2024-08-09 21:23:29 +00:00
|
|
|
"& `$env:INSTALLER_SCRIPT;";
|
|
|
|
}
|
|
|
|
|
2024-07-31 13:41:45 +00:00
|
|
|
<#
|
|
|
|
.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(
|
2024-08-09 12:44:57 +00:00
|
|
|
[Parameter(ParameterSetName="System")]
|
|
|
|
[switch] $System,
|
2024-08-21 16:20:11 +00:00
|
|
|
[Parameter(ParameterSetName="DefaultUser", Mandatory)]
|
|
|
|
[switch] $DefaultUser,
|
2024-08-10 01:40:37 +00:00
|
|
|
[Parameter(ParameterSetName="User", Mandatory)]
|
2024-08-09 12:44:57 +00:00
|
|
|
[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)]
|
2024-07-31 13:41:45 +00:00
|
|
|
[RegistryKey] $UserKey
|
|
|
|
)
|
|
|
|
|
2024-08-21 16:20:11 +00:00
|
|
|
if ($DefaultUser.IsPresent) {
|
|
|
|
Edit-DefaultUserKey {
|
|
|
|
param(
|
|
|
|
[RegistryKey] $Key
|
|
|
|
)
|
|
|
|
|
|
|
|
Register-Setup -UserKey $Key;
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-08-10 01:40:37 +00:00
|
|
|
if ($User.IsPresent -or $UserKey) {
|
2024-08-09 12:44:57 +00:00
|
|
|
if (-not $UserKey) {
|
|
|
|
$UserKey = Get-Item "HKCU:\";
|
|
|
|
}
|
|
|
|
|
|
|
|
$key = Get-RunOnceKey $UserKey;
|
|
|
|
} else {
|
|
|
|
$key = Get-RunOnceKey;
|
|
|
|
}
|
2024-07-31 13:41:45 +00:00
|
|
|
|
2024-08-09 21:23:29 +00:00
|
|
|
Set-ItemProperty -Path $key.PSPath -Name $runOnceName -Type "ExpandString" -Value (Get-StartupScript);
|
2024-07-31 13:41:45 +00:00
|
|
|
$key.Handle.Close();
|
|
|
|
}
|
|
|
|
|
2024-08-10 13:24:04 +00:00
|
|
|
<#
|
|
|
|
.SYNOPSIS
|
|
|
|
Clears leftovers from past registrations.
|
|
|
|
#>
|
|
|
|
function Clear-SetupRegistration {
|
|
|
|
Edit-DefaultUserKey {
|
|
|
|
param(
|
|
|
|
[RegistryKey] $Key
|
|
|
|
)
|
|
|
|
|
|
|
|
$runOnceKey = Get-RunOnceKey $Key;
|
|
|
|
Remove-Item $runOnceKey.PSPath;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-31 13:41:45 +00:00
|
|
|
<#
|
|
|
|
.SYNOPSIS
|
|
|
|
Reboots the machine intermediately and restarts the setup after the next login.
|
|
|
|
#>
|
|
|
|
function Restart-Intermediate {
|
2024-08-09 12:44:57 +00:00
|
|
|
param(
|
2024-08-27 01:11:35 +00:00
|
|
|
[Parameter(ParameterSetName="None")]
|
|
|
|
[switch] $NoRegister,
|
|
|
|
[Parameter(ParameterSetName="Default", Mandatory)]
|
2024-08-18 23:37:14 +00:00
|
|
|
[switch] $DefaultUser,
|
|
|
|
[Parameter(ParameterSetName="Current", Mandatory)]
|
|
|
|
[switch] $CurrentUser
|
2024-08-09 12:44:57 +00:00
|
|
|
)
|
|
|
|
|
2024-08-27 01:11:35 +00:00
|
|
|
if (-not $NoRegister.IsPresent) {
|
|
|
|
if ($DefaultUser.IsPresent) {
|
|
|
|
Register-Setup -DefaultUser;
|
|
|
|
} elseif ($CurrentUser.IsPresent) {
|
|
|
|
Register-Setup -User;
|
|
|
|
} else {
|
|
|
|
Register-Setup;
|
|
|
|
}
|
2024-08-09 12:44:57 +00:00
|
|
|
}
|
|
|
|
|
2024-09-12 17:04:37 +00:00
|
|
|
if ($env:DEBUG -and (-not $Global:NonInteractive)) {
|
|
|
|
Read-Host "Press enter to reboot";
|
|
|
|
}
|
|
|
|
|
2024-08-09 02:42:55 +00:00
|
|
|
Restart-Computer -Force;
|
2024-07-31 13:41:45 +00:00
|
|
|
}
|
|
|
|
}
|