Skip to content

Commit

Permalink
Refactor UpdateInstall.ps1 script to download and install individually
Browse files Browse the repository at this point in the history
  • Loading branch information
Pablo Herranz Ramírez committed Jan 29, 2025
1 parent 0976168 commit c3915f2
Showing 1 changed file with 35 additions and 22 deletions.
57 changes: 35 additions & 22 deletions data/wsl/UpdateInstall.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -33,34 +33,47 @@ foreach ($Update in $SearchResult.Updates) {
$UpdatesToInstall.Add($Update) | Out-Null
}

# Initialize the Downloader
# Download updates individually
LogMessage "Downloading updates..."
$Downloader = $Session.CreateUpdateDownloader()
$Downloader.Updates = $UpdatesToInstall

# Download updates
foreach ($Update in $Downloader.Updates) {
foreach ($Update in $UpdatesToInstall) {
$Downloader = $Session.CreateUpdateDownloader()
$SingleUpdateColl = New-Object -ComObject Microsoft.Update.UpdateColl
$SingleUpdateColl.Add($Update) | Out-Null
$Downloader.Updates = $SingleUpdateColl

LogMessage "Downloading: $($Update.Title)..."
# Start downloading this update individually
$DownloadResult = $Downloader.Download()
LogMessage "Downloaded: $($Update.Title) successfully." -Color "Green"
try {
$DownloadResult = $Downloader.Download()
if ($DownloadResult.ResultCode -eq 2) {
LogMessage "Downloaded: $($Update.Title) successfully." -Color "Green"
} else {
LogMessage "Download failed for: $($Update.Title)" -Color "Yellow"
}
} catch {
LogMessage "Error downloading $($Update.Title): $_" -Color "Red"
}
}

# Install updates
# Install updates individually
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"
foreach ($Update in $UpdatesToInstall) {
$Installer = $Session.CreateUpdateInstaller()
$SingleUpdateColl = New-Object -ComObject Microsoft.Update.UpdateColl
$SingleUpdateColl.Add($Update) | Out-Null
$Installer.Updates = $SingleUpdateColl

LogMessage "Installing: $($Update.Title)..."
try {
$InstallResult = $Installer.Install()
if ($InstallResult.ResultCode -eq 2) {
LogMessage "Successfully installed: $($Update.Title)" -Color "Green"
} else {
LogMessage "Installation failed for: $($Update.Title). Result code: $($InstallResult.ResultCode)" -Color "Yellow"
}
} catch {
LogMessage "Error occurred during installation of $($Update.Title): $_" -Color "Red"
}
} catch {
LogMessage "Error occurred during installation: $_" -Color "Red"
}

# Completion message
LogMessage "Windows Update process completed."
LogMessage "Windows Update process completed."

0 comments on commit c3915f2

Please sign in to comment.