Reputation: 11
Problem - I have an ansible playbook in which I have written 3 powershell scripts to list last 5 installed patches, failed patches, and available patches. Now, the issue happening is:
We have currently, Windows 2012, 2016, 2019 versions and patches are pushed through WSUS. Here is the current script:
Installed Patches
Get-HotFix | Where-Object {$_.Description -match 'Update|Security Update'} | Sort-Object -Property InstalledOn -Descending | Select-Object -First 5 | Select -ExpandProperty HotFixID
Failed Patches
$Searcher = New-Object -ComObject Microsoft.Update.Session
$Searcher.ClientApplicationID = "My App"
$Searcher = $Searcher.CreateUpdateSearcher()
$SearchResult = $Searcher.Search("IsInstalled=0 and Type='Software'")
$FailedUpdates = $SearchResult.Updates | where {$_.ResultCode -ne "0"}
if ($FailedUpdates) {
foreach ($Update in $FailedUpdates) {
foreach ($KBID in $Update.KBArticleIDs) {
[PSCustomObject]@{
UpdateKB = $KBID
}
}
}
} else {
[PSCustomObject]@{
UpdateKB = "No Failed Patches"
}
}
Available Patches
$updateSession = New-Object -ComObject Microsoft.Update.Session
$updateSearcher = $updateSession.CreateUpdateSearcher()
$pendingUpdates = $updateSearcher.Search("IsInstalled=0")
foreach ($update in $pendingUpdates.Updates)
{ Write-Output "$($update.Title)" }
For pending updates, I did try Get-WsusUpdate and WindowsPSUpdate but they are not available on most of the servers and I don't have permission to install cmdlets. I need help on modifying these scripts to give the required results, or if there is any other way to list these then it would be helpful.
Upvotes: 0
Views: 326