Skip to content

Instantly share code, notes, and snippets.

@altrive
Last active November 12, 2019 16:23
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save altrive/5268181 to your computer and use it in GitHub Desktop.
Save altrive/5268181 to your computer and use it in GitHub Desktop.
PowerShell cmdlets to get WindowsUpdate patch file List.

Summary

PowerShell cmdlets to check WindowsUpdate and return patch list.

This script is intended for offline Windows patching. (MBSA 2.2 currently not support Windows 8/Windows Server 2012)

Usage

following sample check WindowsUpdate for specified computer, and download files to network share.

$params=@{
    TargetComputer      = "172.16.100.2"
    Credential          = Get-Credential -UserName "localhost\Administrator" -Message "Enter credential"
    DownloadFolder      = "\\172.16.0.1\Shared\Images\WindowsUpdate\Windows8"
    WindowsUpdateFilte  = "IsHidden=0"
}

#Remove files that updated periodically
Get-ChildItem $params.DownloadFolder | where Name -Like "am_delta_*.exe" | Remove-Item             #Windows Defender Definition
Get-ChildItem $params.DownloadFolder | where Name -Like "im*.cab" | Remove-Item                    #IME patch
Get-ChildItem $params.DownloadFolder | where Name -Like "windows-kb890830-*.exe" | Remove-Item     #Windows Malicious Software Removal Tool
Start-DownloadWindowsUpdateFile @params -Verbose
#Requires –Version 3
#Get WindowsUpdate List for offline patch
function Get-WindowsUpdateFileList
{
param(
[Parameter(Mandatory=$True)]
[string] $Filter
)
$objSession = New-Object -ComObject "Microsoft.Update.Session"
$objSearcher = $objSession.CreateUpdateSearcher()
$results = $objSearcher.Search($Filter)
$downloadList =@()
foreach($update in $results.Updates)
{
$title = $update.Title
$date = $update.LastDeploymentChangeTime
$isInstalled = $update.IsInstalled
$rebootRequired = $update.RebootRequired
foreach($bundledUpdate in $update.BundledUpdates)
{
foreach($content in $bundledUpdate.DownloadContents)
{
#if($url.Contains("-express_") -or $url.Contains("-delta_") -or $url.EndsWith(".psf"))
if($content.IsDeltaCompressedContent)
{
#Skip express package(Delta Compressed Package) and .psf file
continue
}
#Append to download list
$url = $content.DownloadURL
$fileName = [IO.Path]::GetFileName($url)
$downloadList += [pscustomobject]@{Title=$title;LastDeploymentChangeTime=$date ;DownloadURL=$url;IsInstalled=$isInstalled;RebootRequired=$rebootRequired;FileName=$fileName}
}
}
}
return $downloadList
}
function Start-DownloadWindowsUpdateFile
{
[Cmdletbinding()]
param(
[string] $TargetComputer,
$Credential,
[string] $DownloadFolder,
[string] $WindowsUpdateFilter
)
if(!(Test-Path $DownloadFolder))
{
Write-Error "Destination folder not found!"
}
if($TargetComputer -eq $env:ComputerName)
{
Write-Verbose ("Search WindowsUpdate for local computer...")
$results = Get-WindowsUpdateFileList $WindowsUpdateFilter
}
else
{
Write-Verbose ("Search WindowsUpdate for remote computer({0})..." -f $TargetComputer)
$results = Invoke-Command -Computer $TargetComputer -Credential $Credential -ScriptBlock ${function:Get-WindowsUpdateFileList} -ArgumentList $WindowsUpdateFilter
}
Write-Verbose ("WindowsUpdate check completed: {0} files found" -f $results.Count)
#$results | select Title,FileName, LastDeploymentChangeTime | Format-List
#Start Download Files
Write-Verbose ("Download files started...")
foreach($update in $results)
{
$fileName = [IO.Path]::GetFileName($update.DownloadURL)
$filePath = Join-Path $DownloadFolder $fileName
if(Test-Path $filePath)
{
Write-Verbose "`tSkip already exists patch: $fileName"
continue
}
Write-Verbose "`tStart download patch: $fileName"
$params = @{
Source = $update.DownloadURL
DisplayName = $update.Title
Description = $update.DownloadURL
Destination = $DownloadFolder
}
Start-BitsTransfer @params
}
Write-Verbose ("Download files completed...")
}
@velsenthil
Copy link

Hi Team,

How to modify the above code to download only give KB article number and save it local computer.

Request you to kindly help me to modify.

Thanks and regards,

senthil

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment