From 7bb21b9740f23983f67ef0a68afcadaf0cb9f107 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Herranz=20Ram=C3=ADrez?= Date: Wed, 22 Jan 2025 12:02:47 +0100 Subject: [PATCH] Added new update script --- data/wsl/UpdateInstall.ps1 | 66 +++++++++++++++++++++++++++++ tests/wsl/install/update_windows.pm | 6 +-- 2 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 data/wsl/UpdateInstall.ps1 diff --git a/data/wsl/UpdateInstall.ps1 b/data/wsl/UpdateInstall.ps1 new file mode 100644 index 000000000000..538184e8be39 --- /dev/null +++ b/data/wsl/UpdateInstall.ps1 @@ -0,0 +1,66 @@ +# Define a function to log messages with timestamps +function LogMessage { + param ( + [string]$Message, + [string]$Color = "White" + ) + $Timestamp = (Get-Date).ToString("yyyy-MM-dd HH:mm:ss") + Write-Host "$Timestamp - $Message" -ForegroundColor $Color +} + +# Start logging +LogMessage "Starting Windows Update process..." + +# Create Update Session +$Session = New-Object -ComObject Microsoft.Update.Session +$Searcher = $Session.CreateUpdateSearcher() + +# Search for Updates +LogMessage "Searching for updates..." +$SearchResult = $Searcher.Search("IsInstalled=0") + +# Check if updates are available +if ($SearchResult.Updates.Count -eq 0) { + LogMessage "No updates found." -Color "Yellow" + return +} + +# Display updates found +LogMessage "$($SearchResult.Updates.Count) update(s) found." +$UpdatesToInstall = New-Object -ComObject Microsoft.Update.UpdateColl +foreach ($Update in $SearchResult.Updates) { + LogMessage "Update found: $($Update.Title)" + $UpdatesToInstall.Add($Update) | Out-Null +} + +# Initialize the Downloader +LogMessage "Downloading updates..." +$Downloader = $Session.CreateUpdateDownloader() +$Downloader.Updates = $UpdatesToInstall + +# Download updates +foreach ($Update in $Downloader.Updates) { + LogMessage "Downloading: $($Update.Title)..." + # Start downloading this update individually + $DownloadResult = $Downloader.Download() + LogMessage "Downloaded: $($Update.Title) successfully." -Color "Green" +} + +# Install updates +LogMessage "Installing updates..." +$Installer = $Session.CreateUpdateInstaller() +$Installer.Updates = $UpdatesToInstall + +try { + $InstallResult = $Installer.Install() + if ($InstallResult.ResultCode -eq 2) { + LogMessage "Updates installed successfully." -Color "Green" + } else { + LogMessage "Installation completed with errors or partial success." -Color "Yellow" + } +} catch { + LogMessage "Error occurred during installation: $_" -Color "Red" +} + +# Completion message +LogMessage "Windows Update process completed." diff --git a/tests/wsl/install/update_windows.pm b/tests/wsl/install/update_windows.pm index 2b43dc11b3d2..d0d9c873d726 100644 --- a/tests/wsl/install/update_windows.pm +++ b/tests/wsl/install/update_windows.pm @@ -12,11 +12,11 @@ use testapi; sub run { my $self = shift; - my $vbs_url = data_url("wsl/UpdateInstall.vbs"); + my $vbs_url = data_url("wsl/UpdateInstall.ps1"); $self->open_powershell_as_admin; - $self->run_in_powershell(cmd => "Invoke-WebRequest -Uri \"$vbs_url\" -OutFile \"C:\\UpdateInstall.vbs\""); + $self->run_in_powershell(cmd => "Invoke-WebRequest -Uri \"$vbs_url\" -OutFile \"C:\\UpdateInstall.ps1\""); $self->run_in_powershell( - cmd => 'cd \\; $port.WriteLine($(cscript .\\UpdateInstall.vbs /Automate))', + cmd => 'cd \\; $port.WriteLine($(.\\UpdateInstall.ps1))', code => sub { die("Update script finished unespectedly or timed out...") unless wait_serial("The update process finished with value 1", timeout => 3600);