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 #!/bin/pwsh
. "$PSScriptRoot/Drivers.ps1"; . "$PSScriptRoot/Drivers.ps1";
. "$PSScriptRoot/../../../scripts/Windows/Scripts/Context.ps1";
. "$PSScriptRoot/../../../scripts/Windows/Collections/Personal.ps1" . "$PSScriptRoot/../../../scripts/Windows/Collections/Personal.ps1"
. "$PSScriptRoot/../../../scripts/Windows/OS/Install.ps1";
. "$PSScriptRoot/../../../scripts/Windows/Scripts/Context.ps1";
function Initialize-Configuration { function Initialize-Configuration {
Set-ItemProperty "HKLM:\SYSTEM\CurrentControlSet\Control\TimeZoneInformation" -Name "RealTimeIsUniversal" -Value 1 -Type "DWord"; 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"; Write-Host "Starting Restoration of Windows";
. "$PSScriptRoot/../../../scripts/Windows/OS/Install.ps1";
[Context]$context = [Context]::new(); [Context]$context = [Context]::new();
$context.UserName = "Manuel"; $context.UserName = "Manuel";
Invoke-WindowsInstallation $context; Invoke-WindowsInstallation $context;

View file

@ -9,10 +9,26 @@ function Invoke-WindowsInstallation([Context] $context)
{ {
$ErrorActionPreference = "Inquire"; $ErrorActionPreference = "Inquire";
. "$PSScriptRoot/../Scripts/Prerequisites.ps1"; . "$PSScriptRoot/../Scripts/Prerequisites.ps1";
Start-WindowsInstallation $context;
}
function Start-WindowsInstallation([Context] $context) {
$finished = $false;
$configPath = "$PSScriptRoot/../Config"; $configPath = "$PSScriptRoot/../Config";
$softwarePath = "$PSScriptRoot/../Software"; $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)) { if ((Get-Command Initialize-Configuration -ErrorAction SilentlyContinue)) {
Initialize-Configuration $context; Initialize-Configuration $context;
} }
@ -25,13 +41,15 @@ function Invoke-WindowsInstallation([Context] $context)
. "$configPath/chocolatey/Install.ps1"; . "$configPath/chocolatey/Install.ps1";
$context.RemoveDesktopIcon("*Microsoft Edge*"); $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"; Write-Host "Starting Installation and Restoration of Windows";
Update-WindowsInstallation $context; Update-WindowsInstallation $context;
}
if (-not $context.Get("SoftwarePrerequisitesInstalled")) { $prerequisitesStage {
Write-Host "Installing prerequisites for installing software"; Write-Host "Installing prerequisites for installing software";
if (-not $(Get-Command winget)) { 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; winget install --accept-source-agreements --accept-package-agreements -e --id AutoHotkey.AutoHotkey;
$context.Set("SoftwarePrerequisitesInstalled", 1, "DWord"); $context.Set("SoftwarePrerequisitesInstalled", 1, "DWord");
break;
} }
$driverStage {
if (-not $context.Get("DriversInstalled")) { Write-Host "Installing drivers";
Write-Host "Installing Drivers";
Write-Information "Looking for driver installation function";
if ((Get-Command Install-PortValhallaDrivers -ErrorAction SilentlyContinue)) { if ((Get-Command Install-PortValhallaDrivers -ErrorAction SilentlyContinue)) {
Write-Information "Driver installation function was found. Starting installation"; Write-Information "Driver installation function was found. Starting installation";
@ -60,22 +77,33 @@ function Invoke-WindowsInstallation([Context] $context)
} }
Write-Information "Finished installing drivers"; Write-Information "Finished installing drivers";
$context.Set("DriversInstalled", 1, "DWord"); $context.SetStage("DriversInstalled", 1, "DWord");
$context.Reboot(); $context.Reboot();
exit; exit;
} }
$softwareStage {
if (-not $context.Get("MachineWideSoftware")) {
Write-Host "Setting up software with default app associations"; Write-Host "Setting up software with default app associations";
. "$softwarePath/aliae/Install.ps1" $context;
. "$softwarePath/WinSCP/Install.ps1" $context; . "$softwarePath/WinSCP/Install.ps1" $context;
. "$softwarePath/Thunderbird/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/posh-git/Install.ps1";
. "$softwarePath/Terminal-Icons/Install.ps1"; . "$softwarePath/Terminal-Icons/Install.ps1";
. "$softwarePath/Oh My Posh/Install.ps1" $context; . "$softwarePath/Oh My Posh/Install.ps1" $context;
$context.Set("MachineWideSoftware", 1, "DWord"); $context.SetStage($userStage);
break;
} }
$userStage {
New-PersonalUser $context; New-PersonalUser $context;
Restore-WindowsInstallation $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) 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)) if (-not (Get-LocalUser $context.UserName -ErrorAction SilentlyContinue))
{ {
Write-Host "Creating Personal User"; Write-Host "Creating Personal User";
@ -46,23 +60,24 @@ function New-PersonalUser([Context] $context)
Write-Information "Disabling Auto login"; Write-Information "Disabling Auto login";
$context.RemoveAutologin(); $context.RemoveAutologin();
$context.SetStage("DisableUAC"); Set-UserStage "DisableUAC";
Restart-Computer -Force; Restart-Computer -Force;
exit; exit;
} }
elseif ($context.GetStage() -eq "DisableUAC")
{ switch (Get-UserStage) {
"DisableUAC" {
Enable-PersonalUserAutologon $context; Enable-PersonalUserAutologon $context;
$context.RegisterReboot(); $context.RegisterReboot();
$context.SetStage("RemoveAdmin"); Set-UserStage "RemoveAdmin";
Start-EventDrivenTask $context.Get($uacDisablerTriggerProperty); Start-EventDrivenTask $context.Get($uacDisablerTriggerProperty);
exit; exit;
} }
elseif ($context.GetStage() -eq "RemoveAdmin") "RemoveAdmin" {
{
Write-Information "Removing Admin Account"; Write-Information "Removing Admin Account";
Get-CimInstance -ClassName "Win32_UserProfile" -Filter "SID = '$((Get-LocalUser $context.AdminName).SID)'" | Remove-CimInstance; Get-CimInstance -ClassName "Win32_UserProfile" -Filter "SID = '$((Get-LocalUser $context.AdminName).SID)'" | Remove-CimInstance;
$context.RemoveStage(); $context.Remove($userStageProperty);
}
} }
} }