How Can I List All the Sites in Active Directory as well as All the Servers in Those Sites?

How Can I List All the Sites in Active Directory as well as All the Servers in Those Sites?

  • Comments 9
  • Likes
Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I list all the sites in Active Directory as well as all the servers in those sites?

-- DW

SpacerHey, Scripting Guy! AnswerScript Center

Hey, DW. You know, you’re putting us to the test here. Ask us a question about users and groups and we’re fine; we at least kind of understand what users and groups are all about. Ask about computer accounts or OUs; hey, no problem. When it comes to things like sites and site links and stuff like that, however, well, then we start to worry; after all, we’re never really convinced that we even know what those things are, let alone how you can script them.

But would the Scripting Guys ever let a total lack of knowledge and understanding stop them? Of course not. Here’s a script that lists all of the sites in Active Directory, along with the servers found in each of those sites:

On Error Resume Next

Set objRootDSE = GetObject("LDAP://RootDSE")
strConfigurationNC = objRootDSE.Get("configurationNamingContext")
 
strSitesContainer = "LDAP://cn=Sites," & strConfigurationNC
Set objSitesContainer = GetObject(strSitesContainer)
objSitesContainer.Filter = Array("site")
 
For Each objSite In objSitesContainer
    Wscript.Echo objSite.CN
    strSiteName = objSite.Name
    strServerPath = "LDAP://cn=Servers," & strSiteName & ",cn=Sites," & _
        strConfigurationNC
    Set colServers = GetObject(strServerPath)
 
    For Each objServer In colServers
        WScript.Echo vbTab & objServer.CN
    Next
    Wscript.Echo
Next

So what are we doing here? (Actually, that wasn’t a rhetorical question, we were hoping someone would explain it all to us!) We begin by using these two lines of code to bind to the Active Directory root and then to the Configuration container (configuringNamingContext):

Set objRootDSE = GetObject("LDAP://RootDSE")
strConfigurationNC = objRootDSE.Get("configurationNamingContext")

Why do we start out like this? Well, the Configuration container holds information about the physical structure and layout of Active Directory; that’s where we’ll find information about sites and about the servers assigned to each site. By binding to the Active Directory root (rootDSE) and then using the Get method to retrieve the configurationNamingContext we can get the Active Directory path to the Configuration container regardless of the name of our domain.

Note. What does that mean? For one thing, it means you can use this script as-is, without having to change fabrikam.com to the name of your domain; as you can see, we don’t even reference fabrikam.com (or any other domain name) anywhere in the script.

Our next step is to retrieve a collection of all the Active Directory sites. That’s what we do here:

strSitesContainer = "LDAP://cn=Sites," & strConfigurationNC
Set objSitesContainer = GetObject(strSitesContainer)
objSitesContainer.Filter = Array("site")

In the first line we construct the ADsPath to the Sites container, combining LDAP://cn=Sites and the configurationNamingContext. We use the GetObject method to bind to the Container, then apply a Filter that limits items in the collection to Site objects. The net result: objSitesContainer will now contain a collection of all our Active Directory sites.

OK, so maybe this isn’t as difficult as we thought it would be. Next we set up a For Each loop to walk through the collection of sites. Inside that loop we echo the CN for the first site in the collection, then store the site Name (which will be something like CN=Default-First-Site-Name) in the variable strSiteName:

Wscript.Echo objSite.CN
strSiteName = objSite.Name

As it turns out, each Active Directory site has a Servers container, and each Servers container holds a list of the servers assigned to that site. In order to bind to that Servers container for our first site we use this line of code to construct an ADsPath, a path we store in the variable strServerPath:

strServerPath = "LDAP://cn=Servers," & strSiteName & ",cn=Sites," & _
    strConfigurationNC

Once we have the ADsPath we can then bind to the Servers container for the first site using this line of code:

Set colServers = GetObject(strServerPath)

We’re on a roll now. Next we set up a second For Each loop that walks through the collection of items found in the Servers container. For each server in the collection we do nothing more than echo back the server CN:

For Each objServer In colServers
    WScript.Echo vbTab & objServer.CN
Next

Oh, OK: we also put a tab character (vbTab) in front of the server name. You’ll see why we do that when you take a look at the sample output.

