繁体   English   中英

如何获取已安装的更新和修补程序列表?

[英]How do I get a list of installed updates and hotfixes?

我的计算机上安装的每个更新和修补程序的列表,来自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上运行。

在进一步搜索我之前发现的内容之后。 (是的,与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);

您可以使用IUpdateSession3 :: QueryHistory方法
返回条目的属性在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

如果您只是想要更新列表而不关心是否通过代码或GUI获取更新,以下是如何在Powershell中执行此操作:

  1. 打开PowerShell(最好“以管理员身份运行”)
  2. 输入“get-hotfix”并按Enter键。 而已。

获取修补程序

        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数字,我相信

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示例将有效

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM