代码之家  ›  专栏  ›  技术社区  ›  DevinB

使用c与Windows Update交互

  •  14
  • DevinB  · 技术社区  · 15 年前

    是否有用于编写可以与Windows Update交互并使用它选择性安装某些更新的C程序的API?

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

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

    5 回复  |  直到 8 年前
        1
  •  19
  •   Christoph Grimmer    13 年前

    将对wuapilib的引用添加到您的C项目中。

    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);
        }
    }
    

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

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

        2
  •  4
  •   Ryan Bolger    15 年前

    最简单的方法就是使用 WSUS . 它是免费的,基本上可以让您设置自己的本地Windows Update服务器,在那里您可以决定哪些更新是为您的计算机“批准”的。WSUS服务器和客户机都不需要在域中,尽管这样可以更容易地配置客户机(如果是的话)。如果您有不同的机器集需要批准不同的更新集,那么也支持这一点。

    这不仅实现了您所述的目标,而且只从WSUS服务器下载一次更新,从而节省了您的整体网络带宽。

        3
  •  3
  •   P-L    13 年前

    如果在您的上下文中允许您使用Windows服务器更新服务( WSUS ,它将允许您访问 Microsoft.UpdateServices.Administration Namespace .

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

        4
  •  2
  •   LilleCarl    8 年前

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

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

    如果你想自己动手,你需要的是:

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

    有用的接口:

    UpdateSession
    UpdateCollection
    UpdateDownloader
    IUpdateInstaller
    IInstallationResult
    
        5
  •  1
  •   Xavius Pupuss    10 年前

    P L右。我首先尝试了克里斯托夫·格里默的死法,但在某些情况下,它不起作用。我想这是由于.NET或OS体系结构的不同版本(32或64位)造成的。 然后,为了确保我的程序始终获得我的每个计算机域的Windows更新等待列表,我执行了以下操作:

    它将在控制台中打印所有带有UpdateSinstallationStates的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;
            }
        }
    }