简体   繁体   English

从OU获取计算机

[英]get computer from OU

I have a code to get a list of all the computers within a domain. 我有一个代码来获取域中所有计算机的列表。

Now i need to just get the computers which are within a particular OU and not the rest of the machines. 现在,我只需要获取位于特定OU中的计算机,而不是其余计算机。

so here is my code to get all the machines from a domain, this works perfectly fine: 所以这是我的代码,用于从域中获取所有计算机,这工作得很好:

DirectoryEntry entry = new DirectoryEntry("LDAP://" + selectDomain);
        DirectorySearcher mySearcher = new DirectorySearcher(entry);
        mySearcher.Filter = ("(objectClass=computer)");
        mySearcher.SizeLimit = int.MaxValue;
        mySearcher.PageSize = int.MaxValue;

        foreach (SearchResult resEnt in mySearcher.FindAll())
        {
            //"CN=SGSVG007DC"
            string ComputerName = resEnt.GetDirectoryEntry().Name;
            if (ComputerName.StartsWith("CN="))
                ComputerName = ComputerName.Remove(0, "CN=".Length);
            compList.Add(ComputerName);
        }

        mySearcher.Dispose();
        entry.Dispose();

any suggestions?? 有什么建议么?? thanks. 谢谢。

You just need to add the OU to your directory entry, so instead of taking the root of your domain as being the search path, it takes the domain + OU as being the search path. 您只需要将OU添加到目录条目中,因此,无需将域的根作为搜索路径,而是将域+ OU作为搜索路径。

See "Enumerating objects in an OU" @ http://www.codeproject.com/KB/system/everythingInAD.aspx 请参阅“枚举OU中的对象” @ http://www.codeproject.com/KB/system/everythingInAD.aspx

I see from your commments that you're having issues here, so let's put this simply - note that this code isn't tested, but should clarify... 我从您的评论中看到您在这里遇到了问题,所以我们简单地讲一下-请注意,此代码未经测试,但应予以澄清...

string selectDomain = "CN=myCompany,CN=com";
string selectOU = "OU=LosAngeles,OU=America";
DirectoryEntry entry = new DirectoryEntry("LDAP://" + selectOU + "," + selectDomain);

That essentially gives you the string of "LDAP://OU=LosAngeles,OU=America,CN=MyCompany,CN=com" as the new directory entry. 这实际上为您提供了字符串“ LDAP:// OU = LosAngeles,OU = America,CN = MyCompany,CN = com”作为新目录条目。 You must specify the full LDAP path, not just the OU or the domain. 必须指定完整的LDAP路径,而不仅仅是OU或域。

try to use this Directory entry: 尝试使用此目录条目:

DirectoryEntry entry = new DirectoryEntry(string.Format("LDAP://OU={0},{1}", ouName, selectDomain)); DirectoryEntry条目=新的DirectoryEntry(string.Format(“ LDAP:// OU = {0},{1}”,ouName,selectDomain));

i tried all the above.. but it did not work... so this is what i tried and it worked. 我尝试了以上所有方法..但是没有用...所以这就是我尝试过的并且可以使用。

i understand this is not the best way but its the only way working for me... any suggestion.. thanks 我知道这不是最好的方法,但是对我来说是唯一的方法。任何建议..谢谢

DirectoryEntry entry = new DirectoryEntry("LDAP://" + selectedDomain);
            DirectorySearcher mySearcher = new DirectorySearcher(entry);
            mySearcher.Filter = ("(objectClass=organizationalUnit)");
            mySearcher.SizeLimit = int.MaxValue;
            mySearcher.PageSize = int.MaxValue;
            foreach (SearchResult temp in mySearcher.FindAll())
            {
                Global.logger.Debug("OU = " + temp.Properties["name"][0].ToString());
                DirectoryEntry ou = temp.GetDirectoryEntry();
                DirectorySearcher mySearcher1 = new DirectorySearcher(ou);
                mySearcher1.Filter = ("(objectClass=computer)");
                mySearcher1.SizeLimit = int.MaxValue;
                mySearcher1.PageSize = int.MaxValue;

                if (temp.Properties["name"][0].ToString() == selectedOU)
                {
                    foreach (SearchResult resEnt in mySearcher1.FindAll())
                    {
                        //"CN=SGSVG007DC"
                        string ComputerName = resEnt.GetDirectoryEntry().Name;
                        Global.logger.Debug("ComputerName = " + resEnt.Properties["name"][0].ToString());
                        if (ComputerName.StartsWith("CN="))
                            ComputerName = ComputerName.Remove(0, "CN=".Length);
                        compList.Add(ComputerName);
                    }
                }

                mySearcher1.Dispose();
                ou.Dispose();
            }

            mySearcher.Dispose();
            entry.Dispose();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM