예제 #1
0
        public System.Data.DataTable GetQueryDataTable()
        {
            System.Data.DataTable dt = new System.Data.DataTable();
            List <string>         propertiesToLoad = new List <string>();

            if (PropertiesToLoad == null || PropertiesToLoad.Trim().Length == 0)
            {
                propertiesToLoad.Add("sAMAccountName");
                dt.Columns.Add(new System.Data.DataColumn("sAMAccountName", typeof(string)));
            }
            else
            {
                foreach (string propName in PropertiesToLoad.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
                {
                    propertiesToLoad.Add(propName.Trim());
                    dt.Columns.Add(new System.Data.DataColumn(propName.Trim(), typeof(string)));
                }
            }

            if (QueryFilterText == null || QueryFilterText.Trim().Length == 0)
            {
                return(dt);
            }
            string formattedFilterText = FormatQueryFilter(QueryFilterText);

            if (formattedFilterText.Contains("%"))
            {
                formattedFilterText = MacroVariables.FormatVariables(formattedFilterText);
            }

            //int lineCount = 0;
            DirectoryEntry entryRoot = GetADRoot();

            using (DirectorySearcher searcher = new DirectorySearcher(entryRoot, formattedFilterText))
            {
                searcher.PropertiesToLoad.AddRange(propertiesToLoad.ToArray());
                searcher.SizeLimit = 1000;
                searcher.PageSize  = 1000;
                SearchResultCollection matches = searcher.FindAll();

                foreach (SearchResult match in matches)
                {
                    List <string> rowValues = new List <string>();
                    foreach (string propName in propertiesToLoad)
                    {
                        string propertyValue = "";
                        try
                        {
                            if (match.Properties.Contains(propName))
                            {
                                if (match.Properties[propName].Count > 0 && match.Properties[propName] != null)
                                {
                                    propertyValue = match.Properties[propName][0].ToString();
                                }
                                else
                                {
                                    propertyValue = "N/A";
                                }
                            }
                            else
                            {
                                propertyValue = "N/A";
                            }
                        }
                        catch
                        {
                            propertyValue = "err";
                        }
                        rowValues.Add(propertyValue);
                    }
                    dt.Rows.Add(rowValues.ToArray());
                    //lineCount++;
                    //if (lineCount >= MaxRowsToEvaluate)
                    //    break;
                }
            }
            return(dt);
        }
예제 #2
0
        //For Collector testing purposes only the first MaxRowsToEvaluate number of returned records are used unless UseRowCountAsValue is true.
        public object RunQuery()
        {
            object returnValue = null;

            if (QueryFilterText == null || QueryFilterText.Trim().Length == 0)
            {
                return(null);
            }
            string formattedFilterText = FormatQueryFilter(QueryFilterText);

            if (formattedFilterText.Contains("%"))
            {
                formattedFilterText = MacroVariables.FormatVariables(formattedFilterText);
            }

            DirectoryEntry entryRoot = GetADRoot();

            using (DirectorySearcher searcher = new DirectorySearcher(entryRoot, formattedFilterText))
            {
                List <string> propertiesToLoad = new List <string>();
                if (PropertiesToLoad == null || PropertiesToLoad.Trim().Length == 0)
                {
                    propertiesToLoad.Add("sAMAccountName");
                }
                else
                {
                    foreach (string propName in PropertiesToLoad.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
                    {
                        propertiesToLoad.Add(propName.Trim());
                    }
                }
                searcher.PropertiesToLoad.AddRange(propertiesToLoad.ToArray());
                searcher.SizeLimit = 1000;
                searcher.PageSize  = 1000;
                SearchResultCollection matches = searcher.FindAll();
                if (UseRowCountAsValue)
                {
                    returnValue = matches.Count;
                }
                else
                {
                    StringBuilder lines     = new StringBuilder();
                    int           lineCount = 0;
                    foreach (SearchResult match in matches)
                    {
                        StringBuilder line = new StringBuilder();
                        foreach (string propName in propertiesToLoad)
                        {
                            string propertyValue = "";
                            try
                            {
                                if (match.Properties.Contains(propName))
                                {
                                    if (match.Properties[propName].Count > 0 && match.Properties[propName] != null)
                                    {
                                        propertyValue = match.Properties[propName][0].ToString();
                                    }
                                    else
                                    {
                                        propertyValue = "N/A";
                                    }
                                }
                                else
                                {
                                    propertyValue = "N/A";
                                }
                            }
                            catch
                            {
                                propertyValue = "err";
                            }
                            line.Append(propertyValue + ",");
                        }
                        lines.AppendLine(line.ToString().Trim(','));
                        lineCount++;
                        if (lineCount >= MaxRowsToEvaluate)
                        {
                            break;
                        }
                    }
                    returnValue = lines.ToString();
                }
            }
            return(returnValue);
        }