Ejemplo n.º 1
0
        /// <summary>
        /// The do run set target group.
        /// </summary>
        /// <param name="server">
        /// Object representing the WSUS server
        /// </param>
        /// <param name="targetGroup">
        /// </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 DoRunSetTargetGroup(
            IUpdateServer server,
            TargetGroup targetGroup,
            bool isTest,
            List <IComputerTargetGroup> alreadyProcessed)
        {
            Console.WriteLine("Processing Target Groups: " + targetGroup.Guid);

            // check the target group exists on wsus
            IComputerTargetGroup ctg = server.GetComputerTargetGroup(targetGroup.Guid);

            Console.WriteLine("Matched target group guid to: " + ctg.Name);

            // Get classifications
            if (targetGroup.Classifications.ElementInformation.IsPresent)
            {
                Console.WriteLine("Found a collection of specific classifications to apply.");
                DoRunSetClassifications(server, ctg, targetGroup.Classifications, isTest, alreadyProcessed);
            }
            else if (targetGroup.AllClassifications.ElementInformation.IsPresent)
            {
                Console.WriteLine("Applying all classifications.");
                DoRunSetAllClassifications(server, ctg, isTest, targetGroup.AllClassifications);
            }
            else
            {
                throw new ConfigurationErrorsException(
                          "Unable to detect if we are running a specific set of classifications, or processing all classifications");
            }
        }
        private void AddChildGroupNodes(TreeNode node, IComputerTargetGroup parent, int depth)
        {
            if (depth > 10)
            {
                // We're recursing too much - advise end user no further levels will be added and that loop recursion should be checked for
                MessageBox.Show("Computer group rules may be no more than 15 levels deep.  No further levels will be added.  Please check for recursive loops.", "Update Rule Recursion", MessageBoxButtons.OK);
                return;
            }

            // Loop through all computer groups, checking for groups with the specified parent
            foreach (clsConfig.GroupUpdateRule gu in grouprules)
            {
                if ((gu.parentcomputergroup == null && parent == null) || (gu.parentcomputergroup != null && parent != null && gu.parentcomputergroup.Id == parent.Id))
                {
                    // Describe the group
                    string nodedesc;

                    if (gu.parentcomputergroup == null)
                        // Child of root node
                        nodedesc = gu.computergroup.Name + ": short name \"" + gu.shortname + "\", display order " + gu.displayorder.ToString() + ", sort weight " + gu.sortweight.ToString() + ", updates approvable " + TimeSpanReadable(gu.updateinterval) + " after being received by root WSUS server";
                    else
                        // Child of another group
                        nodedesc = gu.computergroup.Name + ": short name \"" + gu.shortname + "\", display order " + gu.displayorder.ToString() + ", sort weight " + gu.sortweight.ToString() + ", updates approvable " + TimeSpanReadable(gu.updateinterval) +
                            " after being approved for " + gu.parentcomputergroup.Name + ", or " + TimeSpanReadable(gu.childupdateinterval) + " after updates for " +
                            gu.parentcomputergroup.Name + " were approvable if no computers in this group require the update";

                    // Add a tree node and recurse for children
                    TreeNode tn = node.Nodes.Add(nodedesc);
                    tn.Name = gu.computergroup.Name;
                    tn.Tag = gu;

                    AddChildGroupNodes(tn, gu.computergroup, depth + 1);
                }
            }
        }
Ejemplo n.º 3
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.");
            }
        }
Ejemplo n.º 4
0
            public GroupUpdateRule this[IComputerTargetGroup index]
            {
                get
                {
                    // Loop through all groups in the collection, returning the first match
                    foreach (GroupUpdateRule cg in this.List)
                    {
                        if (cg.computergroup == index)
                        {
                            return(cg);
                        }
                    }

                    // None found - return null
                    return(null);
                }

                set
                {
                    // Loop through all groups in the collection, updating the first match
                    for (int i = 0; i < this.List.Count; i++)
                    {
                        GroupUpdateRule cg = (GroupUpdateRule)this.List[i];
                        if (cg.computergroup == index)
                        {
                            // Got one - update it
                            cg = value;
                            return;
                        }
                    }

                    // Couldn't find a group - throw exception
                    throw new KeyNotFoundException();
                }
            }
Ejemplo n.º 5
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);
                }
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Return an object representing the 'All Computer' group.
        /// </summary>
        /// <returns>Return a IComputerTargetGroup which represent the 'All Computer' group, or null if Wsus Server is unreachable.</returns>
        internal IComputerTargetGroup GetAllComputerTargetGroup()
        {
            IComputerTargetGroup computerGroup = null;

            try
            {
                computerGroup = this._updateServer.GetComputerTargetGroup(ComputerTargetGroupId.AllComputers);
            }
            catch (Exception) { }

            return(computerGroup);
        }
Ejemplo n.º 7
0
        /*
         * static void DoRunSetClassifications(
         *  Microsoft.UpdateServices.Administration.IUpdateServer server,
         *  Microsoft.UpdateServices.Administration.IComputerTargetGroup ctg,
         *  bool isTest,
         *  System.Collections.Generic.List<Microsoft.UpdateServices.Administration.IComputerTargetGroup> alreadyProcessed
         *  )
         * {
         *  //Model.ClassificationCollection classifications,
         *  Microsoft.UpdateServices.Administration.UpdateClassificationCollection classifications
         *      =server.GetUpdateClassifications();
         *  foreach (Model.Classification classification in classifications)
         *  {
         *      DoRunSetClassification(server, ctg, classification, isTest, alreadyProcessed);
         *  }
         * }
         * */

        /// <summary>
        /// The do run set classifications.
        /// </summary>
        /// <param name="server">
        /// Object representing the WSUS server
        /// </param>
        /// <param name="computerTargetGroup">
        /// The target group to check
        /// </param>
        /// <param name="classifications">
        /// Collection of Classifications to process
        /// </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 DoRunSetClassifications(
            IUpdateServer server,
            IComputerTargetGroup computerTargetGroup,
            ClassificationCollection classifications,
            bool isTest,
            List <IComputerTargetGroup> alreadyProcessed)
        {
            foreach (Classification classification in classifications)
            {
                DoRunSetClassification(server, computerTargetGroup, classification, isTest, alreadyProcessed);
            }
        }
