PortValhalla/scripts/Windows/OS/Manage.ps1

353 lines
17 KiB
PowerShell
Raw Normal View History

2023-07-12 20:37:31 +00:00
#!/bin/pwsh
2024-08-09 02:19:49 +00:00
using namespace System.Security.Principal;
. "$PSScriptRoot/../../../scripts/Windows/Scripts/Prerequisites.ps1";
. "$PSScriptRoot/../../../scripts/Windows/Scripts/WSL.ps1";
2024-08-07 19:05:32 +00:00
. "$PSScriptRoot/../../Common/Scripts/Context.ps1";
. "$PSScriptRoot/../Scripts/Security.ps1";
. "$PSScriptRoot/../Software/Firefox/Install.ps1";
2024-08-27 21:08:41 +00:00
. "$PSScriptRoot/Legacy.ps1";
2024-03-23 14:38:06 +00:00
. "$PSScriptRoot/User/Install.ps1";
2023-06-16 18:24:22 +00:00
$null = New-Module {
. "$PSScriptRoot/../Scripts/Deployment.ps1";
2024-08-01 19:49:46 +00:00
. "$PSScriptRoot/../Scripts/Hooks.ps1";
. "$PSScriptRoot/../Scripts/PowerManagement.ps1";
2024-08-09 02:19:49 +00:00
. "$PSScriptRoot/../Scripts/Registry.ps1";
2024-08-09 14:02:33 +00:00
. "$PSScriptRoot/../Scripts/Security.ps1";
2024-08-01 19:49:46 +00:00
. "$PSScriptRoot/../Scripts/Update.ps1";
2024-08-08 15:49:43 +00:00
. "$PSScriptRoot/../Scripts/Users.ps1";
. "$PSScriptRoot/../Types/WindowsInstallerAction.ps1";
2024-08-07 19:05:32 +00:00
. "$PSScriptRoot/../../Common/Scripts/Config.ps1";
. "$PSScriptRoot/../../Common/Scripts/Operations.ps1";
. "$PSScriptRoot/../../Common/Scripts/Scripting.ps1";
2024-08-07 19:05:32 +00:00
. "$PSScriptRoot/../../Common/Scripts/Software.ps1";
. "$PSScriptRoot/../../Common/Scripts/SoftwareManagement.ps1";
. "$PSScriptRoot/../../Common/Types/InstallerAction.ps1";
<#
.SYNOPSIS
Finishes the installation of a running Windows machine.
#>
function Start-WindowsInstallation {
2024-08-24 14:52:41 +00:00
Start-Operation -NoImplicitCleanup {
Start-InstallationLoop ([WindowsInstallerAction]::Install);
};
}
2024-09-09 22:37:13 +00:00
<#
.SYNOPSIS
Creates a backup of the current Windows machine.
#>
function Start-WindowsBackup {
Start-Operation -NoImplicitCleanup {
Start-InstallationLoop ([WindowsInstallerAction]::Backup);
};
}
<#
.SYNOPSIS
Starts the installation loop.
#>
function Start-InstallationLoop {
param(
[WindowsInstallerAction] $Action
)
while ((Get-Stage) -ne ([WindowsInstallerStage]::Completed)) {
2024-08-27 12:05:16 +00:00
switch (Get-Stage) {
($null) {
Set-Stage ([WindowsInstallerStage]::Initialize);
2024-08-27 12:05:16 +00:00
break;
}
([WindowsInstallerStage]::Initialize) {
2024-08-28 02:40:32 +00:00
$env:BACKUP_ARCHIVE = pwsh -Command Write-Host (
Read-Host (
& {
switch ($Action) {
([WindowsInstallerAction]::Backup) {
"Please select the path you wish to store your backup at"
}
([WindowsInstallerAction]::Install) {
"Please select an archive you wish to restore from, if you wish to restore from a backup"
}
}
}));
Set-Stage ([WindowsInstallerStage]::Run);
break;
}
([WindowsInstallerStage]::Run) {
switch ($Action) {
([WindowsInstallerAction]::Install) {
while (-not (Get-IsFinished)) {
if (Test-Admin) {
$null = Import-Module PSWindowsUpdate;
2024-08-27 12:05:16 +00:00
Invoke-Hook "Invoke-WindowsUpdate" -Fallback {
Update-WindowsInstallation;
};
2024-08-27 12:05:16 +00:00
if ((Get-WURebootStatus -Silent)) {
Restart-Intermediate;
return;
}
}
2024-08-27 12:05:16 +00:00
switch (Get-SetupStage) {
($null) {
2024-09-09 22:37:13 +00:00
Set-SetupStage ([SetupStage]::Initialize);
break;
}
2024-09-09 22:37:13 +00:00
([SetupStage]::Initialize) {
Set-SetupStage ([SetupStage]::Configure);
}
([SetupStage]::Configure) {
if (Get-Config "valhalla.windows.dualboot.enable") {
if (-not (Test-Qemu)) {
# Fix synchronization between Linux and Windows clocks.
Set-ItemProperty "HKLM:\SYSTEM\CurrentControlSet\Control\TimeZoneInformation" -Name "RealTimeIsUniversal" -Value 1 -Type "DWord";
}
# Force time resynchronization
$timeZoneOption = "Start";
$timeZoneKey = "HKLM:\SYSTEM\CurrentControlSet\Services\tzautoupdate";
$service = Get-Service W32Time;
$autoUpdate = (Get-Item $timeZoneKey).GetValue($timeZoneOption);
$stopped = ($service.Status -eq "Stopped");
$setUpdate = { param([int] $Value) Set-ItemProperty $timeZoneKey -Name $timeZoneOption $Value };
& $setUpdate 3;
Start-Service $service;
w32tm /resync /force;
& $setUpdate $autoUpdate;
if ($stopped) {
Stop-Service $service;
}
}
Set-SetupStage ([SetupStage]::Install);
}
([SetupStage]::Install) {
Write-Host "Entering install phase";
Deploy-SoftwareAction;
Set-SetupStage ([SetupStage]::CreateUser);
2024-09-12 17:04:48 +00:00
Restart-Intermediate;
return;
}
([SetupStage]::CreateUser) {
Install-ValhallaUsers;
Set-IsFinished $true;
}
}
}
}
2024-09-09 22:37:13 +00:00
([WindowsInstallerAction]::Backup) {
$finished = $false;
$setupUser = Get-SetupUser;
$adminGroup = @{
SID = [SecurityIdentifier]::new([WellKnownSidType]::BuiltinAdministratorsSid, $null);
};
while (-not $finished) {
switch (Get-BackupStage) {
$null {
Set-BackupStage ([BackupStage]::Initialize);
}
([BackupStage]::Initialize) {
$null = New-LocalUser $setupUser -NoPassword;
Set-LocalUser $setupUser -PasswordNeverExpires $true;
Set-LocalUser $setupUser -PasswordNeverExpires $false;
Add-LocalGroupMember -Member $setupUser @adminGroup;
Set-AutologinUser $setupUser;
Disable-UAC;
Set-BackupStage ([BackupStage]::Backup);
Restart-Intermediate;
return;
}
([BackupStage]::Backup) {
Deploy-SoftwareAction ([InstallerAction]::Backup);
Set-BackupStage ([BackupStage]::BackupUsers);
}
([BackupStage]::BackupUsers) {
$users = @(Get-Users);
$i = Get-CurrentUser;
Disable-LocalUser $setupUser;
for (; $i -lt $users.Count; $i++) {
Set-CurrentUser $i;
$user = $users[$i];
if ($env:UserName -ne $user) {
Set-BootMessage "Please Log In" "Please log in with the user ``$user``";
Add-LocalGroupMember -Member "$user" @adminGroup -ErrorAction SilentlyContinue;
Disable-Autologin;
Restart-Intermediate;
return;
} else {
Deploy-SoftwareAction -Action ([InstallerAction]::BackupUser);
Remove-LocalGroupMember -Member "$user" @adminGroup;
foreach ($group in Get-UserConfig -UserName "$user" "groups") {
Add-LocalGroupMember -Member "$user" $group;
}
}
}
Disable-BootMessage;
2024-09-09 22:38:35 +00:00
Write-Host "Never forget to store the backup somewhere safe!";
Write-Host "I mean… what kind of a dumbass would ever forget to do so, right?";
Read-Host "Press enter once you're done";
2024-09-09 22:37:13 +00:00
$finished = $true;
}
}
}
}
2024-08-27 12:05:16 +00:00
}
Set-Stage ([WindowsInstallerStage]::Cleanup);
break;
}
([WindowsInstallerStage]::Cleanup) {
$taskName = "PortValhalla Cleaner";
$setupUser = Get-SetupUser;
Clear-OperationResources;
Remove-Item -Recurse -Force "C:\ProgramData\PortValhalla";
Get-SetupConfigKey | Remove-Item -Recurse -Force;
Disable-Autologin;
Disable-LocalUser $setupUser;
$script = {
param(
$TaskName,
$UserName
)
$user = Get-LocalUser $UserName;
[string] $sid = $user.SID;
Remove-LocalUser $user;
Get-CimInstance Win32_UserProfile | Where-Object { $_.SID -eq $sid } | Remove-CimInstance;
Unregister-ScheduledTask -Confirm:$false $TaskName;
};
$trigger = New-ScheduledTaskTrigger -AtStartup;
$task = New-ScheduledTaskAction -Execute "pwsh" -Argument "-Command & { $script } $(ConvertTo-Injection $taskName) $(ConvertTo-Injection $setupUser)";
$null = Register-ScheduledTask -Force $taskName -Action $task -Trigger $trigger -RunLevel Highest -User "SYSTEM";
Set-Stage ([WindowsInstallerStage]::Completed);
Enable-UAC;
Restart-Intermediate -NoRegister;
break;
2024-08-01 14:32:03 +00:00
}
}
}
}
function Invoke-WindowsInstallation([Context] $context) {
$Global:InformationPreference = "Continue";
$Global:ErrorActionPreference = "Inquire";
$context.UserNames ??= @("Manuel");
Start-OldWindowsInstallationScript $context;
}
function Start-OldWindowsInstallationScript([Context] $context) {
. "$PSScriptRoot/Upgrade.ps1";
$finished = $false;
Remove-Item Env:\POSH_THEME -ErrorAction SilentlyContinue;
$configPath = "$PSScriptRoot/../Config";
$softwarePath = "$PSScriptRoot/../Software";
$initialConfigStage = "InitialConfiguration";
$prerequisitesStage = "InstallationPrerequisites";
$driverStage = "DriverInstallation";
$softwareStage = "MachineWideSoftwareInstallation";
$userStage = "CreatingUser";
$restorationStage = "Restoring";
while (-not $finished) {
switch ($context.GetStage()) {
{ (-not $_) -or ($_ -eq $initialConfigStage) } {
$context.SetStage($initialConfigStage);
if ((Get-Command Initialize-Configuration -ErrorAction SilentlyContinue)) {
Write-Information "Configuration initialization function was found. Running...";
Initialize-Configuration $context;
}
$null = Enable-WindowsOptionalFeature -Online -All -FeatureName "NetFx3";
. "$configPath/Windows/Install.ps1" $context;
. "$configPath/Explorer/Install.ps1" $context;
. "$configPath/OpenSSH/Install.ps1" $context;
. "$configPath/chocolatey/Install.ps1";
$context.RemoveDesktopIcon("*Microsoft Edge*");
$context.SetStage($prerequisitesStage);
break;
}
# Always install updates
default {
Write-Host "Starting Installation and Restoration of Windows";
Update-WindowsInstallation $context;
}
$prerequisitesStage {
Write-Host "Installing prerequisites for installing software";
if (-not $(Get-Command winget)) {
choco install -y winget;
}
Install-Module -AcceptLicense -Force "NuGet";
Import-Module NuGet;
Install-Firefox $context;
choco install -y selenium-gecko-driver;
$null = Install-Package -Force Selenium.WebDriver -RequiredVersion 4.10.0 -SkipDependencies;
winget install --accept-source-agreements --accept-package-agreements -e --id AutoHotkey.AutoHotkey;
$context.SetStage($driverStage);
break;
}
$driverStage {
Write-Host "Installing drivers";
if ((Get-Command Install-PortValhallaDrivers -ErrorAction SilentlyContinue)) {
Write-Information "Driver installation function was found. Starting installation";
Install-PortValhallaDrivers $context;
}
Write-Information "Finished installing drivers";
$context.SetStage($softwareStage);
$context.Reboot();
exit;
}
$softwareStage {
Write-Host "Setting up software with default app associations";
. "$softwarePath/WinSCP/Install.ps1" $context;
. "$softwarePath/Thunderbird/Install.ps1" $context;
Write-Host "Installing default settings for new users";
. "$softwarePath/aliae/Install.ps1" $context;
. "$softwarePath/posh-git/Install.ps1";
. "$softwarePath/Terminal-Icons/Install.ps1";
. "$softwarePath/Oh My Posh/Install.ps1" $context;
$context.SetStage($userStage);
break;
}
$userStage {
Install-PersonalUsers $context;
$context.SetStage($restorationStage);
break;
}
$restorationStage {
Restore-WindowsInstallation $context;
$finished = $true;
break;
}
}
2023-08-13 23:32:57 +00:00
}
2023-07-16 10:56:54 +00:00
}
};