{"id":70793,"date":"2004-12-16T10:16:00","date_gmt":"2004-12-16T10:16:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2004\/12\/16\/how-can-i-get-a-list-of-all-the-domain-controllers-in-my-domain\/"},"modified":"2004-12-16T10:16:00","modified_gmt":"2004-12-16T10:16:00","slug":"how-can-i-get-a-list-of-all-the-domain-controllers-in-my-domain","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-get-a-list-of-all-the-domain-controllers-in-my-domain\/","title":{"rendered":"How Can I Get a List of All the Domain Controllers in My Domain?"},"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! Is it possible to get a list of all the domain controllers in my domain?<BR><BR>&#8212; KT<\/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, KT. As a matter of fact, there are at least two ways to get a list of all domain controllers in your domain. The first way is pretty easy, but not guaranteed; depending on how you\u2019ve set up Active Directory, you could miss a few of your domain controllers. The second way is a little bit more complicated, but barring any unforeseen circumstances, should always return a list of <I>all<\/I> your domain controllers.<\/P>\n<P>Let\u2019s take a look at the easy way first. By default, the Active Directory computer account for a domain controller is stored in the Domain Controllers OU. If that\u2019s where all your domain controllers accounts are stored, then you can retrieve your list just by enumerating the computer accounts in that OU:<\/P><PRE class=\"codeSample\">Set objOU = GetObject(\u201cLDAP:\/\/ou=Domain Controllers, dc=fabrikam, dc=com\u201d)\nobjOU.Filter = Array(\u201cComputer\u201d)\nFor Each objComputer in objOU\n    Wscript.Echo objComputer.CN\nNext\n<\/PRE>\n<P>The preceding script binds to the Domain Controllers OU and applies a filter to ensure that only computer accounts are returned. A simple For Each loop then walks us through the collection of returned computer accounts, echoing the CN (Common Name) for each one.<\/P>\n<P>So what\u2019s wrong with this script? Well, maybe nothing. However, it\u2019s possible that you have domain controller accounts located elsewhere in Active Directory; if so, this script won\u2019t do you much good. Likewise, it\u2019s possible that you might have other computer accounts (such as those for member servers) in the Domain Controllers; if so, this script will mistakenly identify those computers as domain controllers. That\u2019s because the script is just looking for computer accounts, period. <\/P>\n<P>So what\u2019s a more sure-fire way to get a list of all your domain controllers? Well, if you\u2019re a regular reader of <I>Hey, Scripting Guy!<\/I> then you probably already guessed the answer: search Active Directory.<\/P>\n<P>We know, sometimes it sounds like \u201cSearch Active Directory\u201d is our standard response to <I>any<\/I> question. But, hey, Active Directory is a veritable storehouse of information, and it only makes sense to tap into that storehouse any chance you get. You want a list of all the domain controllers in a domain? Then run this script:<\/P><PRE class=\"codeSample\">On Error Resume Next<\/p>\n<p>Const ADS_SCOPE_SUBTREE = 2<\/p>\n<p>Set objRootDSE = GetObject(&#8220;LDAP:\/\/RootDSE&#8221;)\nstrConfigurationNC = objRootDSE.Get(&#8220;configurationNamingContext&#8221;)<\/p>\n<p>Set objConnection = CreateObject(&#8220;ADODB.Connection&#8221;)\nSet objCommand =   CreateObject(&#8220;ADODB.Command&#8221;)\nobjConnection.Provider = &#8220;ADsDSOObject&#8221;\nobjConnection.Open &#8220;Active Directory Provider&#8221;\nSet objCommand.ActiveConnection = objConnection<\/p>\n<p>objCommand.Properties(&#8220;Page Size&#8221;) = 1000\nobjCommand.Properties(&#8220;Searchscope&#8221;) = ADS_SCOPE_SUBTREE <\/p>\n<p>objCommand.CommandText = _\n    &#8220;SELECT ADsPath FROM &#8216;LDAP:\/\/&#8221; &amp; strConfigurationNC &amp; &#8220;&#8216; WHERE objectClass=&#8217;nTDSDSA'&#8221;  \nSet objRecordSet = objCommand.Execute<\/p>\n<p>objRecordSet.MoveFirst\nDo Until objRecordSet.EOF\n    Set objParent = GetObject(GetObject(objRecordset.Fields(&#8220;ADsPath&#8221;)).Parent)\n    WScript.Echo objParent.CN\n    objRecordSet.MoveNext\nLoop\n<\/PRE>\n<P>Ok, so it is a <I>little<\/I> more complicated, but it works and it will find all the domain controllers regardless of the location of their Active Directory accounts. The script starts out by binding to RootDSE and then connecting to the configuration naming context for the domain. (The configuration naming context &#8211; or configuration directory partition &#8211; holds information of global interest; for example, you\u2019ll find things like the default configuration and policy information for all instances of a given service in the forest.)<\/P>\n<P>From there we do a typical Active Directory search: we want to get the ADsPath for all nTDSDSA objects in the configuration naming context. For our purposes, nTDSDSA is short (in a roundabout way) for Directory System Agent, the software responsible &#8211; among other things &#8211; for providing access to the directory service. Which, of course, is exactly what a domain controller does.<\/P>\n<P>Note, however, that a list of all the nTDSDSA objects is not the same thing as a list of all our domain controllers. The ADsPath to an nTDSDSA object tells us how to find that object in Active Directory; what it <I>doesn\u2019t<\/I> tell us is the name of the computer that object is installed on (and if you have the nTDSDSA object installed, then you must be a domain controller). To determine the computer name, we have this crazy line of code:<\/P><PRE class=\"codeSample\">Set objParent = GetObject(GetObject(objRecordset.Fields(&#8220;ADsPath&#8221;)).Parent)\n<\/PRE>\n<P>What we\u2019re doing here is first binding to an nTDSDSA object, and then immediately binding to that object\u2019s <B>Parent<\/B>. In the case of the nTDSDSA object, the Parent object is the actual computer itself; in other words, binding to the nTDSDSA Parent binds us to the computer account (which, remember, has to be a domain controller, or it wouldn\u2019t have the nTDSDSA object). At that point, we\u2019ve finally reached a domain controller, and all we have to do then is echo back the CN for this computer. We then repeat the loop until we\u2019ve handled all the nTDSDSA objects and &#8211; by extension &#8211; all the domain controllers in the domain.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! Is it possible to get a list of all the domain controllers in my domain?&#8212; KT Hey, KT. As a matter of fact, there are at least two ways to get a list of all domain controllers in your domain. The first way is pretty easy, but not guaranteed; depending on how [&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,168,3,5],"class_list":["post-70793","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-active-directory","tag-domains","tag-scripting-guy","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! Is it possible to get a list of all the domain controllers in my domain?&#8212; KT Hey, KT. As a matter of fact, there are at least two ways to get a list of all domain controllers in your domain. The first way is pretty easy, but not guaranteed; depending on how [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70793","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=70793"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70793\/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=70793"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=70793"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=70793"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}