{"id":68583,"date":"2005-11-07T21:30:00","date_gmt":"2005-11-07T21:30:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/11\/07\/how-can-i-list-all-the-sites-in-active-directory-as-well-as-all-the-servers-in-those-sites\/"},"modified":"2005-11-07T21:30:00","modified_gmt":"2005-11-07T21:30:00","slug":"how-can-i-list-all-the-sites-in-active-directory-as-well-as-all-the-servers-in-those-sites","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-list-all-the-sites-in-active-directory-as-well-as-all-the-servers-in-those-sites\/","title":{"rendered":"How Can I List All the Sites in Active Directory as well as All the Servers in Those Sites?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"> \n<P>Hey, Scripting Guy! How can I list all the sites in Active Directory as well as all the servers in those sites?<BR><BR>&#8212; DW<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" border=\"0\" alt=\"Script Center\" align=\"right\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" height=\"288\"><\/A> \n<P>Hey, DW. You know, you\u2019re putting us to the test here. Ask us a question about users and groups and we\u2019re fine; we at least <I>kind of<\/I> 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\u2019re never really convinced that we even know what those things are, let alone how you can script them. <\/P>\n<P>But would the Scripting Guys ever let a total lack of knowledge and understanding stop them? Of course not. Here\u2019s a script that lists all of the sites in Active Directory, along with the servers found in each of those sites:<\/P><PRE class=\"codeSample\">On Error Resume Next<\/p>\n<p>Set objRootDSE = GetObject(&#8220;LDAP:\/\/RootDSE&#8221;)\nstrConfigurationNC = objRootDSE.Get(&#8220;configurationNamingContext&#8221;)<\/p>\n<p>strSitesContainer = &#8220;LDAP:\/\/cn=Sites,&#8221; &amp; strConfigurationNC\nSet objSitesContainer = GetObject(strSitesContainer)\nobjSitesContainer.Filter = Array(&#8220;site&#8221;)<\/p>\n<p>For Each objSite In objSitesContainer\n    Wscript.Echo objSite.CN\n    strSiteName = objSite.Name\n    strServerPath = &#8220;LDAP:\/\/cn=Servers,&#8221; &amp; strSiteName &amp; &#8220;,cn=Sites,&#8221; &amp; _\n        strConfigurationNC\n    Set colServers = GetObject(strServerPath)<\/p>\n<p>    For Each objServer In colServers\n        WScript.Echo vbTab &amp; objServer.CN\n    Next\n    Wscript.Echo\nNext\n<\/PRE>\n<P>So what are we doing here? (Actually, that wasn\u2019t a rhetorical question, we were hoping someone would explain it all to <I>us<\/I>!) We begin by using these two lines of code to bind to the Active Directory root and then to the Configuration container (<B>configuringNamingContext<\/B>):<\/P><PRE class=\"codeSample\">Set objRootDSE = GetObject(&#8220;LDAP:\/\/RootDSE&#8221;)\nstrConfigurationNC = objRootDSE.Get(&#8220;configurationNamingContext&#8221;)\n<\/PRE>\n<P>Why do we start out like this? Well, the Configuration container holds information about the physical structure and layout of Active Directory; that\u2019s where we\u2019ll find information about sites and about the servers assigned to each site. By binding to the Active Directory root (<B>rootDSE<\/B>) and then using the <B>Get<\/B> method to retrieve the configurationNamingContext we can get the Active Directory path to the Configuration container regardless of the name of our domain.<\/P>\n<TABLE id=\"EPD\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Note<\/B>. 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\u2019t even reference fabrikam.com (or any other domain name) anywhere in the script.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Our next step is to retrieve a collection of all the Active Directory sites. That\u2019s what we do here:<\/P><PRE class=\"codeSample\">strSitesContainer = &#8220;LDAP:\/\/cn=Sites,&#8221; &amp; strConfigurationNC\nSet objSitesContainer = GetObject(strSitesContainer)\nobjSitesContainer.Filter = Array(&#8220;site&#8221;)\n<\/PRE>\n<P>In the first line we construct the ADsPath to the Sites container, combining <B>LDAP:\/\/cn=Sites<\/B> and the configurationNamingContext. We use the <B>GetObject<\/B> method to bind to the Container, then apply a <B>Filter<\/B> that limits items in the collection to <B>Site<\/B> objects. The net result: objSitesContainer will now contain a collection of all our Active Directory sites.<\/P>\n<P>OK, so maybe this isn\u2019t 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 <B>CN<\/B> for the first site in the collection, then store the site <B>Name<\/B> (which will be something like CN=Default-First-Site-Name) in the variable strSiteName:<\/P><PRE class=\"codeSample\">Wscript.Echo objSite.CN\nstrSiteName = objSite.Name\n<\/PRE>\n<P>As it turns out, each Active Directory site has a <B>Servers<\/B> 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:<\/P><PRE class=\"codeSample\">strServerPath = &#8220;LDAP:\/\/cn=Servers,&#8221; &amp; strSiteName &amp; &#8220;,cn=Sites,&#8221; &amp; _\n    strConfigurationNC\n<\/PRE>\n<P>Once we have the ADsPath we can then bind to the Servers container for the first site using this line of code:<\/P><PRE class=\"codeSample\">Set colServers = GetObject(strServerPath)\n<\/PRE>\n<P>We\u2019re 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:<\/P><PRE class=\"codeSample\">For Each objServer In colServers\n    WScript.Echo vbTab &amp; objServer.CN\nNext\n<\/PRE>\n<P>Oh, OK: we also put a tab character (vbTab) in front of the server name. You\u2019ll see why we do that when you take a look at the sample output.<\/P>\n<P>And that\u2019s it: we\u2019ve 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\u2019ve worked with all the sites and echoed back the names of all the servers in those sites. When we\u2019re all done we should have output similar to this:<\/P><PRE class=\"codeSample\">Default-First-Site-Name\n    atl-dc-01\n    atl-dc-02<\/p>\n<p>European-Site\n    lon-dc-01\n    lon-dc-02\n    par-dc-01\n    bru-dc-01\n    bru-dc-02\n<\/PRE>\n<P>Not bad, huh? And just imagine what a cool script we could have written had we actually known what we were doing!<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I list all the sites in Active Directory as well as all the servers in those sites?&#8212; DW Hey, DW. You know, you\u2019re putting us to the test here. Ask us a question about users and groups and we\u2019re fine; we at least kind of understand what users and groups [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[7,3,5],"class_list":["post-68583","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-active-directory","tag-scripting-guy","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I list all the sites in Active Directory as well as all the servers in those sites?&#8212; DW Hey, DW. You know, you\u2019re putting us to the test here. Ask us a question about users and groups and we\u2019re fine; we at least kind of understand what users and groups [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68583","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=68583"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68583\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=68583"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=68583"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=68583"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}