2009-05-02 78 views
17

我的计算机上安装的每个更新和修补程序的列表,来自Microsoft Windows Update或来自知识库。我需要每个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

回答

7

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

Set updateSearch = CreateObject("Microsoft.Update.Session").CreateUpdateSearcher 
Set updateHistory = updateSearch.QueryHistory(1, updateSearch.GetTotalHistoryCount) 

For Each updateEntry in updateHistory 
    Wscript.Echo "Title: " & updateEntry.Title 
    Wscript.Echo "application ID: " & updateEntry.ClientApplicationID 
    Wscript.Echo " --" 
Next

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

+0

不幸的是,如果这些更新中的一个已被卸载,它仍然会在这个列表中显示。 – 2010-07-21 22:43:01

+1

查看操作属性 – 2013-11-02 21:24:23

+0

'updateEntry'中的所有属性列表可以在这里找到(http://msdn.microsoft.com/zh-cn/library/aa386400(v = vs.85)的.aspx)。 – nateirvin 2014-05-22 17:43:59

11

后上什么,我早发现了一些进一步的搜索。 (是的,同为VolkerK建议首先)

  1. 在VS2008 CMD位于%SystemRoot%\ SYSTEM32 \运行命令来获得托管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); 
+0

即使在卸载后,它是否也能正常工作? – user1438082 2014-03-22 10:53:29

+1

不知道,我认为它会反映在Windows Update中看到的历史;但可能是错误的,我建议你对它进行原型设计并看看它的功能。我目前没有可用的Windows计算机,因为我现在正在运行Gentoo Linux。 – 2014-03-22 14:24:53

+0

如何在远程机器上创建'UpdateSession'实例? – 2016-05-02 17:57:55

0

万一你只是想更新的列表,如果你得到它不在乎通过代码或图形用户界面,在这里是如何做到这一点在PowerShell中:

  1. 打开PowerShell中(最好是“作为管理员身份运行”)
  2. 键入“Get-修补程序”,然后回车。而已。

Get hotfixes

0
 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是在安全软件包像汤姆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编号就像你正在寻找我相信

相关问题