PoshCode Archive  Artifact [ddcdf55ed1]

Artifact ddcdf55ed1579a44803b6fd736dc2603330d9ca4b1837422d4dda6f5e992276a:

  • File Automatic-Windows-Update.ps1 — part of check-in [586924bdb5] at 2018-06-10 13:58:58 on branch trunk — The script will launch Windows Update on your machine, and display the patches as they are being installed. Once installed, a result code will be displayed as well. If a reboot is needed, the script will take care of that automatically. (user: JayneticMuffin size: 3216)

# encoding: ascii
# api: powershell
# title: Automatic Windows Update
# description: The script will launch Windows Update on your machine, and display the patches as they are being installed.  Once installed, a result code will be displayed as well. If a reboot is needed, the script will take care of that automatically.
# version: 0.1
# author: JayneticMuffin
# license: CC0
# x-poshcode-id: 5670
# x-archived: 2015-01-31T21:21:06
# x-published: 2015-01-07T14:50:00
#
#
#Checking for available updates
Write-Host "  Checking for available updates... Please wait!" -ForegroundColor 'Yellow'
$UpdateSession = New-Object -ComObject Microsoft.Update.Session
$SearchResult = $UpdateSession.CreateUpdateSearcher().Search($criteria).Updates | ? {$_.Title -notmatch $skipUpdates}
$SearchResult = $SearchResult | Sort-Object LastDeploymentChangeTime -Descending
$totalUpdates = $SearchResult.Count

ForEach ($Update in $SearchResult) {
	If ($totalUpdates -eq $null) { $totalUpdates = 1}
	# Add Update to Collection 
	$UpdatesCollection = New-Object -ComObject Microsoft.Update.UpdateColl
	if ($Update.EulaAccepted -eq 0) { $Update.AcceptEula() }
	$null = $UpdatesCollection.Add($Update)
	
	# Download
	$fileSize = "{0:N0} MB" -f ($($Update.MaxDownloadSize) / 1MB)
	Write-Host "  Downloading ($($counter.ToString("00"))/$($totalUpdates.ToString("00"))) - $fileSize - " -ForegroundColor 'Yellow' -NoNewline
	$counter++
	Write-Host "$($Update.Title)" -ForegroundColor 'White'
	$UpdatesDownloader = $UpdateSession.CreateUpdateDownloader()
	$UpdatesDownloader.Updates = $UpdatesCollection
	$UpdatesDownloader.Priority = 3

	Write-Host "`t- Download " -NoNewline -ForegroundColor 'White'
	$DownloadResult = $UpdatesDownloader.Download()
	Switch ($DownloadResult.ResultCode) {
		0   { Write-Host "Not Started" -ForegroundColor 'Black' -BackgroundColor 'Yellow' }
		1   { Write-Host "In Progress" -ForegroundColor 'Black' -BackgroundColor 'Yellow' }
		2   { Write-Host "Succeeded" -ForegroundColor 'Green' }
		3   { Write-Host "Succeeded (with Errors)" -ForegroundColor 'Black' -BackgroundColor 'Yellow' }
		4   { Write-Host "Failed" -ForegroundColor 'White' -BackgroundColor 'Red' }
		5   { Write-Host "Aborted" -ForegroundColor 'White' -BackgroundColor 'Red' }
	}

	# Install
	$UpdatesInstaller = $UpdateSession.CreateUpdateInstaller()
	$UpdatesInstaller.Updates = $UpdatesCollection

	Write-Host "`t- Install  " -NoNewline -ForegroundColor 'White'
	$InstallResult = $UpdatesInstaller.Install()
	Switch ($installResult.ResultCode) {
		0   { Write-Host "Not Started" -ForegroundColor 'Black' -BackgroundColor 'Yellow' }
		1   { Write-Host "In Progress" -ForegroundColor 'Black' -BackgroundColor 'Yellow' }
		2   { Write-Host "Succeeded" -ForegroundColor 'Green' }
		3   { Write-Host "Succeeded (with Errors)" -ForegroundColor 'Black' -BackgroundColor 'Yellow' }
		4   { Write-Host "Failed" -ForegroundColor 'White' -BackgroundColor 'Red' }
		5   { Write-Host "Aborted" -ForegroundColor 'White' -BackgroundColor 'Red' }
	}
	# Change the line below if you don't want to automatically reboot...
	If ($installResult.rebootRequired -eq 'True') { $needsReboot = $true }
}