Ejemplo n.º 8
0
        /// <summary>
        /// The is update marked for uninstall.
        /// </summary>
        /// <param name="update">
        /// The update.
        /// </param>
        /// <param name="targetGroup">
        /// The target group.
        /// </param>
        /// <returns>
        /// The <see cref="bool"/>.
        /// </returns>
        private static bool IsUpdateMarkedForUninstall(IUpdate update, IComputerTargetGroup targetGroup)
        {
            UpdateApprovalCollection approvals = update.GetUpdateApprovals(targetGroup);

            foreach (IUpdateApproval approval in approvals)
            {
                if (approval.Action == UpdateApprovalAction.Uninstall)
                {
                    return(true);
                }
            }

            return(false);
        }
Ejemplo n.º 9
0
        internal FrmApprovalSet(List <MetaGroup> metaGroups, ComputerGroup computersGroup, UpdateCollection updatesToApprove)
        {
            Logger.EnteringMethod("FrmApprovalSet");
            InitializeComponent();

            _wsus             = WsusWrapper.GetInstance();
            _allComputerGroup = _wsus.GetAllComputerTargetGroup();
            _metaGroups       = metaGroups;
            _computersGroup   = computersGroup;
            dtDeadLine.Value  = DateTime.Now.AddDays(_wsus.CurrentServer.DeadLineDaysSpan);
            nupHour.Value     = _wsus.CurrentServer.DeadLineHour;
            nupMinute.Value   = _wsus.CurrentServer.DeadLineMinute;
            FillDataGridView(updatesToApprove, metaGroups, computersGroup);
        }
Ejemplo n.º 10
0
        /// <summary>
        /// The check needed updates.
        /// </summary>
        /// <param name="server">
        /// </param>
        /// <param name="ctg">
        /// </param>
        /// <param name="isTest">
        /// </param>
        private static void CheckNeededUpdates(
            IUpdateServer server,
            IComputerTargetGroup ctg,
            // Microsoft.UpdateServices.Administration.UpdateCategoryCollection products,
            bool isTest)
        {
            // check classifications
            Console.Out.WriteLine("Getting classifications");
            UpdateClassificationCollection classifications = Server.GetUpdateClassifications(server);

            // check for updates
            foreach (IUpdateClassification classification in classifications)
            {
                CheckClassification(server, classification, ctg, isTest, null);
            }
        }
Ejemplo n.º 11
0
        /// <summary>
        /// The do run set all classifications.
        /// </summary>
        /// <param name="server">
        /// The server.
        /// </param>
        /// <param name="ctg">
        /// The ctg.
        /// </param>
        /// <param name="isTest">
        /// The is Test.
        /// </param>
        /// <param name="allClassifications">
        /// The all Classifications.
        /// </param>
        private static void DoRunSetAllClassifications(
            IUpdateServer server,
            IComputerTargetGroup ctg,
            bool isTest,
            Rule allClassifications)
        {
            // TODO: support products in all classifications element
            // Microsoft.UpdateServices.Administration.UpdateCategoryCollection products = null;
            if (allClassifications.Copy)
            {
                // Check for copy
                Console.Out.WriteLine("Checking TargetGroup to Copy From: ");
                IComputerTargetGroup cftg = server.GetComputerTargetGroup(allClassifications.CopyFrom);
                Console.WriteLine("Matched copy from guid to: " + cftg.Name);
                CopyFrom.DoCopy(server, cftg, ctg, isTest);
            }

            if (allClassifications.ApproveSupersededUpdates)
            {
                // Check for superseded updates
                Console.Out.WriteLine("Checking Superseded Updates: ");
                CheckSupersededUpdates(
                    server,
                    allClassifications.AcceptLicenseAgreement,
                    ctg,
                    null,
                    null,
                    isTest,
                    allClassifications.ShouldApproveUninstalledSupersededUpdate);
            }

            if (allClassifications.ApproveStaleUpdates)
            {
                // Check for stale updates
                Console.Out.WriteLine("Checking Stale Updates: ");
                CheckStaleUpdates(server, allClassifications.AcceptLicenseAgreement, ctg, null, null, isTest);
            }

            if (allClassifications.ApproveNeededUpdates)
            {
                CheckNeededUpdates(server, ctg, isTest);
            }
        }
Ejemplo n.º 12
0
        /// <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);
            }
        }
Ejemplo n.º 13
0
        private List <Dictionary <string, string> > GetComputerTargetGroup(IUpdateServer wsus, string id)
        {
            List <Dictionary <string, string> > retList = new List <Dictionary <string, string> >();
            IComputerTargetGroup     group   = wsus.GetComputerTargetGroup(new Guid(id));
            ComputerTargetCollection members = group.GetComputerTargets();

            foreach (IComputerTarget member in members)
            {
                Dictionary <string, string> details = new Dictionary <string, string>();
                details.Add("Id", member.Id);
                details.Add("Name", member.FullDomainName);
                details.Add("Ip", member.IPAddress.ToString());
                details.Add("LastReport", member.LastReportedInventoryTime.ToString());
                details.Add("ComputerRole", member.ComputerRole.ToString());

                retList.Add(details);
            }
            return(retList);
        }
