示例#1
0
        public NeoQueryData CollectData()
        {
            NeoQueryData  querydata    = new NeoQueryData();
            List <object> propertylist = new List <object>();


            try
            {
                using (PrincipalSearcher principalSearcher = new PrincipalSearcher(new UserPrincipal(this._context)))
                {
                    try
                    {
                        DirectorySearcher searcher = principalSearcher.GetUnderlyingSearcher() as DirectorySearcher;
                        if (searcher != null)
                        {
                            searcher.Filter      = ("(&(objectCategory=foreignSecurityPrincipal))");
                            searcher.SearchScope = SearchScope.Subtree;
                            searcher.PropertiesToLoad.Add("cn");
                            searcher.PropertiesToLoad.Add("name");
                            searcher.PropertiesToLoad.Add("objectcategory");
                            searcher.PropertiesToLoad.Add("objectSid");
                            searcher.PropertiesToLoad.Add("distinguishedName");
                            searcher.PropertiesToLoad.Add("description");

                            searcher.PageSize = 1000;
                            SearchResultCollection results = searcher.FindAll();

                            foreach (SearchResult result in results)
                            {
                                propertylist.Add(new
                                {
                                    name        = ADSearchResultConverter.GetSinglestringValue(result, "Name"),
                                    dn          = ADSearchResultConverter.GetSinglestringValue(result, "distinguishedname"),
                                    description = ADSearchResultConverter.GetSinglestringValue(result, "Description"),
                                    id          = ADSearchResultConverter.GetSidAsString(result)
                                });
                            }
                        }
                        else
                        {
                            Program.ExitError("Error retrieving foreign security principals from AD", ErrorCodes.ForeignSecurityPrincipalCollectorSearcherNull);
                        }
                    }
                    catch (Exception e)
                    {
                        Program.ExitError(e, "Error retrieving foreign security principals from AD", ErrorCodes.ForeignSecurityPrincipalCollectorSearcherException);
                    }
                }
            }
            catch { }

            querydata.Properties = propertylist;
            return(querydata);
        }
