public WindowsUpdateAgent() { this.mUpdateSearcher = this.mUpdateSession.CreateUpdateSearcher(); int totalHistoryCount = this.mUpdateSearcher.GetTotalHistoryCount(); if (totalHistoryCount > 0) { this.mUpdateHistoryCollection = this.mUpdateSearcher.QueryHistory(0, totalHistoryCount); Regex regex = new Regex(@"KB\d{5,5}"); foreach (IUpdateHistoryEntry entry in this.mUpdateHistoryCollection) { Match match = regex.Match(entry.Title); if (match.Success) { if (this.mInstalledUpdates.ContainsKey(match.Value)) { if (this.mInstalledUpdates[match.Value].RevisionNumber < Convert.ToInt32(entry.UpdateIdentity.RevisionNumber)) { this.mInstalledUpdates[match.Value] = new WindowsUpdate(match.Value, entry.Title, new Guid(entry.UpdateIdentity.UpdateID), Convert.ToInt32(entry.UpdateIdentity.RevisionNumber)); } } else { this.mInstalledUpdates.Add(match.Value, new WindowsUpdate(match.Value, entry.Title, new Guid(entry.UpdateIdentity.UpdateID), Convert.ToInt32(entry.UpdateIdentity.RevisionNumber))); } } } regex = null; this.mUpdateHistoryCollection = null; } this.mUpdateSearcher = null; this.mUpdateSession = null; }
static void GetWindowsUpdatesHistory(IUpdateSearcher uSearcher) { int count = uSearcher.GetTotalHistoryCount(); IUpdateHistoryEntryCollection collections = uSearcher.QueryHistory(0, count); UpdateIdsHistory = new List <Dictionary <string, string> >(); foreach (var item in collections) { try { IUpdateHistoryEntry2 historyEntry = (IUpdateHistoryEntry2)item; Dictionary <string, string> hu = new Dictionary <string, string>(); hu["Title"] = historyEntry.Title; hu["UpdateID"] = historyEntry.UpdateIdentity.UpdateID.ToString(); hu["RevisionNumber"] = historyEntry.UpdateIdentity.RevisionNumber.ToString(); //foreach (var item1 in hu) //Console.WriteLine(item1.Key + " : " + item1.Value); UpdateIdsHistory.Add(hu); } catch (Exception ex) { Console.WriteLine(ex); } //Console.WriteLine(); } }
public void GetWindowsUpdates(StreamWriter writer) { writer.WriteLine("- Windows Update History:"); UpdateSession updateSession = new UpdateSession(); IUpdateSearcher updateSearcher = updateSession.CreateUpdateSearcher(); IUpdateHistoryEntryCollection updateHistoryCollection = updateSearcher.QueryHistory(0, updateSearcher.GetTotalHistoryCount()); foreach (IUpdateHistoryEntry updateHistory in updateHistoryCollection) { if (updateHistory.Date == new DateTime(1899, 12, 30)) { continue; } writer.WriteLine("Date: " + updateHistory.Date.ToLongDateString() + " " + updateHistory.Date.ToLongTimeString()); writer.WriteLine("Title: " + (updateHistory.Title != null ? updateHistory.Title : "")); writer.WriteLine("Description: " + (updateHistory.Description != null ? updateHistory.Description : "")); writer.WriteLine("Operation: " + updateHistory.Operation); writer.WriteLine("Result Code: " + updateHistory.ResultCode); writer.WriteLine("Support URL: " + (updateHistory.SupportUrl != null ? updateHistory.SupportUrl : "")); writer.WriteLine("Update ID: " + (updateHistory.UpdateIdentity.UpdateID != null ? updateHistory.UpdateIdentity.UpdateID : "")); writer.WriteLine("Update Revision: " + (updateHistory.UpdateIdentity.RevisionNumber.ToString() != null ? updateHistory.UpdateIdentity.RevisionNumber.ToString() : "")); writer.WriteLine(); } }
public void Init() { AppLog.Line(Program.fmt("Windows Update Manager, Version v{0} by David Xanatos", mVersion)); AppLog.Line(Program.fmt("This Tool is Open Source under the GNU General Public License, Version 3\r\n")); mUpdateSession = new UpdateSession(); mUpdateSession.ClientApplicationID = "Windows Update Manager"; mUpdateServiceManager = new UpdateServiceManager(); foreach (IUpdateService service in mUpdateServiceManager.Services) { if (service.Name == "Offline Sync Service") { mUpdateServiceManager.RemoveService(service.ServiceID); } else { mServiceList.Add(service.Name); } } mUpdateSearcher = mUpdateSession.CreateUpdateSearcher(); int count = mUpdateSearcher.GetTotalHistoryCount(); mUpdateHistory = mUpdateSearcher.QueryHistory(0, count); WindowsUpdateAgentInfo info = new WindowsUpdateAgentInfo(); var currentVersion = info.GetInfo("ApiMajorVersion").ToString().Trim() + "." + info.GetInfo("ApiMinorVersion").ToString().Trim() + " (" + info.GetInfo("ProductVersionString").ToString().Trim() + ")"; AppLog.Line(Program.fmt("Windows Update Agent Version: {0}", currentVersion)); }
void LoadList(IUpdateHistoryEntryCollection List) { if (List != null) { foreach (IUpdateHistoryEntry update in List) { updateView.AddObject(new Update(update)); } } }
public List <InstalledUpdate> GetInstalledUpdates() { IUpdateSearcher updateSearcher = GetNewSearcher(online: false); int count = updateSearcher.GetTotalHistoryCount(); IUpdateHistoryEntryCollection history = updateSearcher.QueryHistory(0, count); List <InstalledUpdate> installedUpdates = new List <InstalledUpdate>(); for (int i = 0; i < count; ++i) { installedUpdates.Add(new InstalledUpdate(Title: history[i].Title, InstalledDate: history[i].Date)); } return(installedUpdates); }
private static bool CheckWindowsUpdate(string kb) { try { UpdateSession session1 = (UpdateSession)Activator.CreateInstance(Marshal.GetTypeFromCLSID(new Guid("4CB43D7F-7EEE-4906-8698-60DA1C38F2FE"))); IUpdateSearcher searcher = session1.CreateUpdateSearcher(); searcher.Online = false; IUpdateHistoryEntryCollection entrys = session1.QueryHistory(string.Empty, 0, searcher.GetTotalHistoryCount()); for (int i = 0; i < entrys.Count; i++) { IUpdateHistoryEntry entry = entrys[i]; if (((entry != null) && (entry.Title != null)) && entry.Title.Contains(kb)) { return(true); } } return(false); } catch (Exception) { return(true); } }
/* * Use WUApi to retrieve the update with the latest successful install date */ public static DateTime GetLastWindowsUpdatesInstalledDate(String serverName) { // Create WUApi instance and intialize return variable Type t = Type.GetTypeFromProgID("Microsoft.Update.Session", serverName); DateTime dt = DateTime.MinValue; try { //Call Update interface and search to get total number of updates installed //Then call interface to retrieve all updates. UpdateSession session = (UpdateSession)Activator.CreateInstance(t); IUpdateSearcher updateSearcher = session.CreateUpdateSearcher(); int count = updateSearcher.GetTotalHistoryCount(); IUpdateHistoryEntryCollection history = updateSearcher.QueryHistory(0, count); //Loop through and overwrite return date variable if install date is later. foreach (IUpdateHistoryEntry he in history) { try { if (he.ResultCode == OperationResultCode.orcSucceeded && dt < he.Date) { dt = he.Date; } } catch (Exception ex) { WriteErrorLog(serverName + ":" + ex.Message); } } } catch (Exception ex) { WriteErrorLog(serverName + ":" + ex.Message); } return(dt); }
protected override void ProcessRecord() { base.ProcessRecord(); updateHistory = Globals.updateSearcher.QueryHistory(startIndex, Count); }
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; } }
private string GetDateInstalled(IUpdateHistoryEntryCollection history, string updateID) { string NoDate = String.Empty; if (history == null) return NoDate; foreach (IUpdateHistoryEntry entry in history) { if (entry.UpdateIdentity.UpdateID.Equals(updateID)) return entry.Date.ToShortDateString(); } return NoDate; }
/// <summary> /// Convert an IUpdate (MS Update model) object to an Update (TopPatch model). /// </summary> /// <param name="iUpdate"></param> /// <returns></returns> private Update ConvertToUpdate(IUpdate iUpdate, IUpdateHistoryEntryCollection history = null) { Update tpUpdate = new Update { Name = iUpdate.Title, VendorId = iUpdate.Identity.UpdateID, FileSize = iUpdate.MaxDownloadSize.ToString(), Description = iUpdate.Description, SupportURL = (iUpdate.MoreInfoUrls.Count <= 0) ? String.Empty : iUpdate.MoreInfoUrls[0], Severity = GetTopPatchSeverity(iUpdate.MsrcSeverity), KB = GetKbString(iUpdate.Title), // Get the KB from the title string. // Getting the date installed of an update is tricky. // Have to go through leaps and bounds. Thank you Microsoft!! DateInstalled = GetDateInstalled(history, iUpdate.Identity.UpdateID), DatePublished = iUpdate.LastDeploymentChangeTime.ToShortDateString(), IsHidden = iUpdate.IsHidden, IsInstalled = iUpdate.IsInstalled }; tpUpdate.TopPatchId = Utils.TopPatch.GenerateTopPatchId(tpUpdate); return tpUpdate; }