Ejemplo n.º 14
0
        /// <summary>
        /// The do run set all target groups.
        /// </summary>
        /// <param name="server">
        /// </param>
        /// <param name="allTargetGroups">
        /// </param>
        /// <param name="isTest">
        /// </param>
        /// <param name="alreadyProcessed">
        /// List of target groups that have already been processed
        /// </param>
        private static void DoRunSetAllTargetGroups(
            IUpdateServer server,
            AllTargetGroups allTargetGroups,
            bool isTest,
            List <IComputerTargetGroup> alreadyProcessed)
        {
            Console.Out.WriteLine("Getting root target group: ");
            IComputerTargetGroup rootGroup = Server.GetRootTargetGroup(server);

            ClassificationCollection classifications = allTargetGroups.Classifications;

            if (classifications.Count > 0)
            {
                DoRunSetClassifications(server, rootGroup, classifications, isTest, alreadyProcessed);
            }
            else
            {
                DoRunSetAllClassifications(server, rootGroup, isTest, allTargetGroups.AllClassifications);
            }
        }
Ejemplo n.º 15
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.");
            }
        }
Ejemplo n.º 16
0
        /// <summary>
        /// Approves a superseded update
        ///     Using previous approval settings
        /// </summary>
        /// <param name="newUpdate">
        /// The new update
        /// </param>
        /// <param name="previousUpdate">
        /// The previously approved update
        /// </param>
        /// <param name="isTest">
        /// Whether we are in test mode
        /// </param>
        /// <param name="recentlyApproved">
        /// List of recently approved updates
        /// </param>
        /// <param name="shouldApproveUninstalledSupersededUpdate">
        /// Whether an update that has been uninstalled should be approved if superseded.
        /// </param>
        private static void ApproveSupersededUpdate(
            IUpdate newUpdate,
            IUpdate previousUpdate,
            bool isTest,
            List <Guid> recentlyApproved,
            bool shouldApproveUninstalledSupersededUpdate)
        {
            if (isTest)
            {
                Console.Out.Write("(TEST) ");
            }

            Console.Out.WriteLine(newUpdate.Title);
            recentlyApproved.Add(newUpdate.Id.UpdateId);

            if (isTest)
            {
                return;
            }

            UpdateApprovalCollection approvals = previousUpdate.GetUpdateApprovals();

            foreach (IUpdateApproval approval in approvals)
            {
                IComputerTargetGroup ctg = approval.GetComputerTargetGroup();

                if (approval.Action == UpdateApprovalAction.Uninstall && !shouldApproveUninstalledSupersededUpdate)
                {
                    // this update has been marked for removal
                    Console.WriteLine(
                        " WARNING: Superseded update is marked for uninstall and settings don't allow it to be automatically approved. Target group: "
                        + ctg.Name);

                    continue;
                }

                newUpdate.Approve(UpdateApprovalAction.Install, ctg);

                Console.Out.WriteLine(" " + ctg.Name);
            }
        }
Ejemplo n.º 17
0
        /// <summary>
        /// Run through collection of updates for a classification
        /// </summary>
        /// <param name="updates">
        /// Collection of updates
        /// </param>
        /// <param name="targetGroup">
        /// A single target 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 DoClassificationUpdates(
            UpdateCollection updates,
            IComputerTargetGroup targetGroup,
            bool isTest,
            List <IComputerTargetGroup> alreadyProcessed)
        {
            Console.Out.WriteLine(" " + updates.Count + " updates required.");

            foreach (IUpdate update in updates)
            {
                if (update.IsDeclined)
                {
                    Console.WriteLine("Update has been declined: " + update.Title);
                    continue;
                }

                if (IsUpdateMarkedForUninstall(update, targetGroup))
                {
                    Console.WriteLine("Update has been marked for uninstall: " + update.Title);
                    continue;
                }

                if (update.RequiresLicenseAgreementAcceptance)
                {
                    if (isTest)
                    {
                        Console.Out.Write("(TEST)");
                    }
                    else
                    {
                        update.AcceptLicenseAgreement();
                    }

                    Console.Out.WriteLine("  License Accepted: " + update.Title);
                }

                Console.Out.WriteLine("   " + update.Title + ".");
                ApproveUpdateForTargetGroup(update, targetGroup, isTest, alreadyProcessed);
            }
        }
        private void AddChildGroupNodes(TreeNode node, IComputerTargetGroup parent, int depth)
        {
            if (depth > 10)
            {
                // We're recursing too much - advise end user no further levels will be added and that loop recursion should be checked for
                MessageBox.Show("Computer group rules may be no more than 15 levels deep.  No further levels will be added.  Please check for recursive loops.", "Update Rule Recursion", MessageBoxButtons.OK);
                return;
            }

            // Loop through all computer groups, checking for groups with the specified parent
            foreach (clsConfig.GroupUpdateRule gu in grouprules)
            {
                if ((gu.parentcomputergroup == null && parent == null) || (gu.parentcomputergroup != null && parent != null && gu.parentcomputergroup.Id == parent.Id))
                {
                    // Describe the group
                    string nodedesc;

                    if (gu.parentcomputergroup == null)
                    {
                        // Child of root node
                        nodedesc = gu.computergroup.Name + ": short name \"" + gu.shortname + "\", display order " + gu.displayorder.ToString() + ", sort weight " + gu.sortweight.ToString() + ", updates approvable " + TimeSpanReadable(gu.updateinterval) + " after being received by root WSUS server";
                    }
                    else
                    {
                        // Child of another group
                        nodedesc = gu.computergroup.Name + ": short name \"" + gu.shortname + "\", display order " + gu.displayorder.ToString() + ", sort weight " + gu.sortweight.ToString() + ", updates approvable " + TimeSpanReadable(gu.updateinterval) +
                                   " after being approved for " + gu.parentcomputergroup.Name + ", or " + TimeSpanReadable(gu.childupdateinterval) + " after updates for " +
                                   gu.parentcomputergroup.Name + " were approvable if no computers in this group require the update";
                    }

                    // Add a tree node and recurse for children
                    TreeNode tn = node.Nodes.Add(nodedesc);
                    tn.Name = gu.computergroup.Name;
                    tn.Tag  = gu;

                    AddChildGroupNodes(tn, gu.computergroup, depth + 1);
                }
            }
        }
