Skip to content

Instantly share code, notes, and snippets.

@altrive
Last active November 12, 2019 16:23
Show Gist options
  • Select an option

  • Save altrive/5268181 to your computer and use it in GitHub Desktop.

Select an option

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.

$target = $env:COMPUTERNAME
#$filter = "IsAssigned=1 and IsHidden=0"
$filter = "IsHidden=0"

#Get WindowsUpdate file list for target computer(if not domain environment -Credential parameter needed)
$results = Invoke-Command -Computer $target -ScriptBlock ${function:Get-WindowsUpdateFileList} -ArgumentList $filter

#Download files
$destination= "\\172.16.0.1\Shared\Images\WindowsUpdate\WindowsServer2012"
foreach($update in $results)
{
    $params = @{
        Source      = $update.DownloadURL
        DisplayName = $update.Title
        Description = $update.DownloadURL
        Destination = $destination
    }
    Start-BitsTransfer @params
}
#Get WindowsUpdate List for offline patch
function Get-WindowsUpdateFileList
{
param(
[Parameter(Mandatory=$True)]
[string] $Filter
)
<#
#Need CreateInstance to kick WindowsUpdate from Remote Computer?
$objSession = [Activator]::CreateInstance([Type]::GetTypeFromProgID("Microsoft.Update.Session",$env:ComputerName))
#>
$objSession = New-Object -ComObject "Microsoft.Update.Session"
$objSearcher = $objSession.CreateUpdateSearcher()
$results = $objSearcher.Search($Filter)
#Write-Debug $results.Updates.Count
$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
$downloadList += [pscustomobject]@{Title=$title;LastDeploymentChangeTime=$date ;DownloadURL=$url;IsInstalled=$isInstalled;RebootRequired=$rebootRequired}
}
}
}
return $downloadList
}
@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