Install Windows based on installation stages

This commit is contained in:
Manuel Thalmann 2024-03-23 15:09:58 +01:00
parent f6baf1a9dc
commit 5245bb9007
3 changed files with 123 additions and 80 deletions

View file

@ -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;

View file

@ -9,10 +9,26 @@ 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";
while (-not $finished) {
switch ($context.GetStage()) {
{ $_ -in $null,$initialConfigStage } {
$context.SetStage($initialConfigStage);
if (-not $context.Get("InitialConfiguration")) {
if ((Get-Command Initialize-Configuration -ErrorAction SilentlyContinue)) {
Initialize-Configuration $context;
}
@ -25,13 +41,15 @@ function Invoke-WindowsInstallation([Context] $context)
. "$configPath/chocolatey/Install.ps1";
$context.RemoveDesktopIcon("*Microsoft Edge*");
$context.Set("InitialConfiguration", 1, "DWord");
$context.SetStage($prerequisitesStage);
break;
}
# Always install updates
default {
Write-Host "Starting Installation and Restoration of Windows";
Update-WindowsInstallation $context;
if (-not $context.Get("SoftwarePrerequisitesInstalled")) {
}
$prerequisitesStage {
Write-Host "Installing prerequisites for installing software";
if (-not $(Get-Command winget)) {
@ -48,11 +66,10 @@ function Invoke-WindowsInstallation([Context] $context)
winget install --accept-source-agreements --accept-package-agreements -e --id AutoHotkey.AutoHotkey;
$context.Set("SoftwarePrerequisitesInstalled", 1, "DWord");
break;
}
if (-not $context.Get("DriversInstalled")) {
Write-Host "Installing Drivers";
Write-Information "Looking for driver installation function";
$driverStage {
Write-Host "Installing drivers";
if ((Get-Command Install-PortValhallaDrivers -ErrorAction SilentlyContinue)) {
Write-Information "Driver installation function was found. Starting installation";
@ -60,22 +77,33 @@ function Invoke-WindowsInstallation([Context] $context)
}
Write-Information "Finished installing drivers";
$context.Set("DriversInstalled", 1, "DWord");
$context.SetStage("DriversInstalled", 1, "DWord");
$context.Reboot();
exit;
}
if (-not $context.Get("MachineWideSoftware")) {
$softwareStage {
Write-Host "Setting up software with default app associations";
. "$softwarePath/aliae/Install.ps1" $context;
. "$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.Set("MachineWideSoftware", 1, "DWord");
$context.SetStage($userStage);
break;
}
$userStage {
New-PersonalUser $context;
$context.SetStage($restorationStage);
break;
}
$restorationStage {
Restore-WindowsInstallation $context;
$finished = $true;
break;
}
}
}
}

View file

@ -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")
{
switch (Get-UserStage) {
"DisableUAC" {
Enable-PersonalUserAutologon $context;
$context.RegisterReboot();
$context.SetStage("RemoveAdmin");
Set-UserStage "RemoveAdmin";
Start-EventDrivenTask $context.Get($uacDisablerTriggerProperty);
exit;
}
elseif ($context.GetStage() -eq "RemoveAdmin")
{
"RemoveAdmin" {
Write-Information "Removing Admin Account";
Get-CimInstance -ClassName "Win32_UserProfile" -Filter "SID = '$((Get-LocalUser $context.AdminName).SID)'" | Remove-CimInstance;
$context.RemoveStage();
$context.Remove($userStageProperty);
}
}
}