【问题标题】:How do I get a list of installed updates and hotfixes?如何获取已安装更新和修补程序的列表?
【发布时间】:2009-05-02 18:32:30
【问题描述】:

已安装在我的计算机上的每个更新和修补程序的列表,来自 Microsoft Windows 更新或知识库。我需要每个 KBxxxxxx 或类似表示形式的 ID...

目前我有:

const string query = "SELECT HotFixID FROM Win32_QuickFixEngineering";
var search = new ManagementObjectSearcher(query);
var collection = search.Get();

foreach (ManagementObject quickFix in collection)
    Console.WriteLine(quickFix["HotFixID"].ToString());

但这似乎并没有列出所有内容,它只列出了 QFE。

我需要它在 Windows XP、Vista 和 7 上工作。

【问题讨论】:

    标签: c# .net windows list


    【解决方案1】:

    在对我之前发现的内容进行了进一步搜索之后。 (是的,和 VolkerK 首先建议的一样)

    1. 在 %SystemRoot%\System32\ 中的 VS2008 CMD 下运行命令以获取托管 dll:
      tlbimp.exe wuapi.dll /out=WUApiInterop.dll
    2. 添加 WUApiInterop.dll 作为项目引用,以便我们查看函数。

    使用以下代码,我可以得到一个列表,我可以从中提取 KB 编号:

    var updateSession = new UpdateSession();
    var updateSearcher = updateSession.CreateUpdateSearcher();
    var count = updateSearcher.GetTotalHistoryCount();
    var history = updateSearcher.QueryHistory(0, count);
    
    for (int i = 0; i < count; ++i)
        Console.WriteLine(history[i].Title);
    

    【讨论】:

    • 不知道,我认为它会反映 Windows 更新中看到的历史;但可能是错误的,我建议您对其进行原型制作并查看它的作用。我目前没有可用的 Windows 计算机,因为我现在正在运行 Gentoo Linux。
    • 如何在远程机器上创建UpdateSession 的实例?
    • 今天在windows 10 Professional上安装windows update [KB3156421, KB890830 and KB3157993](我猜是KB3156421的问题);查询 history[i].ResultCode 抛出 comException: Exception from HRESULT: 0x80240FFF,我为此添加了 try/catch
    • @buildcomplete:看起来更像是 WSUS 问题,请咨询 blogs.technet.microsoft.com/sus/2011/04/14/… 了解更多信息
    • @TomWijsman:我必须承认,我对windows update等不太了解,但是:1)安装的更新都是windows update自动安装的,我们没有内部服务器安装更新。 2)我们只在其中一台失败的机器上安装了“一个”程序(失败的程序)。 3)我可以查询一些对象,只有在读取了一些之后才会失败。
    【解决方案2】:

    您可以使用IUpdateSession3::QueryHistory Method
    返回条目的属性描述在http://msdn.microsoft.com/en-us/library/aa386400(VS.85).aspx

    设置 updateSearch = CreateObject("Microsoft.Update.Session").CreateUpdateSearcher
    设置 updateHistory = updateSearch.QueryHistory(1, updateSearch.GetTotalHistoryCount)
    
    对于 updateHistory 中的每个 updateEntry
      Wscript.Echo "标题:" & updateEntry.Title
      Wscript.Echo "应用程序 ID:" & updateEntry.ClientApplicationID
      Wscript.Echo "--"
    下一个

    编辑:也看看http://msdn.microsoft.com/en-us/library/aa387287%28VS.85%29.aspx

    【讨论】:

    • 很遗憾,如果其中一个更新已被卸载,它仍会显示在此列表中。
    • 看看Operation属性
    • updateEntry中所有属性的列表可以在here找到。
    • 不知道为什么这里是vbscript而不是c#,应该有自己的问题和答案。
    【解决方案3】:
    const string querys = "SELECT HotFixID FROM Win32_QuickFixEngineering";
    var search = new ManagementObjectSearcher(querys);
    var collection = search.Get();
    
    foreach (ManagementObject quickfix in collection)
    {
        hotfix = quickfix["HotFixID"].ToString();
    }
    
    listBox1.Items.Add(hotfix);
    

    这将使用当前安装的修补程序或更新填充列表框

    如果您想列出所有更新和修补程序的历史记录以显示 那么,上面提到的 Tom Wijsman 的例子就可以工作了

    【讨论】:

      【解决方案4】:

      以防万一您只需要更新列表而不关心是通过代码还是 GUI 获取它,以下是在 Powershell 中的操作方法:

      1. 打开 PowerShell(最好“以管理员身份运行”)
      2. 输入“get-hotfix”并回车。就是这样。

      【讨论】:

        【解决方案5】:
                string ExtractString(string s)
            {
                // You should check for errors in real-world code, omitted for brevity
                try
                {
                    var startTag = "(";
                    int startIndex = s.IndexOf(startTag) + startTag.Length;
                    int endIndex = s.IndexOf(")", startIndex);
                    return s.Substring(startIndex, endIndex - startIndex);
                }
                catch
                {
                    return ("CNVFL");
                }
            }
        

        上面是一个简单的提取字符串方法,我用它来发现 KB 像 Tom Wijsman 提到的那样在安全包中并运行他的。

        var updateSession = new UpdateSession();
        var updateSearcher = updateSession.CreateUpdateSearcher();
        var count = updateSearcher.GetTotalHistoryCount();
        var history = updateSearcher.QueryHistory(0, count);
        
        for (int i = 0; i < count; ++i){
           //sets KB here!!
           string _splitstring = ExtractString(history[i].Title);
           Console.WriteLine(_splitstring);
        }
        

        我相信这会让你得到你正在寻找的 KB 编号

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-12-17
          • 2011-09-04
          • 1970-01-01
          • 2021-04-27
          相关资源
          最近更新 更多