2011-03-01 98 views
0

我有一個代碼來獲取域內所有計算機的列表。從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(); 

什麼建議?謝謝。

回答

2

您只需將OU添加到您的目錄條目中,而不是將您的域的根作爲搜索路徑,它將域+ OU作爲搜索路徑。

參見「枚​​舉OU中的對象」 @http://www.codeproject.com/KB/system/everythingInAD.aspx

我從你的commments,你這裏有問題看,讓我們把這個簡單的 - 注意,此代碼沒有進行測試,但應該澄清。 ..

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

這基本上是給你 「LDAP:// OU =洛杉磯,OU =美國,CN = myCompany中,CN = COM」 的字符串作爲新目錄條目。您必須必須指定完整的LDAP路徑,而不僅僅是OU或域。

+0

我想這:的DirectoryEntry條目=新的DirectoryEntry(「LDAP:// OU =「+ selectedOU);但我得到一個錯誤:System.DirectoryServices.DirectoryServicesCOMException(0x80072020):發生操作錯誤。 – user175084 2011-03-02 01:00:03

+1

查看更新的答案。 – dotalchemy 2011-03-02 01:06:01

+0

所以我有我的OU =域控制器和域= freeTest.test.com 因此,我有DirectoryEntry項=新DirectoryEntry(「LDAP://」+「OU =域控制器」+「,」+「CN = freeTest .test,CN = com「); 我在做正確的事情?謝謝 – user175084 2011-03-02 01:19:35

1

嘗試使用此目錄條目:

的DirectoryEntry條目=新的DirectoryEntry(的String.Format( 「LDAP:// OU = {0},{1}」,ouName,selectDomain));

+0

我試過這個,但出現錯誤:「System.DirectoryServices.DirectoryServicesCOMException(0x80072032):指定了無效的dn語法。 – user175084 2011-03-02 00:46:05

+0

您可能沒有正確指定LDAP字符串。它必須是完全限定條目,例如「LDAP:// OU = LosAngeles,OU = America,CN = MyCompany,CN = com」 - 設置斷點並檢查以確保格式化的LDAP字符串正確。 – dotalchemy 2011-03-02 00:54:33

0

我嘗試了以上所有..但它沒有工作...所以這就是我所嘗試和它的工作。

我理解,這是不是最好的方式,但它的唯一的方式爲我工作...任何建議..感謝

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