2008-09-23 23 views
3

我特別感興趣的是Windows 2000/XP,但Vista/7也會很有趣(如果不同)。檢測Windows是否有Windows Update可以下載/安裝的最佳方法?

我一直在考慮按日程安排批處理文件或等同物的任務。

編輯:對不起,我應該提供更多信息。問題屬於我手動應用更新的10臺機器。我不想以編程方式安裝更新程序,只需使用批處理或腳本查看是否有更新可供下載/安裝(即系統托盤中的更新屏蔽圖標指示此問題)。謝謝。

+0

我的答案顯然是一個網絡。你的問題的範圍是什麼?一臺機器?一個網絡?幾個網絡上的幾臺機器? – 2008-09-23 15:14:54

回答

0

最終,Windows SUS不是一種選擇,所以我使用批處理文件結合以下與ActiveState ActivePerl(推薦):

perl的-Nle 「打印$ _如果檢測到M /更新/我」 < C:\ WINDOWS \ windowsupdate.log處

這可能是原油或骯髒,並且在未來有可能打破,但它目前做我所需要的。

感謝所有的想法。

0

「最簡單的」告訴方法是將Windows更新設置爲每晚進行並下載更新(如果可用),然後將更新屏蔽圖標置於系統托盤中。只需看一眼托盤,看看圖標是否存在。

您也可以設置Windows來每晚檢查更新,然後在指定的時間下載並安裝它們。

0

關於什麼mdsindzeleta說 - 以編程方式進行可能不是最好的解決方案。我會使用Windows XP內置的功能來下載和安裝更新。我假設Vista具有類似的功能。

0

我相信Windows更新是使用BITS服務下載的。您可以使用Windows支持工具中的Bitsadmin.exe。從命令行你可以運行bitsadmin.exe/list,你可以看到BITS作業的狀態。 (即下載進度,作業名稱,作業狀態)

3

你可以使用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); 
       } 
      } 
     } 
相關問題