Beispiel #1
0
        /// <summary>
        /// Checks a classification for needed updates
        /// </summary>
        /// <param name="server">
        /// Object representing the WSUS server
        /// </param>
        /// <param name="classification">
        /// a single update classification
        /// </param>
        /// <param name="rootGroup">
        /// the "all computers" group
        /// </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 CheckClassification(
            IUpdateServer server,
            IUpdateClassification classification,
            IComputerTargetGroup rootGroup,
            bool isTest,
            List <IComputerTargetGroup> alreadyProcessed)
        {
            Console.Out.WriteLine("Getting list of updates for : " + classification.Title);

            var searchScope = new UpdateScope {
                IncludedInstallationStates = UpdateInstallationStates.NotInstalled
            };

            searchScope.Classifications.Add(classification);
            UpdateCollection updates = server.GetUpdates(searchScope);

            if (updates.Count > 0)
            {
                DoClassificationUpdates(updates, rootGroup, isTest, alreadyProcessed);
            }
            else
            {
                Console.Out.WriteLine(" No updates required.");
            }
        }
Beispiel #2
0
        private List <Dictionary <string, string> > GetUpdates(IUpdateServer wsus, string name = "")
        {
            List <Dictionary <string, string> > retList = new List <Dictionary <string, string> >();
            UpdateCollection updates = new UpdateCollection();

            if (name == "")
            {
                updates = wsus.GetUpdates();
            }
            else
            {
                updates = wsus.SearchUpdates(name);
            }
            foreach (IUpdate update in updates)
            {
                if (update.Description == "Carteiro Update Package")
                {
                    Dictionary <string, string> details = new Dictionary <string, string>();
                    details.Add("Id", update.Id.UpdateId.ToString());
                    details.Add("Title", update.Title);
                    details.Add("Description", update.Description.Trim());
                    details.Add("CreationDate", update.CreationDate.ToString());
                    details.Add("IsApproved", update.IsApproved.ToString());
                    details.Add("IsDeclined", update.IsDeclined.ToString());
                    retList.Add(details);
                }
            }
            return(retList);
        }
Beispiel #3
0
        static void Main(string[] args)
        {
            //Create IUpdateServer variable and connect to WSUS Server

            //replace the "REPLACETHIS" with your WSUS Server Name, SSL or NOT, and Port. Syntax here is (SERVERNAME, REQUIRESSL, PORT)
            IUpdateServer server = AdminProxy.GetUpdateServer("REPLACETHIS", true, 443);

            //Begin loop thru updates and filter updates to just unapproved updates from beginning of time to current time
            foreach (IUpdate update in server.GetUpdates(ApprovedStates.NotApproved, DateTime.MinValue, DateTime.MaxValue, null, null))
            {
                //Convert UpdateType to string for comparison in following if statement
                string updatetype = update.UpdateType.ToString();

                //Filter for driver updates only
                if (updatetype == "Driver")
                {
                    //Write update name to console
                    Console.WriteLine("Declining update {0}", update.Title);
                    //Decline driver update
                    update.Decline();
                    //Write confimation to console
                    Console.WriteLine("Update Successfully Declined");
                }
            }


            //Wait at end of program
            string strWaitText = Environment.NewLine + "Press return to close";

            Console.WriteLine(strWaitText);
            Console.ReadLine();
        }
Beispiel #4
0
        /// <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);
                }
            }
        }
Beispiel #5
0
        /// <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.");
            }
        }
Beispiel #6
0
        public static void PopulateUpdateResults(IUpdateServer server, UpdateResults resultobject)
        {
            Console.WriteLine("Reading update information. This may take several minutes. Please wait...");
            List <UpdateCollection> collections = new List <UpdateCollection>();
            var approvedstates = Enum.GetValues(typeof(ApprovedStates));

            //This breaks the query up. If we use a straight GetUpdates() it is likely to timeout. This
            //allows the query to be filtered to give more control.
            foreach (IUpdateClassification classification in server.GetUpdateClassifications())
            {
                //Drivers cause timeouts so we skip them
                if (classification.Title == "Drivers")
                {
                    continue;
                }
                Console.Write("Processing " + classification.Title);
                UpdateClassificationCollection classcol = new UpdateClassificationCollection();
                classcol.Add(classification);
                foreach (ApprovedStates state in approvedstates)
                {
                    //Console.WriteLine(state);
                    Console.Write(".");
                    collections.Add(server.GetUpdates(state, DateTime.MinValue, DateTime.MaxValue, null, classcol));
                }
                Console.WriteLine();
            }

            foreach (UpdateCollection col in collections)
            {
                foreach (IUpdate update in col)
                {
                    resultobject.Updates.Add(Update.GetUpdateObject(update));
                    if (update.IsSuperseded)
                    {
                        List <object> supersededmappings = GetRelatedUpdates(update, UpdateRelationship.UpdatesThatSupersedeThisUpdate);
                        resultobject.SupersededUpdates.AddRange(supersededmappings);
                    }
                }
            }
        }