示例#2
0
        public NeoQueryData CollectData()
        {
            NeoQueryData  querydata    = new NeoQueryData();
            List <object> propertylist = new List <object>();

            try
            {
                using (PrincipalSearcher principalSearcher = new PrincipalSearcher(new UserPrincipal(this._context)))
                {
                    try
                    {
                        DirectorySearcher searcher = principalSearcher.GetUnderlyingSearcher() as DirectorySearcher;
                        if (searcher != null)
                        {
                            searcher.Filter      = ("(&(objectCategory=group))");
                            searcher.SearchScope = SearchScope.Subtree;
                            searcher.PropertiesToLoad.Add("cn");
                            searcher.PropertiesToLoad.Add("memberof");
                            searcher.PropertiesToLoad.Add("name");
                            searcher.PropertiesToLoad.Add("samaccountname");
                            searcher.PropertiesToLoad.Add("grouptype");
                            searcher.PropertiesToLoad.Add("member");
                            searcher.PropertiesToLoad.Add("objectcategory");
                            searcher.PropertiesToLoad.Add("objectSid");
                            searcher.PropertiesToLoad.Add("distinguishedName");
                            searcher.PropertiesToLoad.Add("description");
                            searcher.PropertiesToLoad.Add("info");

                            searcher.PageSize = 1000;
                            SearchResultCollection results = searcher.FindAll();

                            foreach (SearchResult result in results)
                            {
                                string id      = ADSearchResultConverter.GetSidAsString(result);
                                string dn      = ADSearchResultConverter.GetSinglestringValue(result, "distinguishedname");
                                var    members = ADSearchResultConverter.GetStringList(result, "member");

                                propertylist.Add(new
                                {
                                    name = ADSearchResultConverter.GetSinglestringValue(result, "Name"),
                                    dn,
                                    description = ADSearchResultConverter.GetSinglestringValue(result, "Description"),
                                    id,
                                    info           = ADSearchResultConverter.GetSinglestringValue(result, "info"),
                                    grouptype      = GetTypeAndScope(ADSearchResultConverter.GetSinglestringValue(result, "grouptype")),
                                    samaccountname = ADSearchResultConverter.GetSinglestringValue(result, "samaccountname"),
                                    members,
                                    rid         = ADSearchResultConverter.GetRidFromSid(id),
                                    path        = dn,
                                    membercount = members.Count
                                });

                                foreach (string memberdn in members)
                                {
                                    this.GroupMemberships.Add(new { id, memberdn });
                                }
                            }
                        }
                        else
                        {
                            Program.ExitError("Error retrieving groups from AD", ErrorCodes.GroupsCollectorSearcherNull);
                        }
                    }
                    catch (Exception e)
                    {
                        Program.ExitError(e, "Error retrieving groups from AD", ErrorCodes.GroupsCollectorException);
                    }
                }
            }
            catch { }

            querydata.Properties = propertylist;
            return(querydata);
        }
        public NeoQueryData CollectData()
        {
            NeoQueryData  querydata    = new NeoQueryData();
            List <object> propertylist = new List <object>();

            querydata.Properties = propertylist;

            using (PrincipalSearcher principalSearcher = new PrincipalSearcher(new UserPrincipal(this._context)))
            {
                try
                {
                    DirectorySearcher searcher = principalSearcher.GetUnderlyingSearcher() as DirectorySearcher;
                    if (searcher != null)
                    {
                        searcher.Filter      = ("(&(objectCategory=computer))");
                        searcher.SearchScope = SearchScope.Subtree;
                        searcher.PropertiesToLoad.Add("name");
                        searcher.PropertiesToLoad.Add("cn");
                        searcher.PropertiesToLoad.Add("samaccountname");
                        searcher.PropertiesToLoad.Add("objectcategory");
                        searcher.PropertiesToLoad.Add("objectSid");
                        searcher.PropertiesToLoad.Add("distinguishedName");
                        searcher.PropertiesToLoad.Add("operatingSystem");
                        searcher.PropertiesToLoad.Add("operatingSystemVersion");
                        searcher.PropertiesToLoad.Add("primaryGroupID");
                        searcher.PropertiesToLoad.Add("description");
                        searcher.PropertiesToLoad.Add("info");

                        searcher.PageSize = 1000;
                        SearchResultCollection results = searcher.FindAll();


                        foreach (SearchResult result in results)
                        {
                            string id = ADSearchResultConverter.GetSidAsString(result);
                            string dn = ADSearchResultConverter.GetSinglestringValue(result, "distinguishedname");

                            //find if the computer is enabled. use bitwise comparison
                            int  istate  = ADSearchResultConverter.GetIntSingleValue(result, "useraccountcontrol");
                            bool enabled = (((UserAccountControlDefinitions)istate & UserAccountControlDefinitions.ACCOUNTDISABLE) == UserAccountControlDefinitions.ACCOUNTDISABLE) ? false : true;

                            propertylist.Add(new
                            {
                                name = ADSearchResultConverter.GetSinglestringValue(result, "Name"),
                                dn,
                                description = ADSearchResultConverter.GetSinglestringValue(result, "Description"),
                                id,
                                info                   = ADSearchResultConverter.GetSinglestringValue(result, "info"),
                                samaccountname         = ADSearchResultConverter.GetSinglestringValue(result, "samaccountname"),
                                path                   = dn,
                                displayname            = ADSearchResultConverter.GetSinglestringValue(result, "displayname"),
                                operatingsystem        = ADSearchResultConverter.GetSinglestringValue(result, "operatingsystem"),
                                operatingsystemversion = ADSearchResultConverter.GetSinglestringValue(result, "operatingsystemversion"),
                                enabled,
                                primarygroupid = ADSearchResultConverter.GetSinglestringValue(result, "primaryGroupID")
                            });
                        }
                    }
                    else
                    {
                        Program.ExitError("Error retrieving computers from AD", ErrorCodes.ComputersCollectorSearcherNull);
                    }
                }
                catch (Exception e)
                {
                    //timer.Stop();
                    Program.ExitError(e, "Error retrieving computers from AD", ErrorCodes.ComputersCollectorSearcherException);
                }
            }
            return(querydata);
        }