简体   繁体   中英

How can I query Active Directory Sites and Services in c#?

I don't know if I'm using the wrong search terms, but I can't find a decent example of this anywhere.

I'd like to query a specific AD site and return the computer names inside of it in c#.

I've tried this without luck

ActiveDirectorySiteCollection coll = new ActiveDirectorySiteCollection("ad site name");

And visual studio tells me it doesn't take a constructor with a single argument?

You can use ActiveDirectorySite.FindByName to find a specific site, then look at the Servers property. For example:

var context = new DirectoryContext(DirectoryContextType.Forest);
var site = ActiveDirectorySite.FindByName(context, "SiteName");

foreach (DirectoryServer server in site.Servers) {
    Console.WriteLine(server.Name);
}

If you don't know the name of the site, you can look up all the sites in your forest like this:

var forest = Forest.GetCurrentForest();

foreach (ActiveDirectorySite site in forest.Sites) {
    Console.WriteLine(site.Name);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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