Ejemplo n.º 19
0
        /// <summary>
        /// Approves and update and checks for child target groups
        /// </summary>
        /// <param name="update">
        /// An update
        /// </param>
        /// <param name="targetGroup">
        /// A target 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>
        public static void ApproveUpdateForTargetGroup(
            IUpdate update,
            IComputerTargetGroup targetGroup,
            bool isTest,
            List <IComputerTargetGroup> alreadyProcessed)
        {
            if (isTest)
            {
                Console.Out.WriteLine("   (TEST)" + targetGroup.Name + ".");
            }
            else
            {
                // 1.0.0.2 issue with Adobe Flash not having an installable update
                if (update.InstallationBehavior.IsSupported)
                {
                    if (update.IsDeclined)
                    {
                        Console.Out.WriteLine("   " + update.Title + ": is declined.");
                    }
                    else
                    {
                        update.Approve(UpdateApprovalAction.Install, targetGroup);
                        Console.Out.WriteLine("   " + update.Title + ": approved for install.");
                    }
                }
                else
                {
                    Console.Out.WriteLine("   " + update.Title + ": doesn't support install approval.");
                }
            }

            ComputerTargetGroupCollection children = targetGroup.GetChildTargetGroups();

            if (children != null && children.Count > 0)
            {
                ApproveUpdate(update, children, isTest, alreadyProcessed);
            }
        }
Ejemplo n.º 20
0
        /// <summary>
        /// This is the path run if there are no run sets in the app.config
        ///     This is the classic apply to all groups approach from 1.0.0.0
        /// </summary>
        /// <param name="settings">
        /// application settings
        /// </param>
        /// <param name="server">
        /// The WSUS Server
        /// </param>
        /// <param name="isTest">
        /// Whether we are in test mode
        /// </param>
        private static void DoNoRunSets(ApplicationSettings settings, IUpdateServer server, bool isTest)
        {
            Console.WriteLine("Performing \"No Run Set\" rule: ");

            // Get target groups
            Console.Out.WriteLine("Getting root target group: ");
            IComputerTargetGroup rootGroup = Server.GetRootTargetGroup(server);

            Console.Out.WriteLine("succeeded.");

            Rule defaultRule = settings.NoRunSet;

            if (defaultRule.ApproveSupersededUpdates)
            {
                // Check for superseded updates
                Console.Out.WriteLine("Checking Superseded Updates: ");
                CheckSupersededUpdates(
                    server,
                    defaultRule.AcceptLicenseAgreement,
                    null,
                    null,
                    null,
                    isTest,
                    defaultRule.ShouldApproveUninstalledSupersededUpdate);
            }

            if (defaultRule.ApproveStaleUpdates)
            {
                // Check for stale updates
                Console.Out.WriteLine("Checking Stale Updates: ");
                CheckStaleUpdates(server, defaultRule.AcceptLicenseAgreement, null, null, null, isTest);
            }

            if (defaultRule.ApproveNeededUpdates)
            {
                CheckNeededUpdates(server, rootGroup, isTest);
            }
        }
Ejemplo n.º 21
0
        /// <summary>
        /// Approves and update and checks for child target groups
        /// </summary>
        /// <param name="update">
        /// An update
        /// </param>
        /// <param name="targetGroup">
        /// A target 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>
        public static void ApproveUpdateForTargetGroup(
            IUpdate update,
            IComputerTargetGroup targetGroup,
            bool isTest,
            List<IComputerTargetGroup> alreadyProcessed)
        {
            if (isTest)
            {
                Console.Out.WriteLine("   (TEST)" + targetGroup.Name + ".");
            }
            else
            {
                // 1.0.0.2 issue with Adobe Flash not having an installable update
                if (update.InstallationBehavior.IsSupported)
                {
                    if (update.IsDeclined)
                    {
                        Console.Out.WriteLine("   " + update.Title + ": is declined.");
                    }
                    else
                    {
                        update.Approve(UpdateApprovalAction.Install, targetGroup);
                        Console.Out.WriteLine("   " + update.Title + ": approved for install.");
                    }
                }
                else
                {
                    Console.Out.WriteLine("   " + update.Title + ": doesn't support install approval.");
                }
            }

            ComputerTargetGroupCollection children = targetGroup.GetChildTargetGroups();
            if (children != null && children.Count > 0)
            {
                ApproveUpdate(update, children, isTest, alreadyProcessed);
            }
        }
Ejemplo n.º 22
0
        /// <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);
            }
        }
