PowerShell code to get pending windows patch count

1

In the past I have used this code to list the count of windows patches:

$server = 'ServerName'  
$updatesession = [activator]::CreateInstance([type]::GetTypeFromProgID("Microsoft.Update.Session", $Server))
$updatesearcher = $updatesession.CreateUpdateSearcher()
$searchresult = $updatesearcher.Search("IsInstalled=0")
$PatchCount = $searchresult.Updates.Count       
return $PatchCount

However, this code suddenly stopped working for some servers. This could be an artifact of recent windows updates. A similar script (which is also failing) is here.

This is the exception I get:

Exception from HRESULT: 0x8024401C At line:5 char:1 + $searchresult = $updatesearcher.Search("IsInstalled=0") + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : OperationStopped: (:) [], COMException + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException

Appreciate any help in fixing this code (or if there's a better alternative, that would work too.)

windows
powershell
asked on Stack Overflow Oct 21, 2019 by RaviLobo • edited Oct 21, 2019 by RaviLobo

1 Answer

1

It looks like the 0x8024401C code means that the Windows Update service could not be contacted.

answered on Stack Overflow Oct 21, 2019 by Bender the Greatest

User contributions licensed under CC BY-SA 3.0