Windows にダウンロード/インストールできる Windows Update があるかどうかを検出する最良の方法は何ですか?
-
02-07-2019 - |
質問
私は特に Windows 2000/XP に興味がありますが、(異なる場合は) Vista/7 も興味深いでしょう。
私は、バッチ ファイルまたは同等のタスクを毎日スケジュールするという方向に沿って考えていました。
編集:申し訳ありませんが、もっと情報を提供するべきでした。この質問は、更新を手動で適用した 10 台のマシンに関するものです。更新プログラムをプログラムでインストールしたくありませんが、ダウンロード/インストールの準備ができている更新プログラムがあるかどうかを確認するだけです (つまり、システム トレイの更新シールド アイコンは、バッチまたはスクリプトを使用していることを示しています。ありがとう。
解決 6
最後に、 WindowsSUS オプションではなかったので、バッチファイルで以下を組み合わせて使用しています ActiveState ActivePerl (推奨):
perl -nle "print $_ if m/updates detected/i" < c:\Windows\WindowsUpdate.log
これは粗雑で汚い可能性があり、将来壊れる可能性がありますが、現時点では必要な機能を果たしています。
すべてのアイデアをありがとう。
他のヒント
使用できます 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);
}
}
}
WindowsSUS ネットワーク上の複数のマシンでは非常にうまく機能します。
これを確認する「最も簡単な」方法は、Windows Update が毎晩行われるように設定し、利用可能な場合は更新プログラムをダウンロードし、システム トレイに更新シールド アイコンを表示することです。トレイを一目見て、アイコンが存在するかどうかを確認してください。
また、更新プログラムを毎晩チェックし、指定した時間にダウンロードしてインストールするように Windows をセットアップすることもできます。
mdsindzeleta が言ったことに関しては、プログラム的にこれを行うことはおそらく最良の解決策ではありません。Windows XP に組み込まれている機能を使用して、更新プログラムをダウンロードしてインストールします。Vistaにも同様の機能があると思います。
Windows 更新プログラムは BITS サービスを使用してダウンロードされると思います。Windows サポート ツールにある Bitsadmin.exe を使用できます。コマンド ラインから bitsadmin.exe /list を実行すると、BITS ジョブのステータスを確認できます。(すなわち、ダウンロードの進行状況、ジョブ名、ジョブのステータス)