Beispiel #7
0
        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);
        }
Beispiel #8
0
        /// <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);
                }
            }
        }
Beispiel #9
0
        /// <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.");
            }
        }
Beispiel #10
0
        /// <summary>
        /// Checks a classification for needed updates
        /// </summary>
        /// <param name="server">
        /// Object representing the WSUS server
        /// </param>
        /// <param name="classification">
        /// a single update classification
        /// </param>
        /// <param name="rootGroup">
        /// the "all computers" group
        /// </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 CheckClassification(
            IUpdateServer server,
            IUpdateClassification classification,
            IComputerTargetGroup rootGroup,
            bool isTest,
            List<IComputerTargetGroup> alreadyProcessed)
        {
            Console.Out.WriteLine("Getting list of updates for : " + classification.Title);

            var searchScope = new UpdateScope { IncludedInstallationStates = UpdateInstallationStates.NotInstalled };

            searchScope.Classifications.Add(classification);
            UpdateCollection updates = server.GetUpdates(searchScope);

            if (updates.Count > 0)
            {
                DoClassificationUpdates(updates, rootGroup, isTest, alreadyProcessed);
            }
            else
            {
                Console.Out.WriteLine(" No updates required.");
            }
        }
Beispiel #11
0
        /// <summary>
        /// Entry point for copying approvals from one target group to another
        /// </summary>
        /// <param name="server">
        /// The server.
        /// </param>
        /// <param name="sourceGroup">
        /// The source group.
        /// </param>
        /// <param name="destinationGroup">
        /// The destination group.
        /// </param>
        /// <param name="isTest">
        /// Whether this is a test run.
        /// </param>
        public static void DoCopy(
            IUpdateServer server, IComputerTargetGroup sourceGroup, IComputerTargetGroup destinationGroup, bool isTest)
        {
            // get IComputerTargetGroup references for the source and destination groups
            if (isTest)
            {
                Console.Out.Write("(TEST) ");
            }

            Console.Out.WriteLine(
                "Copying update approvals from group {0} to group {1}.", sourceGroup.Name, destinationGroup.Name);

            // loop over all updates, copying approvals from the source group to the destination
            // group as necessary
            var updates = server.GetUpdates();

            foreach (IUpdate update in updates)
            {
                var sourceApprovals = update.GetUpdateApprovals(sourceGroup);
                var destinationApprovals = update.GetUpdateApprovals(destinationGroup);

                // for simplicity, this program assumes that an update has
                // at most one approval to a given group
                if (sourceApprovals.Count > 1)
                {
                    Console.Out.WriteLine(
                        "{0} had multiple approvals to group {1}; skipping.", update.Title, sourceGroup.Name);
                    continue;
                }

                if (destinationApprovals.Count > 1)
                {
                    Console.Out.WriteLine(
                        "{0} had multiple approvals to group {1}; skipping.", update.Title, destinationGroup.Name);
                    continue;
                }

                IUpdateApproval sourceApproval = null;
                IUpdateApproval destinationApproval = null;

                if (sourceApprovals.Count > 0)
                {
                    sourceApproval = sourceApprovals[0];
                }

                if (destinationApprovals.Count > 0)
                {
                    destinationApproval = destinationApprovals[0];
                }

                if (sourceApproval == null)
                {
                    // the update is not approved to the source group
                    if (destinationApproval != null)
                    {
                        // the update is not approved to the source group, but it is approved
                        // to the destination group
                        // unapprove the update for the destination group to match the source
                        Console.Out.WriteLine("Unapproving {0} to group {1}.", update.Title, destinationGroup.Name);
                        if (isTest)
                        {
                            Console.Out.Write("(TEST) ");
                        }
                        else
                        {
                            destinationApproval.Delete();
                        }
                    }

                    // neither the source group nor the destination group have an approval;
                    // do nothing
                }
                else
                {
                    // the source group has an approval
                    if (destinationApproval != null)
                    {
                        // destination group has an approval; check to see if we need to overwrite it
                        if (destinationApproval.Action != sourceApproval.Action)
                        {
                            if (destinationApproval.Action == UpdateApprovalAction.Uninstall)
                            {
                                // TODO : allow a rule to override this.
                                Console.WriteLine("Skipping copy of approval on {0} as marked for uninstall in {1}.", update.Title, destinationGroup.Name);
                            }
                            else
                            {
                                // the approvals are different; overwrite
                                Console.Out.WriteLine(
                                    "Changing approval for {0} from {1} to {2} for group {3}.",
                                    update.Title,
                                    destinationApproval.Action,
                                    sourceApproval.Action,
                                    destinationGroup.Name);
                                if (isTest)
                                {
                                    Console.Out.Write("(TEST) ");
                                }

                                Job.ApproveUpdateForTargetGroup(update, destinationGroup, isTest, null);
                            }
                        }
                    }
                    else
                    {
                        // destination group does not have an approval; approve
                        Console.Out.WriteLine(
                            "Approving {0} for {1} for group {2}.",
                            update.Title,
                            sourceApproval.Action,
                            destinationGroup.Name);
                        if (isTest)
                        {
                            Console.Out.Write("(TEST) ");
                        }

                        Job.ApproveUpdateForTargetGroup(update, destinationGroup, isTest, null);
                    }
                }
            }
        }
