From a6985d7e1b12aa4173355c3780144ce1c796ff0b 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 9794a09c..8b417a76 100644 --- a/scripts/Windows/OS/Install.ps1 +++ b/scripts/Windows/OS/Install.ps1 @@ -4,10 +4,10 @@ . "$PSScriptRoot/../Scripts/Hooks.ps1"; . "$PSScriptRoot/../Scripts/PowerManagement.ps1"; . "$PSScriptRoot/../Scripts/SetupConfig.ps1"; +. "$PSScriptRoot/../Scripts/Update.ps1"; . "$PSScriptRoot/../Scripts/Context.ps1"; . "$PSScriptRoot/../Software/Firefox/Install.ps1"; . "$PSScriptRoot/Manage.ps1"; -. "$PSScriptRoot/Upgrade.ps1"; . "$PSScriptRoot/User/Install.ps1"; <# @@ -23,15 +23,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 { @@ -52,12 +46,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 {} } @@ -73,6 +85,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..72049e04 --- /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; +}