代码之家  ›  专栏  ›  技术社区  ›  Umber Ferrule Gokhan Tank

检测Windows是否已准备好下载/安装Windows更新的最佳方法?

  •  3
  • Umber Ferrule Gokhan Tank  · 技术社区  · 16 年前

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

    我一直在考虑每天调度一个批处理文件或等效文件的任务。

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

    6 回复  |  直到 10 年前
        1
  •  3
  •   Danny Beckett    10 年前

    你可以使用 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
  •  1
  •   Austin Salonen gmlacrosse    16 年前

    Windows SUS 对于网络上的多台机器来说工作得很好。

        3
  •  0
  •   mdzindzeleta    16 年前

    最简单的方法是将Windows更新设置为每晚发生,并下载更新(如果可用),然后将更新屏蔽图标放入系统托盘。只需瞥一眼托盘,看看图标是否存在。

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

        4
  •  0
  •   Thomas Owens    16 年前

    至于Mdsindzeleta所说的,从程序上讲,这可能不是最好的解决方案。我将使用WindowsXP内置的功能来下载和安装更新。我假设Vista有类似的功能。

        5
  •  0
  •   Cj Anderson    15 年前

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

        6
  •  0
  •   Umber Ferrule Gokhan Tank    15 年前

    最后, Windows SUS 不是一个选项,所以我将以下内容与 ActiveState ActivePerl (推荐):

    perl-nle“如果检测到m/updates/i,则打印$”<c:\windows\windowsupdate.log

    这可能是粗糙或肮脏的,并可能在未来打破,但它目前做我需要的。

    谢谢你的建议。

    推荐文章