Public/Get-PendingUpdates.ps1
|
function Get-PendingUpdates { <# .SYNOPSIS Retrieves information about pending Windows Updates. .DESCRIPTION Checks for policy-scoped pending Windows Updates and returns detailed information about each update including size, importance, download status, and reboot requirements. This provides visibility into what the effective Windows Update policy currently makes visible. Intune and Windows Autopatch approvals, rollout timing, safeguard holds, and scan-source policy remain authoritative. .PARAMETER IncludeDetails Include additional details like KB numbers, descriptions, and security bulletin IDs. .PARAMETER IncludeDrivers Include driver updates in the search. By default, only software updates are returned. .PARAMETER LogPath Optional path to a log file for detailed logging. .PARAMETER SearchTimeoutSeconds Maximum time to wait for the Windows Update Agent search. The search runs in an isolated child process and is terminated when this timeout is exceeded. .EXAMPLE Get-PendingUpdates Returns basic information about all pending software updates. .EXAMPLE Get-PendingUpdates -IncludeDetails -IncludeDrivers Returns detailed information about all pending updates including drivers. .EXAMPLE $pending = Get-PendingUpdates if ($pending.Count -gt 0) { Write-Host "Found $($pending.Count) pending updates" $pending | Format-Table Title, SizeMB, Importance, IsDownloaded } .EXAMPLE Get-PendingUpdates | Where-Object { $_.Importance -eq 'Critical' } Returns only critical pending updates. .OUTPUTS System.Object[] Returns an array of objects containing update information: - Title: Update title - SizeMB: Update size in megabytes - Importance: Critical, Important, Moderate, Low - IsDownloaded: Whether the update is already downloaded - RebootRequired: Whether the update requires a reboot - ReleaseDate: When the update was released - KBArticleIDs: Associated KB article numbers (if IncludeDetails) - Description: Update description (if IncludeDetails) .NOTES Author: CSOLVE Scripts Version: 1.0.0 This function provides a user-friendly interface to check pending updates before deciding whether to install them using Install-WindowsUpdate. #> [CmdletBinding()] param( [Parameter()] [switch]$IncludeDetails, [Parameter()] [switch]$IncludeDrivers, [Parameter()] [string]$LogPath, [Parameter()] [ValidateRange(5, 1800)] [int]$SearchTimeoutSeconds = 120 ) try { # Call the private function to do the heavy lifting $pendingUpdates = Get-WUPendingUpdates -LogPath $LogPath -SearchTimeoutSeconds $SearchTimeoutSeconds if (-not $pendingUpdates -or $pendingUpdates.Count -eq 0) { Write-Verbose "No pending updates found" return @() } # Filter out drivers if not requested if (-not $IncludeDrivers) { $pendingUpdates = $pendingUpdates | Where-Object { $_.Type -ne 'Driver' } } # Create user-friendly output objects $results = foreach ($update in $pendingUpdates) { $updateInfo = [PSCustomObject]@{ Title = $update.Title SizeMB = $update.SizeMB Importance = $update.Importance IsDownloaded = $update.IsDownloaded RebootRequired = $update.RebootRequired ReleaseDate = $update.ReleaseDate Type = $update.Type ScanScope = $update.ScanScope } # Add detailed information if requested if ($IncludeDetails) { $updateInfo | Add-Member -NotePropertyName 'KBArticleIDs' -NotePropertyValue $update.KBArticleIDs $updateInfo | Add-Member -NotePropertyName 'Description' -NotePropertyValue $update.Description $updateInfo | Add-Member -NotePropertyName 'SecurityBulletinIDs' -NotePropertyValue $update.SecurityBulletinIDs $updateInfo | Add-Member -NotePropertyName 'DownloadStatus' -NotePropertyValue $update.DownloadStatus } $updateInfo } # Display summary information if ($results.Count -gt 0) { $totalSizeMB = [math]::Round(($results | Measure-Object -Property SizeMB -Sum).Sum, 1) $criticalCount = ($results | Where-Object { $_.Importance -eq 'Critical' }).Count $downloadedCount = ($results | Where-Object { $_.IsDownloaded -eq $true }).Count $rebootCount = ($results | Where-Object { $_.RebootRequired -eq $true }).Count Write-Host "`nPending Updates Summary:" -ForegroundColor Cyan Write-Host " Total Updates: $($results.Count)" -ForegroundColor White Write-Host " Total Size: $totalSizeMB MB" -ForegroundColor White Write-Host " Critical Updates: $criticalCount" -ForegroundColor $(if ($criticalCount -gt 0) { 'Yellow' } else { 'Green' }) Write-Host " Already Downloaded: $downloadedCount" -ForegroundColor Green Write-Host " Require Reboot: $rebootCount" -ForegroundColor $(if ($rebootCount -gt 0) { 'Yellow' } else { 'Green' }) if ($IncludeDrivers) { $driverCount = ($results | Where-Object { $_.Type -eq 'Driver' }).Count Write-Host " Driver Updates: $driverCount" -ForegroundColor White } Write-Host "" } return $results } catch { throw "Failed to retrieve policy-scoped pending updates: $($_.Exception.Message)" } } |