[CmdletBinding()]
Param(
[string]$CatalogsFolderPath = "C:\Windows\WinSxS\Catalogs",
[string]$MissingCatalogKeysFile = "Missing_Catalog_Keys.txt"
)
function Get-HardLinks([string]$path)
{
(dir $path -force | Where-Object -Property LinkType -eq HardLink | Select-Object -Property Target).Target
}
$file = $MissingCatalogKeysFile
If (!(Test-Path $file -PathType leaf))
{
throw("FILE_NOT_FOUND: $file'")
}
$kbs = [System.Collections.ArrayList]::new()
$lines = Get-Content $file
$i=0
foreach ($line in $lines)
{
$i = $i + 1
$CatalogProgressParameters = @{
Id = 0
Activity = 'Catalog Keys'
Status = "$i/$($lines.count)"
PercentComplete = ($i / $lines.Count) * 100
CurrentOperation = $line
}
Write-Progress @CatalogProgressParameters
$file = Join-Path -Path $CatalogsFolderPath -ChildPath "$($line).cat"
If (Test-Path $file -PathType leaf)
{
$links = Get-HardLinks($file)
if ($links)
{
$il = 0
foreach ($link in $links)
{
$il++
$LinkProgressParameters = @{
Id = 1
ParentId = 0
Activity = 'Links'
Status = "$il/$($links.count)"
PercentComplete = ($il / $links.Count) * 100
CurrentOperation = $link
}
Write-Progress @LinkProgressParameters
if ($link -match 'for_(KB.+?)~')
{
$kb = $matches[1]
if (-not $kbs.Contains($kb))
{
[void]$kbs.Add($kb)
}
}
Write-Progress -ID 1 -activity 'Links' -Completed
}
}
}
}
if (Get-Module -ListAvailable -Name kbupdate) {
foreach ($kb in ($kbs | Sort-Object))
{
Get-KbUpdate $kb -Architecture x64 -OperatingSystem 'Windows Server 2016' | Select-Object -Property id, Title
}
}
else {
$kbs
}