Ejemplo n.º 23
0
        /// <summary>
        /// The do run set all classifications.
        /// </summary>
        /// <param name="server">
        /// The server.
        /// </param>
        /// <param name="ctg">
        /// The ctg.
        /// </param>
        /// <param name="isTest">
        /// The is Test.
        /// </param>
        /// <param name="allClassifications">
        /// The all Classifications.
        /// </param>
        private static void DoRunSetAllClassifications(
            IUpdateServer server,
            IComputerTargetGroup ctg,
            bool isTest,
            Rule allClassifications)
        {
            // TODO: support products in all classifications element
            // Microsoft.UpdateServices.Administration.UpdateCategoryCollection products = null;
            if (allClassifications.Copy)
            {
                // Check for copy
                Console.Out.WriteLine("Checking TargetGroup to Copy From: ");
                IComputerTargetGroup cftg = server.GetComputerTargetGroup(allClassifications.CopyFrom);
                Console.WriteLine("Matched copy from guid to: " + cftg.Name);
                CopyFrom.DoCopy(server, cftg, ctg, isTest);
            }

            if (allClassifications.ApproveSupersededUpdates)
            {
                // Check for superseded updates
                Console.Out.WriteLine("Checking Superseded Updates: ");
                CheckSupersededUpdates(
                    server,
                    allClassifications.AcceptLicenseAgreement,
                    ctg,
                    null,
                    null,
                    isTest,
                    allClassifications.ShouldApproveUninstalledSupersededUpdate);
            }

            if (allClassifications.ApproveStaleUpdates)
            {
                // Check for stale updates
                Console.Out.WriteLine("Checking Stale Updates: ");
                CheckStaleUpdates(server, allClassifications.AcceptLicenseAgreement, ctg, null, null, isTest);
            }

            if (allClassifications.ApproveNeededUpdates)
            {
                CheckNeededUpdates(server, ctg, isTest);
            }
        }
Ejemplo n.º 24
0
        /// <summary>
        /// Run through collection of updates for a classification
        /// </summary>
        /// <param name="updates">
        /// Collection of updates
        /// </param>
        /// <param name="targetGroup">
        /// A single target 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 DoClassificationUpdates(
            UpdateCollection updates,
            IComputerTargetGroup targetGroup,
            bool isTest,
            List<IComputerTargetGroup> alreadyProcessed)
        {
            Console.Out.WriteLine(" " + updates.Count + " updates required.");

            foreach (IUpdate update in updates)
            {
                if (update.IsDeclined)
                {
                    Console.WriteLine("Update has been declined: " + update.Title);
                    continue;
                }

                if (IsUpdateMarkedForUninstall(update, targetGroup))
                {
                    Console.WriteLine("Update has been marked for uninstall: " + update.Title);
                    continue;
                }

                if (update.RequiresLicenseAgreementAcceptance)
                {
                    if (isTest)
                    {
                        Console.Out.Write("(TEST)");
                    }
                    else
                    {
                        update.AcceptLicenseAgreement();
                    }

                    Console.Out.WriteLine("  License Accepted: " + update.Title);
                }

                Console.Out.WriteLine("   " + update.Title + ".");
                ApproveUpdateForTargetGroup(update, targetGroup, isTest, alreadyProcessed);
            }
        }
Ejemplo n.º 25
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);
                }
            }
        }
Ejemplo n.º 26
0
            public GroupUpdateRule this[IComputerTargetGroup index]
            {
                get
                {
                    // Loop through all groups in the collection, returning the first match
                    foreach (GroupUpdateRule cg in this.List)
                        if (cg.computergroup == index)
                            return cg;

                    // None found - return null
                    return null;
                }

                set
                {
                    // Loop through all groups in the collection, updating the first match
                    for (int i = 0; i < this.List.Count; i++)
                    {
                        GroupUpdateRule cg = (GroupUpdateRule)this.List[i];
                        if (cg.computergroup == index)
                        {
                            // Got one - update it
                            cg = value;
                            return;
                        }
                    }

                    // Couldn't find a group - throw exception
                    throw new KeyNotFoundException();
                }
            }
Ejemplo n.º 27
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.");
            }
        }
Ejemplo n.º 28
0
        /// <summary>
        /// The is update marked for uninstall.
        /// </summary>
        /// <param name="update">
        /// The update.
        /// </param>
        /// <param name="targetGroup">
        /// The target group.
        /// </param>
        /// <returns>
        /// The <see cref="bool"/>.
        /// </returns>
        private static bool IsUpdateMarkedForUninstall(IUpdate update, IComputerTargetGroup targetGroup)
        {
            UpdateApprovalCollection approvals = update.GetUpdateApprovals(targetGroup);
            foreach (IUpdateApproval approval in approvals)
            {
                if (approval.Action == UpdateApprovalAction.Uninstall)
                {
                    return true;
                }
            }

            return false;
        }
Ejemplo n.º 29
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.");
            }
        }
Ejemplo n.º 30
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);
                    }
                }
            }
        }
Ejemplo n.º 31
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);
                    }
                }
            }
        }
Ejemplo n.º 32
0
 public WSUSGroup(IComputerTargetGroup group)
 {
     grp = group;
 }
Ejemplo n.º 33
0
 public void setWSUSGroup(IComputerTargetGroup group)
 {
     grp = group;
 }
