private static void InstallUpdates(UpdateCollection installableUpdates) { Logger.Write("Trying to install update(s)."); try { UpdateSession uSession = new UpdateSession(); UpdateDownloader uDownloader = uSession.CreateUpdateDownloader(); IUpdateInstaller uInstaller = uSession.CreateUpdateInstaller(); if (installableUpdates.Count != 0) { Logger.Write(installableUpdates.Count + " update(s) to install."); uInstaller.ClientApplicationID = "InstallPendingUpdates"; uInstaller.AllowSourcePrompts = false; uDownloader.ClientApplicationID = "InstallPendingUpdates"; uDownloader.Updates = installableUpdates; Logger.Write("Starting to download update(s)."); uDownloader.Download(); Logger.Write("Download finnish."); uInstaller.Updates = installableUpdates; Logger.Write("Starting to install " + installableUpdates.Count + " update(s)."); IInstallationResult installResult = uInstaller.Install(); OperationResultCode resultCode = installResult.ResultCode; Logger.Write("Finnish to install update(s). Result : " + resultCode.ToString()); } else { Logger.Write("No udpdate to install."); } } catch (Exception ex) { Logger.Write("Problem when installing update(s). " + ex.Message); } }
public static void UpdateHist(Server server, bool initiated) { try { Type t = Type.GetTypeFromProgID("Microsoft.Update.Session", server.Location); //wcswapp01.intellig.local OH0KLT733D7S2.global.ds.honeywell.com labengdemctl00.labmasoh.local wcbuildapp02.intellig.local UpdateSession session = (UpdateSession)Activator.CreateInstance(t); IUpdateSearcher updateSearcher = session.CreateUpdateSearcher(); int count = updateSearcher.GetTotalHistoryCount(); IUpdateHistoryEntryCollection history = updateSearcher.QueryHistory(0, count); DateTime current = DateTime.Now; for (int i = 0; i < count; ++i) { if (initiated == true) { int hecc = current.Day * (-1); if (history[i].Date >= current.AddMonths(-1).AddDays(hecc)) { Info temp = new Info(); IUpdateIdentity ID = history[i].UpdateIdentity; OperationResultCode operationResult = history[i].ResultCode; string Result = operationResult.ToString(); if (Result == "orcSucceeded") { temp.TestResults = "Succeeded"; temp.UpdateStatus = "Succeeded"; } else { temp.TestResults = "Failed"; temp.UpdateStatus = "Failed"; } temp.TestDate = history[i].Date; temp.updateID = ID.UpdateID; temp.Title = history[i].Title; temp.ICW = server.ICW; temp.Server = server.ServerName; temp.Active = "Yes"; InfoList.Add(temp); } } else { Info temp = new Info(); IUpdateIdentity ID = history[i].UpdateIdentity; OperationResultCode operationResult = history[i].ResultCode; string Result = operationResult.ToString(); if (Result == "orcSucceeded") { temp.TestResults = "Succeeded"; temp.UpdateStatus = "Succeeded"; } else { temp.TestResults = "Failed"; temp.UpdateStatus = "Failed"; } temp.TestDate = history[i].Date; temp.updateID = ID.UpdateID; temp.Title = history[i].Title; temp.ICW = server.ICW; temp.Server = server.ServerName; temp.Active = "Yes"; //### Uncomment to ignore the updates older than 7 months //if (temp.TestDate <= current.AddMonths(-7)) int y = current.Year; DateTime year = new DateTime(y, 1, 1); if (temp.TestDate <= year) { break; } InfoList.Add(temp); } } } catch (Exception ex) { throw ex; } }