Best way of detecting if Windows has Windows Updates ready to download/install?

蹲街弑〆低调 提交于 2020-01-21 08:36:48

问题


I'm specifically interested in Windows 2000/XP, but Vista/7 would be interesting too (if different).

I was thinking along the lines of task scheduling a batch file or equivalent on a daily basis.

EDIT: Sorry, I should have provided more info. The question pertains to 10 machines which I manually apply updates. I don't want to install the updates programatically, but just find out if there are updates ready to download/install (i.e. the update shield icon in the system tray is indicating this) using a batch or script. Thanks.


回答1:


You could use 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 works very well for several machines on a network.




回答3:


The "easiest" way to tell is to setup Windows Updates to occur nightly and download the updates if available which then puts the update shield icon in the system tray. Just glance at the tray to see if the icon is present.

You could also setup Windows to check nightly for the updates, then download and install them at a specified time.




回答4:


In regards to what mdsindzeleta said - going about this programatically probably isn't the best solution. I would use the features built into Windows XP to download and install updates. I'm assuming that Vista has similar features.




回答5:


I believe Windows updates are downloaded using the BITS service. You could use Bitsadmin.exe found in the Windows Support Tools. From the command line you can run bitsadmin.exe /list and you can see the status of BITS jobs. (i.e. download progress, job name, job status)




回答6:


In the end, Windows SUS wasn't an option, so I'm using the following in a batch file conjunction with ActiveState ActivePerl (recommended):

perl -nle "print $_ if m/updates detected/i" < c:\Windows\WindowsUpdate.log

This might be crude or dirty, and might break in future, but it currently does what I need.

Thanks for all the ideas.



来源:https://stackoverflow.com/questions/121585/best-way-of-detecting-if-windows-has-windows-updates-ready-to-download-install

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!