使用 C# 与 Windows Update 交互

发布于 2024-07-21 06:03:20 字数 265 浏览 3 评论 0原文

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

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

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

Is there any API for writing a C# program that could interface with Windows update, and use it to selectively install certain updates?

I'm thinking somewhere along the lines of storing a list in a central repository of approved updates. Then the client side applications (which would have to be installed once) would interface with Windows Update to determine what updates are available, then install the ones that are on the approved list. That way the updates are still applied automatically from a client-side perspective, but I can select which updates are being applied.

This is not my role in the company by the way, I was really just wondering if there is an API for windows update and how to use it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

安人多梦 2024-07-28 06:03:20

将 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 更新分发的 KB 修补程序。

Add a Reference to WUApiLib to your C# project.

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

Given you have a form with a TextBox this will give you a list of the currently installed updates. See http://msdn.microsoft.com/en-us/library/aa387102(VS.85).aspx for more documentation.

This will, however, not allow you to find KB hotfixes which are not distributed via Windows Update.

つ低調成傷 2024-07-28 06:03:20

完成您想要的操作的最简单方法是使用 WSUS。 它是免费的,基本上可以让您设置自己的本地 Windows 更新服务器,您可以在其中决定哪些更新“批准”用于您的计算机。 WSUS 服务器和客户端都不需要位于域中,但如果它们位于域中,则可以更轻松地配置客户端。 如果您有不同的计算机组需要批准不同的更新组,这也是受支持的。

这不仅可以实现您既定的目标,而且只需从 WSUS 服务器下载一次更新即可节省您的整体网络带宽。

The easiest way to do what you want is using WSUS. It's free and basically lets you setup your own local windows update server where you decide which updates are "approved" for your computers. Neither the WSUS server nor the clients need to be in a domain, though it makes it easier to configure the clients if they are. If you have different sets of machines that need different sets of updates approved, that's also supported.

Not only does this accomplish your stated goal, it saves your overall network bandwidth as well by only downloading the updates once from the WSUS server.

遇到 2024-07-28 06:03:20

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

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

If in your context you're allowed to use Windows Server Update Service (WSUS), it will give you access to the Microsoft.UpdateServices.Administration Namespace.

From there, you should be able to do some nice things :)

她如夕阳 2024-07-28 06:03:20

PL 对。 我首先尝试了 Christoph Grimmer-Die 方法,但在某些情况下,它不起作用。 我猜这是由于 .net 或操作系统架构的不同版本(32 或 64 位)造成的。
然后,为了确保我的程序始终获得每个计算机域的 Windows 更新等待列表,我执行了以下操作:

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

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

  • 仅安装管理控制台组件您的开发工作站并运行以下程序:

它将在控制台中使用 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;
        }
    }
}

P-L right. I tried first the Christoph Grimmer-Die method, and in some case, it was not working. I guess it was due to different version of .net or OS architecture (32 or 64 bits).
Then, to be sure that my program get always the Windows Update waiting list of each of my computer domain, I did the following :

  • Install a serveur with WSUS (may save some internet bandwith) : http://www.microsoft.com/en-us/download/details.aspx?displaylang=en&id=5216
  • Add all your workstations & servers to your WSUS server

  • Get SimpleImpersonation Lib to run this program with different admin right (optional)

  • Install only the administration console component on your dev workstation and run the following program :

It will print in the console all Windows updates with UpdateInstallationStates.Downloaded

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;
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文