Ejemplo n.º 34
0
        static void Main(string[] args)
        {
            var           hook       = new RemoteHook();
            IUpdateServer wsusServer = ConnectLocal();
            var           dict       = new Dictionary <string, string>();

            if (args.Length > 0)
            {
                X509Certificate2 cert = DownloadSslCertificate(Environment.MachineName);
                switch (args[0])
                {
                case "Set-DNSName":
                    try
                    {
                        var name = args[1];
                        hook.SetDnsName(name);
                        dict["value"] = "true";
                        hook.ReturnPayload(dict);
                    }
                    catch (IndexOutOfRangeException)
                    {
                        Console.Error.WriteLine("ERROR: missing argument: <Name>");
                    }
                    break;

                case "Get-DNSName":
                    dict.Add("value", hook.GetDnsName());
                    hook.ReturnPayload(dict);
                    break;

                case "Test-SSL":
                    dict.Add("value", wsusServer.IsConnectionSecureForApiRemoting.ToString());
                    hook.ReturnPayload(dict);
                    break;

                case "Set-Cert":
                    try
                    {
                        var path = args[1];
                        var pass = args[2];
                        SetWsusCertificate(path, pass, wsusServer);
                        dict["value"] = "true";
                        hook.ReturnPayload(dict);
                    }
                    catch (IndexOutOfRangeException)
                    {
                        Console.Error.WriteLine("No Arguments found, generating new Certificate");

                        SetWsusCertificate(wsusServer);
                    }
                    break;

                case "Test-Conf":
                    //Importing into other stores
                    try
                    {
                        dict["value"] = "false";
                        X509Store store = new X509Store("WSUS", StoreLocation.LocalMachine);
                        store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
                        X509Certificate2Collection collection  = (X509Certificate2Collection)store.Certificates;
                        X509Certificate2Collection fcollection = (X509Certificate2Collection)collection.Find(X509FindType.FindByTimeValid, DateTime.Now, false);
                        Console.WriteLine("Number of certificates: {0}{1}", fcollection.Count, Environment.NewLine);
                        foreach (X509Certificate2 x509 in fcollection)
                        {
                            Console.WriteLine(x509);
                            if (x509.GetNameInfo(X509NameType.DnsName, true) == "Carteiro")
                            {
                                dict["value"] = "true";
                            }
                        }
                    }
                    catch (Exception)
                    {
                        dict["value"] = "false";
                    }
                    hook.ReturnPayload(dict);
                    break;

                case "Get-Cache":
                    var cachePath = hook.GetCachePath();
                    List <Dictionary <string, string> > dataList = new List <Dictionary <string, string> >();
                    if (System.IO.Directory.Exists(cachePath))
                    {
                        foreach (var file in System.IO.Directory.GetFiles(cachePath))
                        {
                            Dictionary <string, string> entry = new Dictionary <string, string>();
                            entry.Add("MsiFileName", hook.GetMsiProperty(file, "ProductName"));
                            entry.Add("PackageVersion", hook.GetMsiProperty(file, "ProductVersion"));
                            dataList.Add(entry);
                        }
                        hook.ReturnPayload(dataList);
                    }
                    break;

                case "Test-Cert":
                    if (cert != null)
                    {
                        if (cert.IssuerName.Name == hook.GetDnsName())
                        {
                            dict["value"] = "true";
                        }
                        else
                        {
                            dict["value"] = "false";
                        }
                    }
                    else
                    {
                        Console.WriteLine("ERROR: No Certificate found");
                    }
                    break;

                case "Import-Package":
                    try
                    {
                        var path = hook.GetCachePath() + args[1];
                        try
                        {
                            string desc = "Carteiro Update Package";
                            Console.WriteLine(path);
                            string  name         = hook.GetMsiProperty(path, "ProductName");
                            string  manufacturer = hook.GetMsiProperty(path, "Manufacturer");
                            string  version      = hook.GetMsiProperty(path, "ProductVersion");
                            IUpdate update       = hook.ImportPackage(wsusServer, path, name, desc, manufacturer);
                            dict.Add("Title", update.Title);
                            dict.Add("Id", update.Id.UpdateId.ToString());
                            dict.Add("Manufacturer", manufacturer);
                            dict.Add("Version", version);
                            dict.Add("Status", "Imported");
                            dict.Add("CreationDate", update.CreationDate.ToString());
                            Console.WriteLine(dict.ToString());
                        }
                        catch (Exception e)
                        {
                            dict.Add("Status", "Not Imported");
                            Console.Error.WriteLine(e);
                        }
                        hook.ReturnPayload(dict);
                    }
                    catch (IndexOutOfRangeException)
                    {
                        Console.Error.WriteLine("ERROR: missing argument(s)");
                    }
                    break;

                case "Get-Package":
                    try
                    {
                        var path = args[1];
                        var name = args[2];
                        dict = hook.DownloadPackage(path, name);
                        Console.WriteLine(dict["MsiFileName"]);
                        dict.Add("Status", "Downloaded");
                        hook.ReturnPayload(dict);
                    }
                    catch (Exception e)
                    {
                        dict.Add("Value", "FileNotFound");
                        Console.Error.WriteLine(e);
                        hook.ReturnPayload(dict);
                    }
                    break;

                case "Get-Updates":
                    List <Dictionary <string, string> > resUpdates = new List <Dictionary <string, string> >();
                    resUpdates = args.Length > 1 ? hook.GetUpdates(wsusServer, args[1]) : hook.GetUpdates(wsusServer);
                    hook.ReturnPayload(resUpdates);
                    break;

                case "Delete-Update":
                    try
                    {
                        var id = args[1];
                        dict = hook.DeleteUpdate(wsusServer, id);
                        hook.ReturnPayload(dict);
                    }
                    catch (IndexOutOfRangeException)
                    {
                        Console.Error.WriteLine("ERROR: missing argument(s)");
                    }
                    catch (Exception e)
                    {
                        Console.Error.WriteLine(e);
                    }
                    break;

                //TODO Not Implemented Yet
                case "Approve-Update":
                    try
                    {
                        var update_id = args[1];
                        var option    = args[2];
                        var group_id  = args[3];
                        Dictionary <string, string> resVal = new Dictionary <string, string>();
                        IUpdate update             = wsusServer.GetUpdate(new UpdateRevisionId(new Guid(update_id)));
                        IComputerTargetGroup group = wsusServer.GetComputerTargetGroup(new Guid(group_id));
                        switch (option)
                        {
                        case "Install":
                            update.Approve(UpdateApprovalAction.Install, group);
                            resVal.Add("Value", "installed");
                            break;

                        case "Uninstall":
                            if (update.UninstallationBehavior.IsSupported)
                            {
                                update.Approve(UpdateApprovalAction.Uninstall, group);
                                resVal.Add("Value", "uninstalled");
                            }
                            else
                            {
                                resVal.Add("Value", "not supported");
                            }
                            break;

                        case "NotApproved":
                            try
                            {
                                update.Approve(UpdateApprovalAction.NotApproved, group);
                            }
                            catch (InvalidOperationException)
                            {
                                Console.Error.WriteLine("Cannot deapprove for all computers, instead declining it");
                                update.Decline();
                                resVal.Add("Value", "declined");
                            }
                            resVal.Add("Value", "notApproved");
                            break;
                        }
                        update.RefreshUpdateApprovals();
                        hook.ReturnPayload(resVal);
                    }
                    catch (IndexOutOfRangeException)
                    {
                        Console.Error.WriteLine("ERROR: missing argument(s)");
                    }
                    break;

                case "Get-Groups":
                    List <Dictionary <string, string> > resGroups = new List <Dictionary <string, string> >();
                    resGroups = hook.GetComputerTargetGroups(wsusServer);
                    hook.ReturnPayload(resGroups);
                    break;

                case "Get-Group":
                    try
                    {
                        var id = args[1];
                        List <Dictionary <string, string> > resList = new List <Dictionary <string, string> >();
                        resList = hook.GetComputerTargetGroup(wsusServer, id);
                        hook.ReturnPayload(resList);
                    }
                    catch (IndexOutOfRangeException)
                    {
                        Console.Error.WriteLine("ERROR: missing argument(s)");
                    }
                    break;

                case "Get-Client-Status":
                    try
                    {
                        var id = args[1];
                        List <Dictionary <string, string> > resList = new List <Dictionary <string, string> >();
                        Dictionary <string, string>         retDict = new Dictionary <string, string>();
                        retDict = hook.GetComputerTargetStatus(wsusServer, id);
                        resList.Add(retDict);
                        hook.ReturnPayload(resList);
                    }
                    catch (Exception)
                    {
                        Console.Error.WriteLine("ERROR: missing argument(s)");
                    }
                    break;

                default:
                    Console.Error.WriteLine("ERROR: invalid operation");
                    Console.WriteLine("\nCarteiroWin Commands:\n");
                    Console.WriteLine("CarteiroWin Set-DNSName <Name>           : Sets a new Registry Entry for the connection to local WSUS\n");
                    Console.WriteLine("CarteiroWin Get-DNSName                  : Get connectionstring for local WSUS\n");
                    Console.WriteLine("CarteiroWin Test-SSL                     : Test if ssl connection is possible\n");
                    Console.WriteLine("CarteiroWin Set-Cert [Path] [Password]   : Import or generate certificate into WSUS Settings\n");
                    Console.WriteLine("CarteiroWin Test-Conf                    : Test Certificate Configuration\n");
                    Console.WriteLine("CarteiroWin Get-Cache                    : Returns the current Download Cache\n");
                    Console.WriteLine("CarteiroWin Get-Package <Path> <Name>    : Imports local Path or URL to the CarteiroWin Cache\n");
                    Console.WriteLine("CarteiroWin Import-Package <Filename>    : Imports File from CarteiroWin Cache into WSUS\n");
                    Console.WriteLine("CarteiroWin Get-Updates                  : Returns Local Published Updates from Carteiro Win\n");
                    Console.WriteLine("CarteiroWin Delete-Update <update_id>    : Deletes the given Update\n");
                    Console.WriteLine("CarteiroWin Approve-Update <update_id> <'Install'|'Uninstall'|'NotApproved'> <targetgroup_id \n: Approves the Update with the given Rule for the given WSUS group\n");
                    Console.WriteLine("CarteiroWin Get-Groups                   : Returns all WSUS TargetGroups\n");
                    Console.WriteLine("CarteiroWin Get-Group <id>               : Returns the members of the given Group Id\n");
                    Console.WriteLine("CarteiroWin Get-Client-Status <id>       : Returns WSUS Report for a Client\n");
                    break;
                }
                //hook.ReturnPayload(dict);
            }
            else
            {
                Console.WriteLine("ERROR: no Operation given");
            }
        }
