0

I'm trying to retrieve all the subnets in our AD. I need the values from the cn, location, description and Site attributes. I can see these attributes when i open the "Active Directory Sites and Services" app and select "subnets"

I found some code which i thought would retrieve these values

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

        }

However, the "Description" attribute isn't there. Each Subnet only exposes 3 properties/attributes. Does anyone know how i can get access to all the Attributes on all our subnets ?

Thanks

Erck

4

1 回答 1

0

好的,所以当您引用 AD 中的对象时,您可以GetDirectoryEntry对其执行

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)))
   }

}

以及您想要的任何其他属性。

于 2013-10-25T05:57:32.180 回答