【问题标题】:Best way of detecting if Windows has Windows Updates ready to download/install?检测 Windows 是否已准备好下载/安装 Windows 更新的最佳方法?
【发布时间】:2010-09-12 09:41:31
【问题描述】:

我对 Windows 2000/XP 特别感兴趣,但 Vista/7 也会很有趣(如果不同的话)。

我正在考虑每天安排批处理文件或等效文件的任务。

编辑:对不起,我应该提供更多信息。这个问题与我手动应用更新的 10 台机器有关。我不想以编程方式安装更新,而只是使用批处理或脚本找出是否有准备好下载/安装的更新(即系统托盘中的更新盾牌图标表明这一点)。谢谢。

【问题讨论】:

  • 我的答案显然是针对网络的。你的问题的范围是什么?单机?一个网络?多个网络上的几台机器?
  • 如果所有机器都在您的网络上,请在下面查看我的答案。我的网络管理员会杀死任何试图从他手中夺走它的人(他管理着大约 50 台机器)。
  • Windows 目录中的 WindowsUpdate.log 中似乎有一些信息。这有点神秘,但它可能会对你有所帮助。我找到了可能对您有帮助的信息:msdn.microsoft.com/en-us/library/aa387102(VS.85).aspx
  • 我认为这属于 serverfault.com

标签: windows-xp windows-update


【解决方案1】:

你可以使用WUApiLib:

UpdateSessionClass session = new UpdateSessionClass();

IUpdateSearcher search = session.CreateUpdateSearcher();

ISearchResult result = search.Search("IsInstalled=0 and IsPresent=0 and Type='Software'");

int numberOfUpdates = result.Updates.Count - 1;

Log.Debug("Found " + numberOfUpdates.ToString() + " updates");

UpdateCollection updateCollection = new UpdateCollection();

for (int i = 0; i < numberOfUpdates; i++)
{
    IUpdate update = result.Updates[i];

    if (update.EulaAccepted == false)
    {
        update.AcceptEula();
    }

    updateCollection.Add(update);
}

if (numberOfUpdates > 0)
{
    UpdateCollection downloadCollection = new UpdateCollection();

    for (int i = 0; i < updateCollection.Count; i++)
    {
        downloadCollection.Add(updateCollection[i]);
    }

    UpdateDownloader downloader = session.CreateUpdateDownloader();

    downloader.Updates =  downloadCollection;

    IDownloadResult dlResult = downloader.Download();

    if (dlResult.ResultCode == OperationResultCode.orcSucceeded)
    {
        for (int i = 0; i < downloadCollection.Count; i++)
        {
            Log.Debug(string.Format("Downloaded {0} with a result of {1}", downloadCollection[i].Title, dlResult.GetUpdateResult(i).ResultCode));
        }

        UpdateCollection installCollection = new UpdateCollection();

        for (int i = 0; i < updateCollection.Count; i++)
        {
            if (downloadCollection[i].IsDownloaded)
            {
                installCollection.Add(downloadCollection[i]);
            }
        }

        UpdateInstaller installer = session.CreateUpdateInstaller() as UpdateInstaller;

        installer.Updates = installCollection;

        IInstallationResult iresult = installer.Install();

        if (iresult.ResultCode == OperationResultCode.orcSucceeded)
        {
            updated = installCollection.Count.ToString() + " updates installed";

            for (int i = 0; i < installCollection.Count; i++)
            {
                Log.Debug(string.Format("Installed {0} with a result of {1}", installCollection[i].Title, iresult.GetUpdateResult(i).ResultCode));
            }

            if (iresult.RebootRequired == true)
            {
                ManagementClass mcWin32 = new ManagementClass("Win32_OperatingSystem");

                foreach (ManagementObject shutdown in mcWin32.GetInstances())
                {
                    shutdown.Scope.Options.EnablePrivileges = true;
                    shutdown.InvokeMethod("Reboot", null);
                }
            }
        }

【讨论】:

    【解决方案2】:

    Windows SUS 非常适合网络上的多台机器。

    【讨论】:

      【解决方案3】:

      “最简单”的判断方法是将 Windows 更新设置为每晚发生并下载更新(如果有),然后将更新盾牌图标放在系统托盘中。只需看一眼托盘即可查看图标是否存在。

      您还可以将 Windows 设置为每晚检查更新,然后在指定时间下载并安装它们。

      【讨论】:

        【解决方案4】:

        关于 mdsindzeleta 所说的 - 以编程方式进行此操作可能不是最佳解决方案。我会使用 Windows XP 内置的功能来下载和安装更新。我假设 Vista 有类似的功能。

        【讨论】:

          【解决方案5】:

          我相信 Windows 更新是使用 BITS 服务下载的。您可以使用 Windows 支持工具中的 Bitsadmin.exe。您可以从命令行运行 bitsadmin.exe /list 并查看 BITS 作业的状态。 (即下载进度、作业名称、作业状态)

          【讨论】:

            【解决方案6】:

            最后,Windows SUS 不是一个选项,所以我在批处理文件中使用以下内容和ActiveState ActivePerl(推荐):

            perl -nle "print $_ if m/updates detected/i"

            这可能是粗糙或肮脏的,并且将来可能会损坏,但它目前可以满足我的需要。

            感谢所有的想法。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2017-09-21
              • 2011-01-17
              • 2014-06-14
              • 2017-05-23
              • 2018-03-07
              • 1970-01-01
              • 2023-03-12
              • 1970-01-01
              相关资源
              最近更新 更多