Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query for accurate and localized list of installed Windows updates

Tags:

c#

wmi

How can I query for an accurate and localized list of Windows updates installed on a machine using C#?

I define accurate as matching what is displayed in the "Microsoft Windows" category of Microsoft's View Installed Updates dialog under Programs and Features in Windows 7.

If I use WUApi.DLL, the information is returned localized but I can't get an accurate list. In the case of WUApi.dll, some hotfixes are missing and if an update has been uninstalled, it still shows up in the list generated by the following code:

public static void GetWindowsUpdates() 
{ 
    var updateSession = new UpdateSession(); 
    var updateSearcher = updateSession.CreateUpdateSearcher(); 
    var count = updateSearcher.GetTotalHistoryCount(); 
    if (count == 0) 
        return; 

    var history = updateSearcher.QueryHistory(0, count); 
    for (int i = 0; i < count; i++) 
    { 
        if (history[i].ResultCode == OperationResultCode.orcSucceeded) 
        { 
            Console.WriteLine(history[i].Title); 

            if (history[i].Operation == UpdateOperation.uoUninstallation) 
            { 
                Console.WriteLine("!!! Operation == uninstall"); // This is never true 
            } 
        } 
    } 
} 

The WUApi search method also didn't provide an accurate list using the following code:

        WUApiLib.UpdateSessionClass session = new WUApiLib.UpdateSessionClass(); 
        WUApiLib.IUpdateSearcher searcher = session.CreateUpdateSearcher(); 

        searcher.IncludePotentiallySupersededUpdates = true; 

        WUApiLib.ISearchResult result = searcher.Search("IsInstalled=1"); 
        Console.WriteLine("Updates found: " + result.Updates.Count); 
        foreach (IUpdate item in result.Updates) 
        { 
            Console.WriteLine(item.Title); 
        } 

If I use WMI to read the list of updates, I can get an accurate list, but it is not localized. I use the following code:

ManagementObjectSearcher searcher = new ManagementObjectSearcher(new ObjectQuery("select * from Win32_QuickFixEngineering")); 
searcher.Options.UseAmendedQualifiers = true; 
searcher.Scope.Options.Locale = "MS_" + CultureInfo.CurrentCulture.LCID.ToString("X"); 
ManagementObjectCollection results = searcher.Get(); 

Console.WriteLine("\n==WMI==" + results.Count); 
foreach (ManagementObject item in results) 
{ 
    Console.WriteLine("\t--Properties--"); 
    foreach (var x in item.Properties) 
    { 
        Console.WriteLine(x.Name + ": " + item[x.Name]); 
    } 
    Console.WriteLine("\t--System Properties--"); 
    foreach (var x in item.SystemProperties) 
    { 
        Console.WriteLine(x.Name + ": " + x.Value); 
    } 
    Console.WriteLine("\t--Qualifiers--"); 
    foreach (var x in item.Qualifiers) 
    { 
        Console.WriteLine(x.Name + ": " + x.Value); 
    } 
} 
like image 571
Todd Kobus Avatar asked Aug 19 '10 18:08

Todd Kobus


1 Answers

The WUApi only registers actions completed via the WUApi, so if you manually install or remove an update it will either remain in the list after being uninstalled or never show up in the list. As a result, in my opinion, WUApi can not be counted on for an accurate list.

WMI allows access to an accurate list of Windows Updates, but the list is filtered to only the "Microsoft Windows" category. This was difficult, because my requirement was to get a list of all updates.

Internally the "View Installed Updates" dialog uses the CBS (Component Based Servicing). Unfortunately, CBS is not public. Some details regarding the API can be found here: http://msdn.microsoft.com/en-us/library/Aa903048.aspx

like image 170
Todd Kobus Avatar answered Nov 15 '22 04:11

Todd Kobus