使用C#与Windows Update进行交互

是否有任何用于编写可与Windows更新接口的C#程序的API,并使用它来有选择地安装某些更新?

我正在考虑将列表存储在已批准更新的中央存储库中。 然后,客户端应用程序(必须安装一次)将与Windows Update连接以确定可用的更新,然后安装批准列表中的更新。 这样,更新仍然从客户端角度自动应用,但我可以选择正在应用哪些更新。

顺便说一下,这不是我在公司中的角色,我真的只是想知道是否有一个用于Windows更新的API以及如何使用它。

在您的C#项目中添加对WUApiLib的引用。

using WUApiLib; protected override void OnLoad(EventArgs e){ base.OnLoad(e); UpdateSession uSession = new UpdateSession(); IUpdateSearcher uSearcher = uSession.CreateUpdateSearcher(); uSearcher.Online = false; try { ISearchResult sResult = uSearcher.Search("IsInstalled=1 And IsHidden=0"); textBox1.Text = "Found " + sResult.Updates.Count + " updates" + Environment.NewLine; foreach (IUpdate update in sResult.Updates) { textBox1.AppendText(update.Title + Environment.NewLine); } } catch (Exception ex) { Console.WriteLine("Something went wrong: " + ex.Message); } } 

鉴于您有一个带有TextBox的表单,这将为您提供当前安装的更新列表。 有关更多文档,请参见http://msdn.microsoft.com/en-us/library/aa387102(VS.85).aspx 。

但是,这将不允许您查找未通过Windows Update分发的KB修补程序。

做你想做的最简单的方法就是使用WSUS 。 它是免费的,基本上允许您设置自己的本地Windows更新服务器,您可以在其中决定哪些更新被“批准”为您的计算机。 WSUS服务器和客户端都不需要在域中,但如果它们更容易配置客户端。 如果您有不同的机器组需要批准不同的更新集,那么也支持。

这不仅可以实现您的既定目标,还可以通过从WSUS服务器下载一次更新来节省您的整体网络带宽。

如果在您的上下文中允许您使用Windows Server Update Service( WSUS ),它将允许您访问Microsoft.UpdateServices.Administration命名空间 。

从那里,你应该能够做一些好事:)

我为此编写了一些虚拟代码(一个简单的控制台应用程序,它将根据命令行参数安装我们想要的所有更新,它可以在这里找到并包含执行更新所需的一切)

https://bitbucket.org/LilleCarl/c-windowsupdate

如果你想自己做,你需要什么:

 Add reference to C:\Windows\System32\wuapi.dll using WUApiLib; 

有用的界面:

 UpdateSession UpdateCollection UpdateDownloader IUpdateInstaller IInstallationResult 

PL吧。 我首先尝试了Christoph Grimmer-Die方法,在某些情况下,它无法正常工作。 我想这是由于.net或OS体系结构的不同版本(32位或64位)。 然后,为了确保我的程序始终获得每个计算机域的Windows Update等待列表,我执行了以下操作:

  • 使用WSUS安装服务器(可以节省一些互联网带宽): http : //www.microsoft.com/en-us/download/details.aspx? displaylang = en& id = 5216
  • 将所有工作站和服务器添加到WSUS服务器

  • 获取SimpleImpersonation Lib以使用不同的管理权限运行此程序(可选)

  • 在dev工作站上仅安装管理控制台组件并运行以下程序:

它将使用UpdateInstallationStates.Downloaded在控制台中打印所有Windows更新

 using System; using Microsoft.UpdateServices.Administration; using SimpleImpersonation; namespace MAJSRS_CalendarChecker { class WSUS { public WSUS() { // I use impersonation to use other logon than mine. Remove the following "using" if not needed using (Impersonation.LogonUser("mydomain.local", "admin_account_wsus", "Password", LogonType.Batch)) { ComputerTargetScope scope = new ComputerTargetScope(); IUpdateServer server = AdminProxy.GetUpdateServer("wsus_server.mydomain.local", false, 80); ComputerTargetCollection targets = server.GetComputerTargets(scope); // Search targets = server.SearchComputerTargets("any_server_name_or_ip"); // To get only on server FindTarget method IComputerTarget target = FindTarget(targets, "any_server_name_or_ip"); Console.WriteLine(target.FullDomainName); IUpdateSummary summary = target.GetUpdateInstallationSummary(); UpdateScope _updateScope = new UpdateScope(); // See in UpdateInstallationStates all other properties criteria _updateScope.IncludedInstallationStates = UpdateInstallationStates.Downloaded; UpdateInstallationInfoCollection updatesInfo = target.GetUpdateInstallationInfoPerUpdate(_updateScope); int updateCount = updatesInfo.Count; foreach (IUpdateInstallationInfo updateInfo in updatesInfo) { Console.WriteLine(updateInfo.GetUpdate().Title); } } } public IComputerTarget FindTarget(ComputerTargetCollection coll, string computername) { foreach (IComputerTarget target in coll) { if (target.FullDomainName.Contains(computername.ToLower())) return target; } return null; } } }