Ejemplo n.º 35
0
 /*
 static void DoRunSetClassifications(
     Microsoft.UpdateServices.Administration.IUpdateServer server,
     Microsoft.UpdateServices.Administration.IComputerTargetGroup ctg,
     bool isTest,
     System.Collections.Generic.List<Microsoft.UpdateServices.Administration.IComputerTargetGroup> alreadyProcessed
     )
 {
     //Model.ClassificationCollection classifications,
     Microsoft.UpdateServices.Administration.UpdateClassificationCollection classifications
         =server.GetUpdateClassifications();
     foreach (Model.Classification classification in classifications)
     {
         DoRunSetClassification(server, ctg, classification, isTest, alreadyProcessed);
     }
 }
  * */
 /// <summary>
 /// The do run set classifications.
 /// </summary>
 /// <param name="server">
 /// Object representing the WSUS server
 /// </param>
 /// <param name="computerTargetGroup">
 /// The target group to check
 /// </param>
 /// <param name="classifications">
 /// Collection of Classifications to process
 /// </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 DoRunSetClassifications(
     IUpdateServer server,
     IComputerTargetGroup computerTargetGroup,
     ClassificationCollection classifications,
     bool isTest,
     List<IComputerTargetGroup> alreadyProcessed)
 {
     foreach (Classification classification in classifications)
     {
         DoRunSetClassification(server, computerTargetGroup, classification, isTest, alreadyProcessed);
     }
 }
