From 12c449eb7cf19f22f2cb2ab58e6aa8a6ed5e3c31 Mon Sep 17 00:00:00 2001 From: Manuel Thalmann Date: Thu, 1 Aug 2024 02:59:32 +0200 Subject: [PATCH] Add scripts for updating windows --- scripts/Windows/OS/Install.ps1 | 35 ++++++++++++++++++++---------- scripts/Windows/Scripts/Update.ps1 | 34 +++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 11 deletions(-) create mode 100644 scripts/Windows/Scripts/Update.ps1 diff --git a/scripts/Windows/OS/Install.ps1 b/scripts/Windows/OS/Install.ps1 index dea1ee9e..31a49a80 100644 --- a/scripts/Windows/OS/Install.ps1 +++ b/scripts/Windows/OS/Install.ps1 @@ -2,13 +2,13 @@ . "$PSScriptRoot/../Scripts/Prerequisites.ps1"; . "$PSScriptRoot/Manage.ps1"; -. "$PSScriptRoot/Upgrade.ps1"; . "$PSScriptRoot/User/Install.ps1"; . "$PSScriptRoot/../Scripts/Choco.ps1"; . "$PSScriptRoot/../Scripts/Context.ps1"; . "$PSScriptRoot/../Scripts/Hooks.ps1"; . "$PSScriptRoot/../Scripts/PowerManagement.ps1"; . "$PSScriptRoot/../Scripts/SetupConfig.ps1"; +. "$PSScriptRoot/../Scripts/Update.ps1"; . "$PSScriptRoot/../Software/Firefox/Install.ps1"; <# @@ -24,15 +24,9 @@ function Start-WindowsInstallation { Starts the installation loop. #> function Start-InstallationLoop { - . "$PSScriptRoot/../Scripts/Choco.ps1"; - . "$PSScriptRoot/../Scripts/Hooks.ps1"; - . "$PSScriptRoot/../Scripts/PowerManagement.ps1"; - . "$PSScriptRoot/../Scripts/SetupConfig.ps1"; - while (-not (Get-IsFinished)) { - if (-not (Get-Stage)) { - Set-Stage [SetupStage]::Initialize; - break; + if ($null -eq (Get-Stage)) { + Set-Stage ([SetupStage]::Initialize); } elseif ((Get-Stage) -eq ([SetupStage]::Initialize)) { if (-not ((Get-Command "choco") -and (Get-Command "refreshenv"))) { Invoke-Hook "Install-Chocolatey" -Fallback { @@ -53,12 +47,30 @@ function Start-InstallationLoop { return; } + Invoke-Hook "Install-PSModules" -Fallback { + Install-Module -AcceptLicense -Force PSWindowsUpdate; + }; + if (Test-Path $env:PWSH_PATH) { - Remove-Item -Recurse $env:PWSH_PATH; + attrib "-R" "$env:PWSH_PATH\*" /S /D; + Remove-Item -Recurse -Force $env:PWSH_PATH; + } else { + Set-Stage ([SetupStage]::Install); } } else { + $null = Import-Module PSWindowsUpdate; + + Invoke-Hook "Invoke-WindowsUpdate" -Fallback { + Update-WindowsInstallation; + }; + + if ((Get-WURebootStatus -Silent)) { + Restart-Intermediate; + return; + } + switch (Get-Stage) { - $null { + ([SetupStage]::Install) { } Default {} } @@ -74,6 +86,7 @@ function Invoke-WindowsInstallation([Context] $context) { } function Start-OldWindowsInstallationScript([Context] $context) { + . "$PSScriptRoot/Upgrade.ps1"; $finished = $false; Remove-Item Env:\POSH_THEME -ErrorAction SilentlyContinue; $configPath = "$PSScriptRoot/../Config"; diff --git a/scripts/Windows/Scripts/Update.ps1 b/scripts/Windows/Scripts/Update.ps1 new file mode 100644 index 00000000..fc1ba29e --- /dev/null +++ b/scripts/Windows/Scripts/Update.ps1 @@ -0,0 +1,34 @@ +<# + .SYNOPSIS + Updates the running Windows machine. +#> +function Update-WindowsInstallation { + <# + Runs the Windows update loop. + #> + function Start-UpdateLoop { + Write-Host "Preparing for Windows Update"; + $null = Import-Module PSWindowsUpdate; + + while (((Get-WindowsUpdate -IgnoreReboot).Count -gt 0)) { + Write-Host "There are updates available."; + Write-Host "Installing updates"; + + try { + $null = Install-WindowsUpdate -AcceptAll -IgnoreReboot -ErrorAction "SilentlyContinue"; + } + catch { } + + if ((Get-WURebootStatus -Silent)) { + Write-Information "A Reboot is Required!"; + Write-Information "Windows will reboot now and the installation will be continued automatically."; + return; + } + } else { + Write-Information "Upgrading Windows finished successfully!"; + return; + } + } + + Start-UpdateLoop; +}