Ejemplo n.º 1
0
        public Message GetAll(int current_page = 0, int per_page = 0, string ou = "")
        {
            try
            {
                //current_page = current_page < 1 ? 1 : current_page;
                //per_page = per_page < 1 ? 10 : per_page;
                DistributionGroupsShorter shorty = new DistributionGroupsShorter();

                string result = DistributionRepo.GetDistributionGroup("", current_page, per_page, ou);

                shorty = XmlSerializationHelper.Deserialize<DistributionGroupsShorter>(result);

                return MessageBuilder.CreateResponseMessage(shorty);
            }
            catch (Exception e)
            {
                string message = "";
                while (e != null)
                {
                    message += e.Message;
                    e = e.InnerException;
                }
                XmlDocument doc = new XmlDocument();
                XmlElement elem = doc.CreateElement("error");
                elem.InnerText = message;
                return MessageBuilder.CreateResponseMessage(doc);
            }
        }
        // GetDistributionGroup()
        // desc: Method returns a list of Distribution Groups
        // params: sring identity - Name of Distribution group to return
        // method: public
        // return: string
        public string GetDistributionGroup(string identity, int current_page, int per_page, string ou)
        {
            String ErrorText = "";
            RunspaceConfiguration config = RunspaceConfiguration.Create();
            DistributionGroup group = null;
            List<DistributionGroup> groups = new List<DistributionGroup>();
            PSSnapInException warning;
            int total_entries = 0;
            // Load Exchange PowerShell snap-in.
            config.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.Admin", out warning);
            if (warning != null) throw warning;

            using (Runspace thisRunspace = RunspaceFactory.CreateRunspace(config))
            {
                try
                {
                    thisRunspace.Open();
                    using (Pipeline thisPipeline = thisRunspace.CreatePipeline())
                    {

                        thisPipeline.Commands.Add("Get-DistributionGroup");
                        if (identity != "") thisPipeline.Commands[0].Parameters.Add("Identity", @identity);
                        thisPipeline.Commands[0].Parameters.Add("SortBy", "DisplayName");
                        try
                        {
                            List<PSObject> original_results = thisPipeline.Invoke().ToList();
                            IEnumerable<PSObject> results = null;

                            if (ou != "")
                                original_results = original_results.Where(x => x.Members["OrganizationalUnit"].Value.ToString() == ou).ToList();
                            total_entries = original_results.Count;
                            if (current_page == 0 && per_page == 0)
                                results = original_results;
                            else if (current_page < 2)
                                results = original_results.Take<PSObject>(per_page);// + 1); // This one is working as you would expect, as opposed to users. not sure what was done there
                            else
                                results = original_results.Skip<PSObject>((current_page - 1) * per_page).Take<PSObject>(per_page);
                           foreach (PSObject result in results)
                           {

                               group = new DistributionGroup();

                               foreach (PSMemberInfo member in result.Members)
                               {
                                   switch (member.Name)
                                   {
                                       case "Alias":
                                           group.Alias = member.Value.ToString().Trim();
                                           break;
                                       case "Name":
                                           group.Name = member.Value.ToString().Trim();
                                           break;
                                       case "DisplayName":
                                           group.displayName = member.Value.ToString().Trim();
                                           break;
                                       case "GroupType":
                                           group.groupType = member.Value.ToString().Trim();
                                           break;
                                       case "PrimarySmtpAddress":
                                           group.primarySmtpAddress = member.Value.ToString();
                                           break;
                                       case "CustomAttribute11": // We're using attribute 11 to indicate if we have children or not. it will be blank or true most likely
                                           bool hasChildren;
                                           if (bool.TryParse(member.Value.ToString().Trim(), out hasChildren) && hasChildren)
                                               group.HasChildren = true;
                                           else
                                               group.HasChildren = false;
                                           break;
                                   }

                               }
                               if (group.displayName.Length > 0)
                               {
                                   group.users = new ExchangeUserMembers();
                                   if (identity != "")
                                       group.users.users = GetDistributionGroupMembers(group.Name);
                                   groups.Add(group);
                               }

                           }

                        }
                        catch (Exception ex)
                        {
                            ErrorText = "Error: " + ex.ToString();
                            return ErrorText;
                        }
                        // Check for errors in the pipeline and throw an exception if necessary.
                        if (thisPipeline.Error != null && thisPipeline.Error.Count > 0)
                        {
                            StringBuilder pipelineError = new StringBuilder();
                            pipelineError.AppendFormat("Error calling Get-DistributionGroup.");
                            foreach (object item in thisPipeline.Error.ReadToEnd())
                            {
                                pipelineError.AppendFormat("{0}\n", item.ToString());
                            }
                            ErrorText = ErrorText + "Error: " + pipelineError.ToString();
                            return ErrorText;
                        }

                    }

                }
                finally
                {
                    thisRunspace.Close();
                }

            }
            var shorty = new DistributionGroupsShorter() { PerPage = per_page, CurrentPage = current_page,
                TotalEntries = total_entries, groups = groups };
            return XmlSerializationHelper.Serialize(shorty);
        }