2013-10-24 85 views
0

我試圖檢索我們AD中的所有子網。 我需要cn,location,description和Site屬性中的值。 我可以看到這些屬性,當我打開「Active Directory站點和服務」應用程序,選擇「子網」查找AD中的所有子網

我發現一些代碼,我以爲會檢索這些值

 Forest myForest = Forest.GetCurrentForest(); 
     ReadOnlySiteCollection mySites = default(ReadOnlySiteCollection); 
     ActiveDirectorySubnetCollection mySubnets = default(ActiveDirectorySubnetCollection); 
     int iEnumSites = 0; 
     int iEnumSubnets = 0; 
     mySites = myForest.Sites; 
     Dictionary<string, Subnet> Subnets = new Dictionary<string, Subnet>(); 
     //for each site loop through 
     for (iEnumSites = 0; iEnumSites < mySites.Count -1; iEnumSites++) 
     { 
     // for each subnet in each site loop through 
     mySubnets = mySites[iEnumSites].Subnets; 
      for (iEnumSubnets = 0 ; iEnumSubnets < mySubnets.Count -1; iEnumSubnets++) 
      { 

       Subnet s = new Subnet(mySubnets[iEnumSubnets].Name, mySubnet[iEnumSubnets].Site, mySubnets[iEnumSubnets].Location,""); 
       Subnets.Add(s.GetKey(), s); 
      } 

     } 

然而,「說明「屬性不存在。每個子網只公開3個屬性/屬性。 有誰知道我可以如何訪問我們所有子網上的所有屬性?

感謝

ERCK

回答

0

確定,所以,當你在AD有一個對象的引用,你可以在它

Forest myForest = Forest.GetCurrentForest(); 
ReadOnlySiteCollection mySites = default(ReadOnlySiteCollection); 
ActiveDirectorySubnetCollection mySubnets = default(ActiveDirectorySubnetCollection); 
int iEnumSites = 0; 
int iEnumSubnets = 0; 
mySites = myForest.Sites; 
Dictionary<string, Subnet> Subnets = new Dictionary<string, Subnet>(); 
//for each site loop through 
for (iEnumSites = 0; iEnumSites < mySites.Count -1; iEnumSites++) 
{ 
    // for each subnet in each site loop through 
    mySubnets = mySites[iEnumSites].Subnets; 
    for (iEnumSubnets = 0 ; iEnumSubnets < mySubnets.Count -1; iEnumSubnets++) 
    { 
     var ent= mySubnet[iEnumSubnets].GetDirectoryEntry(); 

     // you now have access to all attributes e.g the description attribute 
     string.Join(",",ent.Properties["description"].Cast<object>().Select(v => Convert.ToString(v))) 
    } 

} 

和任何其他屬性你想要做一個GetDirectoryEntry