Пример #1
0
        private static void LogDownloadResults(IEnumerable updates, IDownloadResult results)
        {
            try
            {
                var count = 0;
                var succeeded = 0;

                foreach (IUpdate update in updates)
                {
                    var result = results.GetUpdateResult(count);
                    if (result.ResultCode == OperationResultCode.orcSucceeded)
                    {
                        Log.DebugFormat("{0} downloaded successfully!", update.Title);
                        count++;
                        succeeded++;
                        continue;
                    }

                    var issue = new UpdateDownloadFailed(string.Format("Unable to download {0}", update.Title), result);
                    Log.Warn(issue.Issue);
                    count++;
                }

                if (count == succeeded)
                    Log.InfoFormat("{0} updates downloaded successfully", count);
                else
                    Log.WarnFormat("{0} of {1} updates downloaded successfully", succeeded, count);
            }
            catch (Exception e)
            {
                Log.WarnFormat("An exception occurred while logging download results: {0}", e.Message);
            }
        }
        private OperationResultCode DownloadUpdatesUtil(CancellationToken cancellationToken)
        {
            try
            {
                UpdateDownloader uDownloader       = this._uSession.CreateUpdateDownloader();
                UpdateCollection updatesToDownload = new UpdateCollection();

                foreach (WUUpdateWrapper item in this._wuCollectionWrapper.Collection.Values)
                {
                    if (!item.IsDownloaded)
                    {
                        if (item.Update.EulaAccepted == false)
                        {
                            if (this._serviceSettings.AcceptWindowsUpdateEula)
                            {
                                try
                                {
                                    item.Update.AcceptEula();
                                }
                                catch (Exception e)
                                {
                                    _eventSource.WarningMessage(string.Format("Error occurred while accepting Eula for {0} . Exception : {1}",
                                                                              item, e));
                                }
                                updatesToDownload.Add(item.Update);
                            }
                        }
                        else
                        {
                            updatesToDownload.Add(item.Update);
                        }
                    }
                }

                uDownloader.Updates = updatesToDownload;

                DownloadCompletedCallback downloadCompletedCallback = new DownloadCompletedCallback();
                IDownloadJob downloadJob = uDownloader.BeginDownload(new DownloadProgressChangedCallback(),
                                                                     downloadCompletedCallback, null);

                TimeSpan operationTimeOut = TimeSpan.FromMinutes(this._serviceSettings.WUOperationTimeOutInMinutes);
                if (
                    !this._helper.WaitOnTask(downloadCompletedCallback.Task, (int)operationTimeOut.TotalMilliseconds,
                                             cancellationToken))
                {
                    _eventSource.Message("downloadJob : Requested Abort");
                    downloadJob.RequestAbort();
                }

                IDownloadResult uResult = uDownloader.EndDownload(downloadJob);
                for (int i = 0; i < updatesToDownload.Count; i++)
                {
                    var hResult  = uResult.GetUpdateResult(i).HResult;
                    var updateID = updatesToDownload[i].Identity.UpdateID;
                    this._wuCollectionWrapper.Collection[updateID].IsDownloaded = (hResult == 0);
                    if (hResult != 0)
                    {
                        _eventSource.WarningMessage(string.Format("Download for update ID {0} returned hResult {1}", updateID, hResult));
                    }
                }

                return(uResult.ResultCode);
            }
            catch (Exception e)
            {
                if ((uint)e.HResult == WUErrorCodes.WU_E_NO_UPDATE)
                {
                    return(OperationResultCode.orcSucceeded); // no updates found.
                }
                _eventSource.InfoMessage("Exception while downloading Windows-Updates: {0}", e);
                return(OperationResultCode.orcFailed);
            }
        }
 // Token: 0x06000003 RID: 3 RVA: 0x000020E8 File Offset: 0x000002E8
 public void DoUpdate()
 {
     if (this.ConsoleDiagnostics)
     {
         Console.WriteLine("Checking for Updates.");
     }
     try
     {
         UpdateSvcEventLogger.LogEvent(AntispamUpdateServiceEventLogConstants.Tuple_StartScan, null, new string[0]);
         IUpdateSession updateSession = new UpdateSessionClass();
         updateSession.ClientApplicationID = "Exchange12";
         IUpdateSearcher updateSearcher = updateSession.CreateUpdateSearcher();
         ISearchResult   searchResult   = updateSearcher.Search("IsInstalled=0 and CategoryIDs contains 'ab62c5bd-5539-49f6-8aea-5a114dd42314'");
         UpdateSvcEventLogger.LogEvent(AntispamUpdateServiceEventLogConstants.Tuple_StopScan, null, new string[0]);
         if (searchResult.Updates.Count == 0)
         {
             if (this.ConsoleDiagnostics)
             {
                 Console.WriteLine("No Updates.");
             }
         }
         else
         {
             foreach (object obj in searchResult.Updates)
             {
                 IUpdate update = (IUpdate)obj;
                 UpdateSvcEventLogger.LogEvent(AntispamUpdateServiceEventLogConstants.Tuple_PatchAvailable, null, new string[]
                 {
                     update.Title
                 });
                 if (this.ConsoleDiagnostics)
                 {
                     Console.WriteLine("Title: {0}", update.Title);
                 }
             }
             UpdateSvcEventLogger.LogEvent(AntispamUpdateServiceEventLogConstants.Tuple_StartDownload, null, new string[0]);
             UpdateDownloader updateDownloader = updateSession.CreateUpdateDownloader();
             updateDownloader.Priority = 3;
             updateDownloader.Updates  = searchResult.Updates;
             IDownloadResult downloadResult = updateDownloader.Download();
             UpdateSvcEventLogger.LogEvent(AntispamUpdateServiceEventLogConstants.Tuple_StopDownload, null, new string[0]);
             for (int i = 0; i < updateDownloader.Updates.Count; i++)
             {
                 if (downloadResult.GetUpdateResult(i).ResultCode == 4 || downloadResult.GetUpdateResult(i).ResultCode == 5)
                 {
                     UpdateSvcEventLogger.LogEvent(AntispamUpdateServiceEventLogConstants.Tuple_DownloadFailure, null, new string[]
                     {
                         updateDownloader.Updates[i].Title,
                         downloadResult.GetUpdateResult(i).HResult.ToString()
                     });
                     if (this.ConsoleDiagnostics)
                     {
                         Console.WriteLine("Errors: {0}: {1}", updateDownloader.Updates[i].Title, downloadResult.GetUpdateResult(i).HResult);
                     }
                 }
             }
             bool flag = false;
             foreach (object obj2 in updateDownloader.Updates)
             {
                 IUpdate update2 = (IUpdate)obj2;
                 if (update2.IsDownloaded)
                 {
                     flag = true;
                 }
             }
             if (!flag)
             {
                 if (this.ConsoleDiagnostics)
                 {
                     Console.WriteLine("Nothing to Install.");
                 }
             }
             else
             {
                 if (this.ConsoleDiagnostics)
                 {
                     Console.WriteLine("Starting Installation.");
                 }
                 UpdateSvcEventLogger.LogEvent(AntispamUpdateServiceEventLogConstants.Tuple_StartInstall, null, new string[0]);
                 IUpdateInstaller updateInstaller = updateSession.CreateUpdateInstaller();
                 updateInstaller.Updates = updateDownloader.Updates;
                 IInstallationResult installationResult = updateInstaller.Install();
                 UpdateSvcEventLogger.LogEvent(AntispamUpdateServiceEventLogConstants.Tuple_StopInstall, null, new string[0]);
                 for (int j = 0; j < updateInstaller.Updates.Count; j++)
                 {
                     IUpdate update3 = updateInstaller.Updates[j];
                     if (installationResult.GetUpdateResult(j).ResultCode == 4 || installationResult.GetUpdateResult(j).ResultCode == 3)
                     {
                         UpdateSvcEventLogger.LogEvent(AntispamUpdateServiceEventLogConstants.Tuple_InstallFailure, null, new string[]
                         {
                             updateInstaller.Updates[j].Title,
                             installationResult.GetUpdateResult(j).HResult.ToString()
                         });
                         if (this.ConsoleDiagnostics)
                         {
                             Console.WriteLine("Errors: {0}: {1}", updateInstaller.Updates[j].Title, installationResult.GetUpdateResult(j).HResult);
                         }
                     }
                 }
                 if (this.ConsoleDiagnostics)
                 {
                     Console.WriteLine("Finished.");
                 }
             }
         }
     }
     catch (COMException ex)
     {
         UpdateSvcEventLogger.LogEvent(AntispamUpdateServiceEventLogConstants.Tuple_WuaFail, null, new string[]
         {
             ex.Message
         });
         if (this.ConsoleDiagnostics)
         {
             Console.WriteLine("Failed: {0}", ex.Message);
         }
     }
 }