{"id":70523,"date":"2005-02-02T09:00:00","date_gmt":"2005-02-02T09:00:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/02\/02\/how-can-i-get-a-list-of-all-my-windows-server-2003-computers\/"},"modified":"2005-02-02T09:00:00","modified_gmt":"2005-02-02T09:00:00","slug":"how-can-i-get-a-list-of-all-my-windows-server-2003-computers","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-get-a-list-of-all-my-windows-server-2003-computers\/","title":{"rendered":"How Can I Get a List of All My Windows Server 2003 Computers?"},"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 gather the names of all the computers in my domain that are running Windows Server 2003?<BR><BR>&#8212; AS<\/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, AS. When we choose questions to answer for this column, we try to take questions from different areas of scripting. That does two things for us: it keeps us from being the answers-only-questions-about-say-scripting-Active-Directory column, and it ensures that we don\u2019t sound like we\u2019re providing the same answer to the same question day after day. Of course, on the other hand, we get lazy every now and then, and when we do, we usually fall back on one of our old standbys: search Active Directory. <\/P>\n<P>As you might have guessed, we\u2019re feeling lazy today. How can you gather the names of all the computers in your domain that are running Windows Server 2003? Search Active Directory.<\/P>\n<P>Here\u2019s a script that echoes back the names of all the computers in the fabrikam.com domain that are running Windows Server 2003:<\/P><PRE class=\"codeSample\">Const ADS_SCOPE_SUBTREE = 2<\/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;<\/p>\n<p>Set objCommand.ActiveConnection = objConnection\nobjCommand.CommandText = _\n    &#8220;SELECT Name FROM &#8216;LDAP:\/\/DC=fabrikam,DC=com&#8217; WHERE objectClass=&#8217;computer&#8217; &#8221; &amp; _\n        &#8220;and operatingSystemVersion = &#8216;5.2 (3790)'&#8221;  \nobjCommand.Properties(&#8220;Page Size&#8221;) = 1000\nobjCommand.Properties(&#8220;Searchscope&#8221;) = ADS_SCOPE_SUBTREE \nSet objRecordSet = objCommand.Execute\nobjRecordSet.MoveFirst<\/p>\n<p>Do Until objRecordSet.EOF\n    Wscript.Echo &#8220;Computer Name: &#8221; &amp; objRecordSet.Fields(&#8220;Name&#8221;).Value\n    objRecordSet.MoveNext\nLoop\n<\/PRE>\n<P>As with all Active Directory search scripts, the key to this script lies in the SQL query. In this query, we\u2019re looking for all the computers (<B>objectClass=&#8217;computer\u2019<\/B>) where the operating system version happens to be Windows Server 2003 (<B>operatingSystemVersion = &#8216;5.2 (3790)&#8217;<\/B>). We\u2019re simply returning the names of all computers where the operatingSystemVersion attribute has a value of 5.2 (3790); that happens to be the way the version number for Windows Server 2003 is stored in Active Directory.<\/P>\n<P>So how did we <I>know<\/I> that the version number for Windows Server 2003 is stored in Active Directory as 5.2 (3790)? Hey, come on, we\u2019re the Scripting Guys; we know <I>everything<\/I>!<\/P>\n<P>Well, OK: we opened up Active Directory Users and Computers and found a computer that we knew was running Windows Server 2003. We right-clicked the computer name, clicked <B>Properties<\/B>, and then looked on the <B>Operating System<\/B> tab to find the operating system version.<\/P>\n<P>We can also use a script to grab <I>all<\/I> the operating systems and their version numbers from Active Directory. Unfortunately, Active Directory doesn\u2019t support SQL\u2019s <B>SELECT DISTINCT<\/B> query type, so we can\u2019t pull out <I>just<\/I> the 2 or 3 or 4 operating systems in use; instead, we have to grab all the computers and their operating systems, and then look through the list to pull out the unique versions. But this might be a good exercise for you: see if you can find a way to put some sort of filtering into this script so it shows only the unique (distinct) operating system versions. (One hint: You might consider creating a Dictionary object and storing the versions there. That way, you could easily check to see if any one version is already in the Dictionary. If it is, then don\u2019t add a duplicate entry.)<\/P>\n<P>Oh, right; here\u2019s the script:<\/P><PRE class=\"codeSample\">Const ADS_SCOPE_SUBTREE = 2<\/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;<\/p>\n<p>Set objCommand.ActiveConnection = objConnection\nobjCommand.CommandText = _\n    &#8220;Select operatingSystem, operatingSystemVersion from &#8221; &amp; _\n        &#8220;&#8216;LDAP:\/\/DC=fabrikam,DC=com&#8217; where objectClass=&#8217;computer&#8217; ORDER BY operatingSystem&#8221;  \nobjCommand.Properties(&#8220;Page Size&#8221;) = 1000\nobjCommand.Properties(&#8220;Searchscope&#8221;) = ADS_SCOPE_SUBTREE \nSet objRecordSet = objCommand.Execute\nobjRecordSet.MoveFirst<\/p>\n<p>Do Until objRecordSet.EOF\n    Wscript.Echo objRecordSet.Fields(&#8220;operatingSystem&#8221;).Value, _\n        objRecordSet.Fields(&#8220;operatingSystemVersion&#8221;).Value\n    objRecordSet.MoveNext\nLoop\n<\/PRE><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I gather the names of all the computers in my domain that are running Windows Server 2003?&#8212; AS Hey, AS. When we choose questions to answer for this column, we try to take questions from different areas of scripting. That does two things for us: it keeps us from being [&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,8,5],"class_list":["post-70523","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-active-directory","tag-scripting-guy","tag-searching-active-directory","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I gather the names of all the computers in my domain that are running Windows Server 2003?&#8212; AS Hey, AS. When we choose questions to answer for this column, we try to take questions from different areas of scripting. That does two things for us: it keeps us from being [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70523","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=70523"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70523\/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=70523"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=70523"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=70523"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}