Nest installation functions in a module

This commit is contained in:
Manuel Thalmann 2024-08-01 19:02:12 +02:00
parent 5bb1a19a1f
commit 9b6cdd24e5

View file

@ -12,213 +12,215 @@
. "$PSScriptRoot/../Scripts/Update.ps1"; . "$PSScriptRoot/../Scripts/Update.ps1";
. "$PSScriptRoot/../Software/Firefox/Install.ps1"; . "$PSScriptRoot/../Software/Firefox/Install.ps1";
<# $null = New-Module {
.SYNOPSIS <#
Finishes the installation of a running Windows machine. .SYNOPSIS
#> Finishes the installation of a running Windows machine.
function Start-WindowsInstallation { #>
Start-InstallationLoop; function Start-WindowsInstallation {
} Start-InstallationLoop;
}
<# <#
.SYNOPSIS .SYNOPSIS
Starts the installation loop. Starts the installation loop.
#> #>
function Start-InstallationLoop { function Start-InstallationLoop {
while (-not (Get-IsFinished)) { while (-not (Get-IsFinished)) {
if ($null -eq (Get-Stage)) { if ($null -eq (Get-Stage)) {
Set-Stage ([SetupStage]::Initialize); Set-Stage ([SetupStage]::Initialize);
} elseif ((Get-Stage) -eq ([SetupStage]::Initialize)) { } elseif ((Get-Stage) -eq ([SetupStage]::Initialize)) {
if (-not ((Test-Command "choco") -and (Test-Command "refreshenv"))) { if (-not ((Test-Command "choco") -and (Test-Command "refreshenv"))) {
Invoke-Hook "Install-Chocolatey" -Fallback { Invoke-Hook "Install-Chocolatey" -Fallback {
# Install chocolatey # Install chocolatey
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072;
Invoke-Expression ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1')); Invoke-Expression ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'));
Import-Module $env:ChocolateyInstall/helpers/chocolateyProfile.psm1; Import-Module $env:ChocolateyInstall/helpers/chocolateyProfile.psm1;
refreshenv; refreshenv;
};
continue;
}
if (-not (Test-ChocoPackage "powershell-core")) {
Invoke-Hook "Install-PowerShellCore" -Fallback {
choco install -y powershell-core --install-arguments='"ADD_FILE_CONTEXT_MENU_RUNPOWERSHELL=1 ADD_EXPLORER_CONTEXT_MENU_OPENPOWERSHELL=1 REGISTER_MANIFEST=1 USER_MU=1 ENABLE_MU=1"';
};
Restart-Intermediate;
return;
}
Invoke-Hook "Install-PSModules" -Fallback {
Install-Module -AcceptLicense -Force PSWindowsUpdate;
}; };
continue; if (Test-Path $env:PWSH_PATH) {
} attrib "-R" "$env:PWSH_PATH\*" /S /D;
Remove-Item -Recurse -Force $env:PWSH_PATH;
continue;
}
if (-not (Test-ChocoPackage "powershell-core")) { if (-not (Test-Command winget)) {
Invoke-Hook "Install-PowerShellCore" -Fallback { . "$PSScriptRoot/../Software/winget/Manage.ps1";
choco install -y powershell-core --install-arguments='"ADD_FILE_CONTEXT_MENU_RUNPOWERSHELL=1 ADD_EXPLORER_CONTEXT_MENU_OPENPOWERSHELL=1 REGISTER_MANIFEST=1 USER_MU=1 ENABLE_MU=1"'; continue;
}; }
Restart-Intermediate; if (-not (& { wsl --status; $?; })) {
return; wsl --install --no-launch;
} Restart-Intermediate;
return;
} else {
ubuntu install --root;
wsl -- sh `<`(curl -L https://nixos.org/nix/install`) --daemon --yes;
wsl --shutdown;
}
Invoke-Hook "Install-PSModules" -Fallback { if (wsl --shell-type login type -t nix) {
Install-Module -AcceptLicense -Force PSWindowsUpdate; Set-Stage ([SetupStage]::Configure);
}; }
if (Test-Path $env:PWSH_PATH) {
attrib "-R" "$env:PWSH_PATH\*" /S /D;
Remove-Item -Recurse -Force $env:PWSH_PATH;
continue;
}
if (-not (Test-Command winget)) {
. "$PSScriptRoot/../Software/winget/Manage.ps1";
continue;
}
if (-not (& { wsl --status; $?; })) {
wsl --install --no-launch;
Restart-Intermediate;
return;
} else { } else {
ubuntu install --root; $null = Import-Module PSWindowsUpdate;
wsl -- sh `<`(curl -L https://nixos.org/nix/install`) --daemon --yes;
wsl --shutdown;
}
if (wsl --shell-type login type -t nix) { Invoke-Hook "Invoke-WindowsUpdate" -Fallback {
Set-Stage ([SetupStage]::Configure); Update-WindowsInstallation;
} };
} else {
$null = Import-Module PSWindowsUpdate;
Invoke-Hook "Invoke-WindowsUpdate" -Fallback { if ((Get-WURebootStatus -Silent)) {
Update-WindowsInstallation; Restart-Intermediate;
}; return;
}
if ((Get-WURebootStatus -Silent)) { switch (Get-Stage) {
Restart-Intermediate; ([SetupStage]::Configure) {
return; if (Get-Config "valhalla.windows.dualboot.enable") {
} # Fix synchronization between Linux and Windows clocks.
Set-ItemProperty "HKLM:\SYSTEM\CurrentControlSet\Control\TimeZoneInformation" -Name "RealTimeIsUniversal" -Value 1 -Type "DWord";
switch (Get-Stage) { # Force time resynchronization
([SetupStage]::Configure) { $service = Get-Service W32Time;
if (Get-Config "valhalla.windows.dualboot.enable") { $stopped = ($service.Status -eq "Stopped");
# Fix synchronization between Linux and Windows clocks. Start-Service $service;
Set-ItemProperty "HKLM:\SYSTEM\CurrentControlSet\Control\TimeZoneInformation" -Name "RealTimeIsUniversal" -Value 1 -Type "DWord"; w32tm /resync /force;
# Force time resynchronization if ($stopped) {
$service = Get-Service W32Time; Stop-Service $service;
$stopped = ($service.Status -eq "Stopped"); }
Start-Service $service;
w32tm /resync /force;
if ($stopped) {
Stop-Service $service;
} }
} }
([SetupStage]::Install) {
Write-Host "Entering install phase";
Set-IsFinished $true;
}
Default {}
} }
([SetupStage]::Install) {
Write-Host "Entering install phase";
Set-IsFinished $true;
}
Default {}
} }
} }
} }
}
function Invoke-WindowsInstallation([Context] $context) { function Invoke-WindowsInstallation([Context] $context) {
$Global:InformationPreference = "Continue"; $Global:InformationPreference = "Continue";
$Global:ErrorActionPreference = "Inquire"; $Global:ErrorActionPreference = "Inquire";
$context.UserNames ??= @("Manuel"); $context.UserNames ??= @("Manuel");
Start-OldWindowsInstallationScript $context; Start-OldWindowsInstallationScript $context;
} }
function Start-OldWindowsInstallationScript([Context] $context) { function Start-OldWindowsInstallationScript([Context] $context) {
. "$PSScriptRoot/Upgrade.ps1"; . "$PSScriptRoot/Upgrade.ps1";
$finished = $false; $finished = $false;
Remove-Item Env:\POSH_THEME -ErrorAction SilentlyContinue; Remove-Item Env:\POSH_THEME -ErrorAction SilentlyContinue;
$configPath = "$PSScriptRoot/../Config"; $configPath = "$PSScriptRoot/../Config";
$softwarePath = "$PSScriptRoot/../Software"; $softwarePath = "$PSScriptRoot/../Software";
$initialConfigStage = "InitialConfiguration"; $initialConfigStage = "InitialConfiguration";
$prerequisitesStage = "InstallationPrerequisites"; $prerequisitesStage = "InstallationPrerequisites";
$driverStage = "DriverInstallation"; $driverStage = "DriverInstallation";
$softwareStage = "MachineWideSoftwareInstallation"; $softwareStage = "MachineWideSoftwareInstallation";
$userStage = "CreatingUser"; $userStage = "CreatingUser";
$restorationStage = "Restoring"; $restorationStage = "Restoring";
while (-not $finished) { while (-not $finished) {
switch ($context.GetStage()) { switch ($context.GetStage()) {
{ (-not $_) -or ($_ -eq $initialConfigStage) } { { (-not $_) -or ($_ -eq $initialConfigStage) } {
$context.SetStage($initialConfigStage); $context.SetStage($initialConfigStage);
if ((Get-Command Initialize-Configuration -ErrorAction SilentlyContinue)) { if ((Get-Command Initialize-Configuration -ErrorAction SilentlyContinue)) {
Write-Information "Configuration initialization function was found. Running..."; Write-Information "Configuration initialization function was found. Running...";
Initialize-Configuration $context; 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
$null = Enable-WindowsOptionalFeature -Online -All -FeatureName "NetFx3"; default {
Write-Host "Starting Installation and Restoration of Windows";
. "$configPath/Windows/Install.ps1" $context; Update-WindowsInstallation $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;
} }
$prerequisitesStage {
Write-Host "Installing prerequisites for installing software";
Install-Module -AcceptLicense -Force "NuGet"; if (-not $(Get-Command winget)) {
Import-Module NuGet; choco install -y winget;
}
Install-Firefox $context; Install-Module -AcceptLicense -Force "NuGet";
choco install -y selenium-gecko-driver; Import-Module NuGet;
$null = Install-Package -Force Selenium.WebDriver -RequiredVersion 4.10.0 -SkipDependencies;
winget install --accept-source-agreements --accept-package-agreements -e --id AutoHotkey.AutoHotkey; Install-Firefox $context;
choco install -y selenium-gecko-driver;
$null = Install-Package -Force Selenium.WebDriver -RequiredVersion 4.10.0 -SkipDependencies;
$context.SetStage($driverStage); winget install --accept-source-agreements --accept-package-agreements -e --id AutoHotkey.AutoHotkey;
break;
}
$driverStage {
Write-Host "Installing drivers";
if ((Get-Command Install-PortValhallaDrivers -ErrorAction SilentlyContinue)) { $context.SetStage($driverStage);
Write-Information "Driver installation function was found. Starting installation"; break;
Install-PortValhallaDrivers $context;
} }
$driverStage {
Write-Host "Installing drivers";
Write-Information "Finished installing drivers"; if ((Get-Command Install-PortValhallaDrivers -ErrorAction SilentlyContinue)) {
$context.SetStage($softwareStage); Write-Information "Driver installation function was found. Starting installation";
$context.Reboot(); Install-PortValhallaDrivers $context;
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"; Write-Information "Finished installing drivers";
. "$softwarePath/aliae/Install.ps1" $context; $context.SetStage($softwareStage);
. "$softwarePath/posh-git/Install.ps1"; $context.Reboot();
. "$softwarePath/Terminal-Icons/Install.ps1"; exit;
. "$softwarePath/Oh My Posh/Install.ps1" $context; }
$context.SetStage($userStage); $softwareStage {
break; Write-Host "Setting up software with default app associations";
} . "$softwarePath/WinSCP/Install.ps1" $context;
$userStage { . "$softwarePath/Thunderbird/Install.ps1" $context;
Install-PersonalUsers $context;
$context.SetStage($restorationStage); Write-Host "Installing default settings for new users";
break; . "$softwarePath/aliae/Install.ps1" $context;
} . "$softwarePath/posh-git/Install.ps1";
$restorationStage { . "$softwarePath/Terminal-Icons/Install.ps1";
Restore-WindowsInstallation $context; . "$softwarePath/Oh My Posh/Install.ps1" $context;
$finished = $true; $context.SetStage($userStage);
break; break;
}
$userStage {
Install-PersonalUsers $context;
$context.SetStage($restorationStage);
break;
}
$restorationStage {
Restore-WindowsInstallation $context;
$finished = $true;
break;
}
} }
} }
} }
} };