Skip to content

Commit

Permalink
Added new update script
Browse files Browse the repository at this point in the history
  • Loading branch information
Pablo Herranz Ramírez committed Jan 22, 2025
1 parent d87857f commit 7bb21b9
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 3 deletions.
66 changes: 66 additions & 0 deletions data/wsl/UpdateInstall.ps1
Original file line number Diff line number Diff line change
@@ -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."
6 changes: 3 additions & 3 deletions tests/wsl/install/update_windows.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 7bb21b9

Please sign in to comment.