检测Windows是否已准备好下载/安装Windows更新的最佳方法?
时间:2020-03-06 14:36:15 来源:igfitidea点击:
我对Windows 2000 / XP尤其感兴趣,但是Vista / 7也会很有趣(如果有所不同)。
我一直在考虑每天计划一个批处理文件或者等效文件的任务。
编辑:对不起,我应该提供更多的信息。这个问题与我手动应用更新的10台计算机有关。我不想以编程方式安装更新,而只是使用批处理或者脚本找出是否有准备好下载/安装的更新(即系统托盘中的更新防护图标指示此更新)。谢谢。
解决方案
Windows SUS在网络上的多台计算机上都可以很好地工作。
最简单的方法是将Windows Updates设置为每晚进行一次,并下载更新(如果有),然后将更新防护图标放入系统托盘中。只需看一眼托盘即可查看该图标是否存在。
我们还可以将Windows设置为每晚检查更新,然后在指定的时间下载并安装它们。
关于mdsindzeleta所说的以编程方式进行操作可能不是最佳解决方案。我将使用Windows XP内置的功能来下载和安装更新。我假设Vista具有类似的功能。
我相信Windows更新是使用BITS服务下载的。我们可以使用Windows支持工具中的Bitsadmin.exe。在命令行中,我们可以运行bitsadmin.exe / list,并且可以查看BITS作业的状态。 (即下载进度,工作名称,工作状态)
我们可以使用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);
}
}
}
最后,Windows SUS并不是一个选择,因此我在批处理文件中将以下内容与ActiveState ActivePerl结合使用(推荐):
perl -nle"如果检测到m / updates / i,则打印$ _" <c:\ Windows \ WindowsUpdate.log
这可能是粗糙的或者肮脏的,并且可能在将来中断,但是当前它可以满足我的需求。
感谢所有想法。

