private void MakeVisibleInWsusConsole(IUpdate PublishedUpdate) { Logger.EnteringMethod(); SqlHelper sqlHelper = SqlHelper.GetInstance(); string sqlServerName = _wsus.GetSqlServerName(); string sqlDataBaseName = _wsus.GetSqlDataBaseName(); System.Version wsusVersion = _wsus.GetServerVersion(); if (PublishedUpdate == null) { return; } if (sqlServerName.Contains("MICROSOFT##SSEE") || sqlServerName.Contains("MICROSOFT##WID")) { if (wsusVersion.Major == 3) { sqlHelper.ServerName = @"\\.\pipe\MSSQL$MICROSOFT##SSEE\sql\query"; } if (wsusVersion.Major == 6) { sqlHelper.ServerName = @"\\.\pipe\Microsoft##WID\tsql\query"; } sqlHelper.DataBaseName = "SUSDB"; } else { sqlHelper.ServerName = _wsus.GetSqlServerName(); sqlHelper.DataBaseName = _wsus.GetSqlDataBaseName(); } Logger.Write(sqlHelper.ServerName); Logger.Write(sqlHelper.DataBaseName); if (sqlHelper.Connect(string.Empty, string.Empty)) { Logger.Write("Connected to SQL."); List <Guid> updateIDs = new List <Guid>(); updateIDs.Add(PublishedUpdate.Id.UpdateId); UpdateCategoryCollection categories = PublishedUpdate.GetUpdateCategories(); foreach (IUpdateCategory category in categories) { if (!updateIDs.Contains(category.Id)) { updateIDs.Add(category.Id); } if (category.ProhibitsSubcategories && !category.ProhibitsUpdates) { IUpdateCategory parentCategory = category.GetParentUpdateCategory(); if (!updateIDs.Contains(parentCategory.Id)) { updateIDs.Add(parentCategory.Id); } } } sqlHelper.ShowUpdatesInConsole(updateIDs); sqlHelper.Disconnect(); } }
/// <summary> /// Gets a list of update categories by searching for the guids specified in the config /// </summary> /// <param name="server"> /// wsus server connection /// </param> /// <param name="products"> /// list of product guids from config /// </param> /// <returns> /// collection of update categories /// </returns> private static UpdateCategoryCollection GetUpdateCategoryCollection( IUpdateServer server, ProductCollection products) { if (products == null) { throw new ArgumentNullException("products"); } if (products.Count < 1) { throw new ArgumentException("products has no product items."); } var result = new UpdateCategoryCollection(); foreach (Product product in products) { IUpdateCategory category = server.GetUpdateCategory(product.Guid); result.Add(category); } return(result); }
/// <summary> /// checks for superseded updates and approves the new revision /// </summary> /// <param name="server"> /// The WSUS Server /// </param> /// <param name="approveLicenseAgreement"> /// Whether to approve any license agreement /// </param> /// <param name="computerTargetGroup"> /// The target group to check /// </param> /// <param name="classification"> /// Classification to process /// </param> /// <param name="products"> /// Collection of products to process /// </param> /// <param name="isTest"> /// Whether we are in test mode /// </param> /// <param name="shouldApproveUninstalledSupersededUpdate"> /// The should Approve Uninstalled Superseded Update. /// </param> private static void CheckSupersededUpdates( IUpdateServer server, bool approveLicenseAgreement, IComputerTargetGroup computerTargetGroup, IUpdateClassification classification, UpdateCategoryCollection products, bool isTest, bool shouldApproveUninstalledSupersededUpdate) { var searchScope = new UpdateScope { ApprovedStates = ApprovedStates.HasStaleUpdateApprovals }; if (computerTargetGroup != null) { searchScope.ApprovedComputerTargetGroups.Add(computerTargetGroup); } if (classification != null) { searchScope.Classifications.Add(classification); } if (products != null) { searchScope.Categories.AddRange(products); } UpdateCollection updates = server.GetUpdates(searchScope); if (updates.Count <= 0) { return; } var recentlyApproved = new List <Guid>(); if (updates.Count == 0) { Console.Out.WriteLine(" No updates required."); return; } foreach (IUpdate update in updates) { if (update.IsSuperseded) { CheckSupersededUpdate( update, approveLicenseAgreement, isTest, recentlyApproved, shouldApproveUninstalledSupersededUpdate); } } }
private int NumberOfVisibleCategory(UpdateCategoryCollection updateCategoryCollection) { Logger.EnteringMethod(); int number = 0; foreach (IUpdateCategory category in updateCategoryCollection) { if (category.UpdateSource == UpdateSource.MicrosoftUpdate) { number++; } } Logger.Write("Returning " + number.ToString()); return(number); }
/// <summary> /// The do run set classification. /// </summary> /// <param name="server"> /// Object representing the WSUS server /// </param> /// <param name="computerTargetGroup"> /// The target group to check /// </param> /// <param name="classification"> /// </param> /// <param name="isTest"> /// Whether we are in test mode /// </param> /// <param name="alreadyProcessed"> /// List of target groups that have already been processed /// </param> private static void DoRunSetClassification( IUpdateServer server, IComputerTargetGroup computerTargetGroup, Classification classification, bool isTest, List <IComputerTargetGroup> alreadyProcessed) { // check the classification exists on wsus IUpdateClassification uc = server.GetUpdateClassification(classification.Guid); // check for a product collection UpdateCategoryCollection products = classification.Products.ElementInformation.IsPresent ? GetUpdateCategoryCollection(server, classification.Products) : null; if (classification.ApproveStaleUpdates) { CheckStaleUpdates( server, classification.AcceptLicenseAgreement, computerTargetGroup, uc, products, isTest); } if (classification.ApproveSupersededUpdates) { CheckSupersededUpdates( server, classification.AcceptLicenseAgreement, computerTargetGroup, uc, products, isTest, classification.ShouldApproveUninstalledSupersededUpdate); } if (classification.ApproveNeededUpdates) { CheckClassification(server, uc, computerTargetGroup, isTest, alreadyProcessed); } }
/// <summary> /// Checks for updates that may have a new revision /// </summary> /// <param name="server"> /// The WSUS Server /// </param> /// <param name="approveLicenseAgreement"> /// Whether to approve any license agreement /// </param> /// <param name="computerTargetGroup"> /// The target group to check /// </param> /// <param name="classifications"> /// Classification to process /// </param> /// <param name="products"> /// Collection of products to process /// </param> /// <param name="isTest"> /// Whether we are in test mode /// </param> private static void CheckStaleUpdates( IUpdateServer server, bool approveLicenseAgreement, IComputerTargetGroup computerTargetGroup, IUpdateClassification classifications, UpdateCategoryCollection products, bool isTest) { var searchScope = new UpdateScope { ApprovedStates = ApprovedStates.HasStaleUpdateApprovals }; if (computerTargetGroup != null) { searchScope.ApprovedComputerTargetGroups.Add(computerTargetGroup); } if (classifications != null) { searchScope.Classifications.Add(classifications); } if (products != null) { searchScope.Categories.AddRange(products); } UpdateCollection updates = server.GetUpdates(searchScope); if (updates.Count > 0) { foreach (IUpdate update in updates) { CheckStaleUpdate(update, isTest, approveLicenseAgreement); } } else { Console.Out.WriteLine(" No updates required."); } }
private static UpdateCollection getUpdates() { Console.WriteLine("Getting updates from WSUS.."); IUpdateServer server = AdminProxy.GetUpdateServer("localhost", false, 8530); UpdateScope scope = new UpdateScope(); UpdateCategoryCollection categories = server.GetUpdateCategories(); foreach (IUpdateCategory cat in categories) { if (cat.Description.Contains("10")) { Console.WriteLine(cat.Description); scope.Categories.Add(cat); } } int updateCount = server.GetUpdateCount(scope); Console.WriteLine($"Updates found: {updateCount}"); UpdateCollection updates = server.GetUpdates(scope); return(updates); }
/// <summary> /// checks for superseded updates and approves the new revision /// </summary> /// <param name="server"> /// The WSUS Server /// </param> /// <param name="approveLicenseAgreement"> /// Whether to approve any license agreement /// </param> /// <param name="computerTargetGroup"> /// The target group to check /// </param> /// <param name="classification"> /// Classification to process /// </param> /// <param name="products"> /// Collection of products to process /// </param> /// <param name="isTest"> /// Whether we are in test mode /// </param> /// <param name="shouldApproveUninstalledSupersededUpdate"> /// The should Approve Uninstalled Superseded Update. /// </param> private static void CheckSupersededUpdates( IUpdateServer server, bool approveLicenseAgreement, IComputerTargetGroup computerTargetGroup, IUpdateClassification classification, UpdateCategoryCollection products, bool isTest, bool shouldApproveUninstalledSupersededUpdate) { var searchScope = new UpdateScope { ApprovedStates = ApprovedStates.HasStaleUpdateApprovals }; if (computerTargetGroup != null) { searchScope.ApprovedComputerTargetGroups.Add(computerTargetGroup); } if (classification != null) { searchScope.Classifications.Add(classification); } if (products != null) { searchScope.Categories.AddRange(products); } UpdateCollection updates = server.GetUpdates(searchScope); if (updates.Count <= 0) { return; } var recentlyApproved = new List<Guid>(); if (updates.Count == 0) { Console.Out.WriteLine(" No updates required."); return; } foreach (IUpdate update in updates) { if (update.IsSuperseded) { CheckSupersededUpdate( update, approveLicenseAgreement, isTest, recentlyApproved, shouldApproveUninstalledSupersededUpdate); } } }
/// <summary> /// Gets a list of update categories by searching for the guids specified in the config /// </summary> /// <param name="server"> /// wsus server connection /// </param> /// <param name="products"> /// list of product guids from config /// </param> /// <returns> /// collection of update categories /// </returns> private static UpdateCategoryCollection GetUpdateCategoryCollection( IUpdateServer server, ProductCollection products) { if (products == null) { throw new ArgumentNullException("products"); } if (products.Count < 1) { throw new ArgumentException("products has no product items."); } var result = new UpdateCategoryCollection(); foreach (Product product in products) { IUpdateCategory category = server.GetUpdateCategory(product.Guid); result.Add(category); } return result; }
private void ChangeVisibiltyInWsusConsole(UpdateCollection updates, int status) { Logger.EnteringMethod(); SqlHelper sqlHelper = SqlHelper.GetInstance(); List <Guid> updateIDs = new List <Guid>(); string sqlServerName = _wsus.GetSqlServerName(); string sqlDataBaseName = _wsus.GetSqlDataBaseName(); System.Version wsusVersion = _wsus.GetServerVersion(); if (sqlServerName.Contains("MICROSOFT##SSEE") || sqlServerName.Contains("MICROSOFT##WID")) { if (wsusVersion.Major == 3) { sqlHelper.ServerName = @"\\.\pipe\MSSQL$MICROSOFT##SSEE\sql\query"; } if (wsusVersion.Major == 6) { sqlHelper.ServerName = @"\\.\pipe\Microsoft##WID\tsql\query"; } sqlHelper.DataBaseName = "SUSDB"; } else { sqlHelper.ServerName = _wsus.GetSqlServerName(); sqlHelper.DataBaseName = _wsus.GetSqlDataBaseName(); } if (sqlHelper.Connect(string.Empty, string.Empty)) { foreach (IUpdate update in updates) { updateIDs.Add(update.Id.UpdateId); if (status == 0) { sqlHelper.ShowUpdatesInConsole(updateIDs); } if (status == 1) { sqlHelper.HideUpdatesInConsole(updateIDs); } } updateIDs.Clear(); foreach (IUpdate update in updates) { UpdateCategoryCollection categories = update.GetUpdateCategories(); foreach (IUpdateCategory category in categories) { if (!updateIDs.Contains(category.Id) && (status == 0 || NumberOfVisibleUpdate(category.GetUpdates()) == 0)) { updateIDs.Add(category.Id); if (category.ProhibitsSubcategories && !category.ProhibitsUpdates) { IUpdateCategory parentCategory = category.GetParentUpdateCategory(); if (!updateIDs.Contains(parentCategory.Id) && (status == 0 || NumberOfVisibleCategory(parentCategory.GetSubcategories()) == 1)) { updateIDs.Add(parentCategory.Id); } } } } } if (status == 0) { sqlHelper.ShowUpdatesInConsole(updateIDs); } if (status == 1) { sqlHelper.HideUpdatesInConsole(updateIDs); } sqlHelper.Disconnect(); } }