Ejemplo n.º 36
0
        /// <summary>
        /// The check needed updates.
        /// </summary>
        /// <param name="server">
        /// </param>
        /// <param name="ctg">
        /// </param>
        /// <param name="isTest">
        /// </param>
        private static void CheckNeededUpdates(
            IUpdateServer server,
            IComputerTargetGroup ctg,
            // Microsoft.UpdateServices.Administration.UpdateCategoryCollection products,
            bool isTest)
        {
            // check classifications
            Console.Out.WriteLine("Getting classifications");
            UpdateClassificationCollection classifications = Server.GetUpdateClassifications(server);

            // check for updates
            foreach (IUpdateClassification classification in classifications)
            {
                CheckClassification(server, classification, ctg, isTest, null);
            }
        }
        private void DisplayReportASynch()
        {
            Dictionary <Guid, Company>  companies = ComputerCtrl.Companies;
            UpdateApprovalCollection    approvalsForThisGroup;
            UpdateApprovalCollection    approvalsForAllComputersGroup;
            IComputerTargetGroup        allComputerTargetGroup = _wsus.GetAllComputerTargetGroup();
            Dictionary <string, string> computers   = new Dictionary <string, string>();
            ComputerTargetScope         targetScope = new ComputerTargetScope();
            ReportResult resultToDisplay            = new ReportResult();

            if (!cancelDisplayReport && SelectedRows.Count != 0)
            {
                targetScope.ComputerTargetGroups.Add(_wsus.GetComputerGroup(ComputerCtrl.ComputerGroupID));
                targetScope.IncludeDownstreamComputerTargets = true;

                foreach (DataGridViewRow row in SelectedRows)
                {
                    if (row.Index != -1 && row.Visible)
                    {
                        string tempComputer = _wsus.GetComputerTargetByName((row.Cells["ComputerName"].Value.ToString())).Id;
                        if (tempComputer != null)
                        {
                            computers.Add(tempComputer, row.Cells["ComputerName"].Value.ToString());
                        }
                    }
                }

                foreach (Company company in companies.Values)
                {
                    if (cancelDisplayReport)
                    {
                        break;
                    }
                    foreach (Product product in company.Products.Values)
                    {
                        if (cancelDisplayReport)
                        {
                            break;
                        }
                        foreach (IUpdate update in product.Updates)
                        {
                            if (cancelDisplayReport)
                            {
                                break;
                            }
                            resultToDisplay.ResetCounters();

                            approvalsForThisGroup         = _wsus.GetUpdateApprovalStatus(ComputerCtrl.ComputerGroupID, update);
                            approvalsForAllComputersGroup = _wsus.GetUpdateApprovalStatus(allComputerTargetGroup.Id, update);
                            if ((approvalsForThisGroup.Count != 0 || approvalsForAllComputersGroup.Count != 0) && (chkBxShowSupersededUpdates.Checked || !update.IsSuperseded))
                            {
                                UpdateInstallationInfoCollection installationInfo = update.GetUpdateInstallationInfoPerComputerTarget(targetScope);
                                foreach (IUpdateInstallationInfo info in installationInfo)
                                {
                                    if (computers.ContainsKey(info.ComputerTargetId))
                                    {
                                        switch (info.UpdateInstallationState)
                                        {
                                        case UpdateInstallationState.Downloaded:
                                            resultToDisplay.DownloadedCount++;
                                            break;

                                        case UpdateInstallationState.Failed:
                                            resultToDisplay.FailedCount++;
                                            break;

                                        case UpdateInstallationState.Installed:
                                            resultToDisplay.InstalledCount++;
                                            break;

                                        case UpdateInstallationState.InstalledPendingReboot:
                                            resultToDisplay.InstalledPendingRebootCount++;
                                            break;

                                        case UpdateInstallationState.NotApplicable:
                                            resultToDisplay.NotApplicableCount++;
                                            break;

                                        case UpdateInstallationState.NotInstalled:
                                            resultToDisplay.NotInstalledCount++;
                                            break;

                                        case UpdateInstallationState.Unknown:
                                            resultToDisplay.UnknownCount++;
                                            break;

                                        default:
                                            break;
                                        }
                                    }
                                }

                                object[] args = new object[4];

                                args[0] = update.CompanyTitles[0];
                                args[1] = update.Title;
                                args[2] = ((approvalsForThisGroup.Count != 0) ? resMan.GetString(approvalsForThisGroup[0].Action.ToString()) : resMan.GetString(approvalsForAllComputersGroup[0].Action.ToString())) + ((update.IsSuperseded) ? "(" + resMan.GetString("Superseded") + ")" : string.Empty);
                                args[3] = resultToDisplay;

                                FillRow(args);
                            }
                        }
                    }
                }
                Action action = () =>
                {
                    if (dtGvReport.SortedColumn != null)
                    {
                        dtGvReport.Sort(dtGvReport.SortedColumn, (dtGvReport.SortOrder == SortOrder.Ascending) ? ListSortDirection.Ascending : ListSortDirection.Descending);
                    }
                };
                this.Invoke(action);
            }
        }
Ejemplo n.º 38
0
 protected override void BeginProcessing()
 {
     base.BeginProcessing();
     group = TargetGroupParameter.GroupFromTheName(_lib["ComputerGroup"].Value as string);
 }