And that’s it: we’ve now echoed back the name of the first site as well as all the servers assigned to that site. We then loop around and repeat the process with the second site in the collection. This continues until we’ve worked with all the sites and echoed back the names of all the servers in those sites. When we’re all done we should have output similar to this:

Default-First-Site-Name
    atl-dc-01
    atl-dc-02

European-Site
    lon-dc-01
    lon-dc-02
    par-dc-01
    bru-dc-01
    bru-dc-02

Not bad, huh? And just imagine what a cool script we could have written had we actually known what we were doing!

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • Very nice. Love you style and wit. By now you guys must be friggin Geniuses.

  • I appreciate your effort in writing this script.

    This is exactly what I was looking for. However I want the output in a text file and I do not have any scripting knowledge. Could anyone add the code to the existing script so that output is in a text file.

    Much appreciated!!

    Rav

  • Hey script guys..  How about one step further?

    A listing of sites, and all the replication links that those sites are contained in..

  • What about all computers in a site?  This is great, but it's only listing the DCs for me.

  • Agreed...this only lists DCs.  Is there a way to list all servers?  All computers?

  • @Nicole

    Get-AdComputer -filter *

  • thanks

  • Problem: HP Printer not connecting to my laptop.

    I had an issue while connecting my 2 year old HP printer to my brother's laptop that I had borrowed for starting my own business. I used a quick google search to fix the problem but that did not help me.

    I then decided to get professional help to solve my problem. After having received many quotations from various companies, i decided to go ahead with Online Tech Repair (www.onlinetechrepairs.com).

    Reasons I chose them over the others:
    1) They were extremely friendly and patient with me during my initial discussions and responded promptly to my request.
    2) Their prices were extremely reasonable.
    3) They were ready and willing to walk me through the entire process step by step and were on call with me till i got it fixed.

    How did they do it
    1) They first asked me to state my problem clearly and asked me a few questions. This was done to detect any physical connectivity issues with the printer.
    2) After having answered this, they confirmed that the printer and the laptop were functioning correctly.
    3) They then, asked me if they could access my laptop remotely to troubleshoot the problem and fix it. I agreed.
    4) One of the tech support executives accessed my laptop and started troubleshooting.
    5) I sat back and watched as the tech support executive was navigating my laptop to spot the issue. The issue was fixed.
    6) I was told that it was due to an older version of the driver that had been installed.

    My Experience
    I loved the entire friendly conversation that took place with them. They understood my needs clearly and acted upon the solution immediately. Being a technical noob, i sometimes find it difficult to communicate with tech support teams. It was a very different experience with the guys at Online Tech Repairs. You can check out their website www.onlinetechrepairs.com or call them on 1-914-613-3786.
    Would definitely recommend this service to anyone who needs help fixing their computers.
    Thanks a ton guys. Great Job....!!

  • Is Your VIRUS REMOVAL
    Computer Sluggish or Plagued With a Virus? – If So you Need Online Tech Repairs
    As a leader in online computer repair, Online Tech Repairs Inc has the experience to deliver professional system optimization and virus removal.Headquartered in Great Neck, New York our certified technicians have been providing online computer repair and virus removal for customers around the world since 2004.
    Our three step system is easy to use; and provides you a safe, unobtrusive, and cost effective alternative to your computer service needs. By using state-of-the-art technology our computer experts can diagnose, and repair your computer system through the internet, no matter where you are.
    Our technician will guide you through the installation of Online Tech Repair Inc secure software. This software allows your dedicated computer expert to see and operate your computer just as if he was in the room with you. That means you don't have to unplug everything and bring it to our shop, or have a stranger tramping through your home.
    From our remote location the Online Tech Repairs.com expert can handle any computer issue you want addressed, like:
    • - System Optimization
    • - How it works Software Installations or Upgrades
    • - How it works Virus Removal
    • - How it works Home Network Set-ups
    Just to name a few.
    If you are unsure of what the problem may be, that is okay. We can run a complete diagnostic on your system and fix the problems we encounter. When we are done our software is removed; leaving you with a safe, secure and properly functioning system. The whole process usually takes less than an hour. You probably couldn't even get your computer to your local repair shop that fast!
    Call us now for a FREE COMPUTER DIAGONISTIC using DISCOUNT CODE (otr214422@gmail.com) on +1-914-613-3786 or chat with us on www.onlinetechrepairs.com.