Beispiel #12
0
        /// <summary>
        /// Entry point for copying approvals from one target group to another
        /// </summary>
        /// <param name="server">
        /// The server.
        /// </param>
        /// <param name="sourceGroup">
        /// The source group.
        /// </param>
        /// <param name="destinationGroup">
        /// The destination group.
        /// </param>
        /// <param name="isTest">
        /// Whether this is a test run.
        /// </param>
        public static void DoCopy(
            IUpdateServer server, IComputerTargetGroup sourceGroup, IComputerTargetGroup destinationGroup, bool isTest)
        {
            // get IComputerTargetGroup references for the source and destination groups
            if (isTest)
            {
                Console.Out.Write("(TEST) ");
            }

            Console.Out.WriteLine(
                "Copying update approvals from group {0} to group {1}.", sourceGroup.Name, destinationGroup.Name);

            // loop over all updates, copying approvals from the source group to the destination
            // group as necessary
            var updates = server.GetUpdates();

            foreach (IUpdate update in updates)
            {
                var sourceApprovals      = update.GetUpdateApprovals(sourceGroup);
                var destinationApprovals = update.GetUpdateApprovals(destinationGroup);

                // for simplicity, this program assumes that an update has
                // at most one approval to a given group
                if (sourceApprovals.Count > 1)
                {
                    Console.Out.WriteLine(
                        "{0} had multiple approvals to group {1}; skipping.", update.Title, sourceGroup.Name);
                    continue;
                }

                if (destinationApprovals.Count > 1)
                {
                    Console.Out.WriteLine(
                        "{0} had multiple approvals to group {1}; skipping.", update.Title, destinationGroup.Name);
                    continue;
                }

                IUpdateApproval sourceApproval      = null;
                IUpdateApproval destinationApproval = null;

                if (sourceApprovals.Count > 0)
                {
                    sourceApproval = sourceApprovals[0];
                }

                if (destinationApprovals.Count > 0)
                {
                    destinationApproval = destinationApprovals[0];
                }

                if (sourceApproval == null)
                {
                    // the update is not approved to the source group
                    if (destinationApproval != null)
                    {
                        // the update is not approved to the source group, but it is approved
                        // to the destination group
                        // unapprove the update for the destination group to match the source
                        Console.Out.WriteLine("Unapproving {0} to group {1}.", update.Title, destinationGroup.Name);
                        if (isTest)
                        {
                            Console.Out.Write("(TEST) ");
                        }
                        else
                        {
                            destinationApproval.Delete();
                        }
                    }

                    // neither the source group nor the destination group have an approval;
                    // do nothing
                }
                else
                {
                    // the source group has an approval
                    if (destinationApproval != null)
                    {
                        // destination group has an approval; check to see if we need to overwrite it
                        if (destinationApproval.Action != sourceApproval.Action)
                        {
                            if (destinationApproval.Action == UpdateApprovalAction.Uninstall)
                            {
                                // TODO : allow a rule to override this.
                                Console.WriteLine("Skipping copy of approval on {0} as marked for uninstall in {1}.", update.Title, destinationGroup.Name);
                            }
                            else
                            {
                                // the approvals are different; overwrite
                                Console.Out.WriteLine(
                                    "Changing approval for {0} from {1} to {2} for group {3}.",
                                    update.Title,
                                    destinationApproval.Action,
                                    sourceApproval.Action,
                                    destinationGroup.Name);
                                if (isTest)
                                {
                                    Console.Out.Write("(TEST) ");
                                }

                                Job.ApproveUpdateForTargetGroup(update, destinationGroup, isTest, null);
                            }
                        }
                    }
                    else
                    {
                        // destination group does not have an approval; approve
                        Console.Out.WriteLine(
                            "Approving {0} for {1} for group {2}.",
                            update.Title,
                            sourceApproval.Action,
                            destinationGroup.Name);
                        if (isTest)
                        {
                            Console.Out.Write("(TEST) ");
                        }

                        Job.ApproveUpdateForTargetGroup(update, destinationGroup, isTest, null);
                    }
                }
            }
        }