From 5245bb90077ae8d14bdda946f4880212a9621fff Mon Sep 17 00:00:00 2001 From: Manuel Thalmann Date: Sat, 23 Mar 2024 15:09:58 +0100 Subject: [PATCH] Install Windows based on installation stages --- profiles/DerGeret/Windows/Restore.ps1 | 4 +- scripts/Windows/OS/Install.ps1 | 156 +++++++++++++++----------- scripts/Windows/OS/User/Add.ps1 | 43 ++++--- 3 files changed, 123 insertions(+), 80 deletions(-) diff --git a/profiles/DerGeret/Windows/Restore.ps1 b/profiles/DerGeret/Windows/Restore.ps1 index fc49ed6f..6ed4c48d 100644 --- a/profiles/DerGeret/Windows/Restore.ps1 +++ b/profiles/DerGeret/Windows/Restore.ps1 @@ -1,7 +1,8 @@ #!/bin/pwsh . "$PSScriptRoot/Drivers.ps1"; -. "$PSScriptRoot/../../../scripts/Windows/Scripts/Context.ps1"; . "$PSScriptRoot/../../../scripts/Windows/Collections/Personal.ps1" +. "$PSScriptRoot/../../../scripts/Windows/OS/Install.ps1"; +. "$PSScriptRoot/../../../scripts/Windows/Scripts/Context.ps1"; function Initialize-Configuration { Set-ItemProperty "HKLM:\SYSTEM\CurrentControlSet\Control\TimeZoneInformation" -Name "RealTimeIsUniversal" -Value 1 -Type "DWord"; @@ -13,7 +14,6 @@ function Restore-Apps { } Write-Host "Starting Restoration of Windows"; -. "$PSScriptRoot/../../../scripts/Windows/OS/Install.ps1"; [Context]$context = [Context]::new(); $context.UserName = "Manuel"; Invoke-WindowsInstallation $context; diff --git a/scripts/Windows/OS/Install.ps1 b/scripts/Windows/OS/Install.ps1 index 4fbbc4a4..47a64101 100644 --- a/scripts/Windows/OS/Install.ps1 +++ b/scripts/Windows/OS/Install.ps1 @@ -9,73 +9,101 @@ function Invoke-WindowsInstallation([Context] $context) { $ErrorActionPreference = "Inquire"; . "$PSScriptRoot/../Scripts/Prerequisites.ps1"; + Start-WindowsInstallation $context; +} + +function Start-WindowsInstallation([Context] $context) { + $finished = $false; $configPath = "$PSScriptRoot/../Config"; $softwarePath = "$PSScriptRoot/../Software"; + $initialConfigStage = "InitialConfiguration"; + $prerequisitesStage = "InstallationPrerequisites"; + $driverStage = "DriverInstallation"; + $softwareStage = "MachineWideSoftwareInstallation"; + $userStage = "CreatingUser"; + $restorationStage = "Restoring"; - if (-not $context.Get("InitialConfiguration")) { - if ((Get-Command Initialize-Configuration -ErrorAction SilentlyContinue)) { - Initialize-Configuration $context; + + while (-not $finished) { + switch ($context.GetStage()) { + { $_ -in $null,$initialConfigStage } { + $context.SetStage($initialConfigStage); + + if ((Get-Command Initialize-Configuration -ErrorAction SilentlyContinue)) { + 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.Set("SoftwarePrerequisitesInstalled", 1, "DWord"); + 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("DriversInstalled", 1, "DWord"); + $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 { + New-PersonalUser $context; + $context.SetStage($restorationStage); + break; + } + $restorationStage { + Restore-WindowsInstallation $context; + $finished = $true; + break; + } } - - $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.Set("InitialConfiguration", 1, "DWord"); } - - Write-Host "Starting Installation and Restoration of Windows"; - Update-WindowsInstallation $context; - - if (-not $context.Get("SoftwarePrerequisitesInstalled")) { - 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.Set("SoftwarePrerequisitesInstalled", 1, "DWord"); - } - - if (-not $context.Get("DriversInstalled")) { - Write-Host "Installing Drivers"; - Write-Information "Looking for driver installation function"; - - 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.Set("DriversInstalled", 1, "DWord"); - $context.Reboot(); - exit; - } - - if (-not $context.Get("MachineWideSoftware")) { - Write-Host "Setting up software with default app associations"; - . "$softwarePath/aliae/Install.ps1" $context; - . "$softwarePath/WinSCP/Install.ps1" $context; - . "$softwarePath/Thunderbird/Install.ps1" $context; - . "$softwarePath/posh-git/Install.ps1"; - . "$softwarePath/Terminal-Icons/Install.ps1"; - . "$softwarePath/Oh My Posh/Install.ps1" $context; - $context.Set("MachineWideSoftware", 1, "DWord"); - } - - New-PersonalUser $context; - Restore-WindowsInstallation $context; } diff --git a/scripts/Windows/OS/User/Add.ps1 b/scripts/Windows/OS/User/Add.ps1 index 4f206c23..d5735f6a 100644 --- a/scripts/Windows/OS/User/Add.ps1 +++ b/scripts/Windows/OS/User/Add.ps1 @@ -8,6 +8,20 @@ $uacDisablerTriggerProperty = "UACDisablerTrigger"; function New-PersonalUser([Context] $context) { + $userStageProperty = "UserStage"; + + $null = New-Module { + + Get-UserStage { + return $context.Get($userStageProperty); + } + + Set-UserStage { + param([string]$value); + $context.Set($userStageProperty, $value); + } + } + if (-not (Get-LocalUser $context.UserName -ErrorAction SilentlyContinue)) { Write-Host "Creating Personal User"; @@ -46,23 +60,24 @@ function New-PersonalUser([Context] $context) Write-Information "Disabling Auto login"; $context.RemoveAutologin(); - $context.SetStage("DisableUAC"); + Set-UserStage "DisableUAC"; Restart-Computer -Force; exit; } - elseif ($context.GetStage() -eq "DisableUAC") - { - Enable-PersonalUserAutologon $context; - $context.RegisterReboot(); - $context.SetStage("RemoveAdmin"); - Start-EventDrivenTask $context.Get($uacDisablerTriggerProperty); - exit; - } - elseif ($context.GetStage() -eq "RemoveAdmin") - { - Write-Information "Removing Admin Account"; - Get-CimInstance -ClassName "Win32_UserProfile" -Filter "SID = '$((Get-LocalUser $context.AdminName).SID)'" | Remove-CimInstance; - $context.RemoveStage(); + + switch (Get-UserStage) { + "DisableUAC" { + Enable-PersonalUserAutologon $context; + $context.RegisterReboot(); + Set-UserStage "RemoveAdmin"; + Start-EventDrivenTask $context.Get($uacDisablerTriggerProperty); + exit; + } + "RemoveAdmin" { + Write-Information "Removing Admin Account"; + Get-CimInstance -ClassName "Win32_UserProfile" -Filter "SID = '$((Get-LocalUser $context.AdminName).SID)'" | Remove-CimInstance; + $context.Remove($userStageProperty); + } } }