簡體   English   中英

從OU獲取計算機

[英]get computer from OU

我有一個代碼來獲取域中所有計算機的列表。

現在,我只需要獲取位於特定OU中的計算機,而不是其余計算機。

所以這是我的代碼,用於從域中獲取所有計算機,這工作得很好:

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();

有什么建議么?? 謝謝。

您只需要將OU添加到目錄條目中,因此,無需將域的根作為搜索路徑,而是將域+ OU作為搜索路徑。

請參閱“枚舉OU中的對象” @ http://www.codeproject.com/KB/system/everythingInAD.aspx

我從您的評論中看到您在這里遇到了問題,所以我們簡單地講一下-請注意,此代碼未經測試,但應予以澄清...

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

這實際上為您提供了字符串“ LDAP:// OU = LosAngeles,OU = America,CN = MyCompany,CN = com”作為新目錄條目。 必須指定完整的LDAP路徑,而不僅僅是OU或域。

嘗試使用此目錄條目:

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

我嘗試了以上所有方法..但是沒有用...所以這就是我嘗試過的並且可以使用。

我知道這不是最好的方法,但是對我來說是唯一的方法。任何建議..謝謝

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