{"id":56263,"date":"2008-02-06T22:43:00","date_gmt":"2008-02-06T22:43:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2008\/02\/06\/hey-scripting-guy-how-can-i-read-computer-names-from-a-text-file-and-then-verify-whether-or-not-those-computers-still-exist\/"},"modified":"2008-02-06T22:43:00","modified_gmt":"2008-02-06T22:43:00","slug":"hey-scripting-guy-how-can-i-read-computer-names-from-a-text-file-and-then-verify-whether-or-not-those-computers-still-exist","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-read-computer-names-from-a-text-file-and-then-verify-whether-or-not-those-computers-still-exist\/","title":{"rendered":"Hey, Scripting Guy! How Can I Read Computer Names From a Text File and Then Verify Whether or Not Those Computers Still Exist?"},"content":{"rendered":"<p><img decoding=\"async\" class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" height=\"34\" alt=\"Hey, Scripting Guy! Question\" width=\"34\" align=\"left\" border=\"0\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" \/> <\/p>\n<p>Hey, Scripting Guy! How can I read a list of computer names from a text file, and then check to see if those computers still exist in Active Directory?<\/p>\n<p>&#8212; CL<\/p>\n<p><img decoding=\"async\" height=\"5\" alt=\"Spacer\" width=\"5\" border=\"0\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" \/><img decoding=\"async\" class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" height=\"34\" alt=\"Hey, Scripting Guy! Answer\" width=\"34\" align=\"left\" border=\"0\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" \/><a href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><img decoding=\"async\" class=\"farGraphic\" title=\"Script Center\" height=\"288\" alt=\"Script Center\" width=\"120\" align=\"right\" border=\"0\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" \/><\/a> <\/p>\n<p>You know, that\u2019s a good question, CL, we \u2013 hold on a second. Some of you out there look so sad; why the long faces? After all, the <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/funzone\/games\/default.mspx\"><b>2008 Winter Scripting Games<\/b><\/a> start in less than two weeks (February 15<sup>th<\/sup>). Shouldn\u2019t you be happy and excited?<\/p>\n<p>Ah, we see. As it turns out, many of you are Perl scripters, and you\u2019re feeling a little left out. Granted, Perl <i>has<\/i> been added to the Scripting Games, but there\u2019s no doubt that many of the <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/funzone\/games\/games08\/prizes.mspx\"><b>prizes<\/b><\/a> we\u2019re giving away are aimed at Windows PowerShell and VBScript users. Dr. Scripto bobblehead dolls and copies of Windows Vista are great (and they are, by the way), but why don\u2019t Perl scripters have a chance to win some <i>Perl-related<\/i> software? Does mean that \u2013 gasp! \u2013 the Scripting Guys are prejudiced against Perl users?<\/p>\n<p>No, of course not; it just means that we haven\u2019t had a chance yet to announce a major addition to the Scripting Games prize list. As it turns out, the good people at <a target=\"_blank\" href=\"http:\/\/www.activestate.com\/\"><b>ActiveState<\/b><\/a> \u2013 people who make absolutely <i>dynamite<\/i> software, including the version of Perl that we\u2019ll use to test Scripting Games scripts \u2013 have generously donated a pair of prize packages consisting of:<\/p>\n<table cellspacing=\"0\" cellpadding=\"0\" border=\"0\">\n<tbody>\n<tr>\n<td class=\"listBullet\" valign=\"top\">\u2022<\/td>\n<td class=\"listItem\">\n<p>A copy of the Perl Developer Kit (with a retail value of $295).<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td class=\"listBullet\" valign=\"top\">\u2022<\/td>\n<td class=\"listItem\">\n<p>An ActiveState T-shirt.<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td class=\"listBullet\" valign=\"top\">\u2022<\/td>\n<td class=\"listItem\">\n<p>An ActiveState mug.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>We thought that would bring a smile to your face. These prizes would bring a smile to <i>anyone\u2019s <\/i>face. <\/p>\n<p>What\u2019s that? What about that <a target=\"_blank\" href=\"http:\/\/www.superdickery.com\/stupor\/45.html\"><b>evil villain<\/b><\/a> in the old Batman comic book who doesn\u2019t even <i>have<\/i> a face? Is he smiling, too? You know, we\u2019ll have to get back you on that. Keep in mind, however, that the Scripting Guys don\u2019t cater to evil villains. If we did, we\u2019d do more articles on Linux scripting.<\/p>\n<p>Hey, we\u2019re just <i>kidding<\/i>. <\/p>\n<p>At any rate, the Scripting Games has picked up some really great prizes from a first-rate company; ActiveState has a well-earned reputation for producing outstanding software. Now, as for today\u2019s question, we \u2013 what\u2019s that? How did we know that Perl people were feeling down? For that matter, how did we know that they all had long faces? Does this mean that each copy of Windows contains some secret Microsoft software the enables us to monitor everything you do and everything you feel?<\/p>\n<p>Well, of <i>course<\/i> it does; everyone knows that Microsoft \u2013 uh, no, of course not. We aren\u2019t secretly monitoring everything you do; that\u2019s just crazy.<\/p>\n<p>Although TW in Omaha, you really <i>should<\/i> see a doctor about your cholesterol level. You don\u2019t want to take chances with your health, you know.<\/p>\n<p>Now, where were we? Oh, that\u2019s right: today\u2019s question. How can you read a list of computer names from a text file and then check to see if those computers still exist in Active Directory? Well, here\u2019s one way:<\/p>\n<pre class=\"codeSample\">On Error Resume NextConst ADS_SCOPE_SUBTREE = 2Const ForReading = 1Set objConnection = CreateObject(\"ADODB.Connection\")Set objCommand = CreateObject(\"ADODB.Command\")objConnection.Provider = \"ADsDSOObject\"objConnection.Open \"Active Directory Provider\"Set objCommand.ActiveConnection = objConnectionobjCommand.Properties(\"Page Size\") = 1000objCommand.Properties(\"Searchscope\") = ADS_SCOPE_SUBTREE Set objFSO = CreateObject(\"Scripting.FileSystemObject\")Set objFile = objFSO.OpenTextFile(\"C:\\Scripts\\Computers.txt\")Do Until objFile.AtEndOfStream    strComputer = objFile.ReadLine    objCommand.CommandText = \"SELECT Name FROM 'LDAP:\/\/dc=fabrikam,dc=com' \" &amp; _        \"WHERE objectCategory='computer' AND Name = '\" &amp; strComputer &amp; \"'\"    Set objRecordset = objCommand.Execute    If objRecordset.RecordCount = 0 Then        Wscript.Echo strComputer &amp; \" does not exist.\"    Else        Wscript.Echo strComputer &amp; \" still exists.\"    End IfLoop<\/pre>\n<p>So how exactly does this script work? Well, to begin with, we define a pair of constants: ADS_SCOPE_SUBTREE, which we\u2019ll use to tell the script to search the entire domain; and ForReading, which we\u2019ll need when we open our text file for reading. We then use this block of code to create instances of the <b>ADODB.Connection<\/b> and the <b>ADODB.Command<\/b> objects, and prepare our script to do a search of Active Directory:<\/p>\n<pre class=\"codeSample\">Set objConnection = CreateObject(\"ADODB.Connection\")Set objCommand = CreateObject(\"ADODB.Command\")objConnection.Provider = \"ADsDSOObject\"objConnection.Open \"Active Directory Provider\"Set objCommand.ActiveConnection = objConnectionobjCommand.Properties(\"Page Size\") = 1000objCommand.Properties(\"Searchscope\") = ADS_SCOPE_SUBTREE<\/pre>\n<p>Aren\u2019t we going to explain what all this code is for? Sorry, but the answer is no; that\u2019s far more than we can explain in the daily <i>Hey, Scripting Guy!<\/i> column. But hey, don\u2019t look so down in the dumps; if you need more background information about Active Directory search scripts we have a two-part <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/tales\/sg0405.mspx\"><b>Tales From the Script<\/b><\/a> series designed to give you that very information.<\/p>\n<table class=\"dataTable\" id=\"E3F\" cellspacing=\"0\" cellpadding=\"0\">\n<thead><\/thead>\n<tbody>\n<tr class=\"record\" valign=\"top\">\n<td>\n<p class=\"lastInCell\"><b>Note<\/b>. How did we know that people were looking down in the dumps? Oh, come on; that was just an educated guess. Besides, <i>lots<\/i> of people get down in the dumps while reading this column.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"dataTableBottomMargin\"><\/div>\n<p>At this point we\u2019re ready to open our text file. To do that, we create an instance of the <b>Scripting.FileSystemObject<\/b>, then use this line of code to open the file C:\\Scripts\\Computers.txt for reading:<\/p>\n<pre class=\"codeSample\">Set objFile = objFSO.OpenTextFile(\"C:\\Scripts\\Computers.txt\")<\/pre>\n<p>Once the file is open we set up a Do loop designed to run until the file\u2019s <b>AtEndOfStream<\/b> property is True. (In other words, we\u2019re going to read the file, line-by-line, until there\u2019s nothing left to read.) Inside that loop, we read the first line in the text file and assign it to a variable named strComputer:<\/p>\n<pre class=\"codeSample\">strComputer = objFile.ReadLine<\/pre>\n<p>Some of you look like \u2013 um, some of you <i>might<\/i> be wondering why we\u2019re reading a single line from the text file. That\u2019s because we\u2019re assuming the text file looks like this, with each line representing the name of a computer:<\/p>\n<pre class=\"codeSample\">atl-fs-001atl-fs-002atl-fs-003atl-fs-004atl-fs-005<\/pre>\n<p>Do you<i><\/i><i>have<\/i> to structure your text file in this manner? No. But, trust us: it will make your life much easier if you do. Reading the file line-by-line is way easier than, say, having to parse a comma-separated values or tab-separated values file.<\/p>\n<p>That brings us to this line of code:<\/p>\n<pre class=\"codeSample\">objCommand.CommandText = \"SELECT Name FROM 'LDAP:\/\/dc=fabrikam,dc=com' \" &amp; _        \"WHERE objectCategory='computer' AND Name = '\" &amp; strComputer &amp; \"'\"<\/pre>\n<p>What we\u2019re doing here is configuring our Active Directory search query: we\u2019re simply asking to get back a recordset consisting of all the objects in the domain fabrikam.com that have an <b>objectCategory<\/b> equal to <i>computer<\/i> and a <b>Name<\/b> equal to the name we just read in from the text file. Because computer names must be unique within a domain, the recordset we get back will contain one of two possible values: 0 if no such computer can be found, or 1 if the computer name <i>can<\/i> be found.<\/p>\n<p>After configuring the query we then call the <b>Execute<\/b> method to execute the query and return the recordset:<\/p>\n<pre class=\"codeSample\">Set objRecordset = objCommand.Execute<\/pre>\n<p>The rest is easy: we simply check the number of items in the recordset (something we can do using the <b>RecordCount<\/b> property), and then echo back the appropriate message:<\/p>\n<pre class=\"codeSample\">If objRecordset.RecordCount = 0 Then    Wscript.Echo strComputer &amp; \" does not exist.\"Else    Wscript.Echo strComputer &amp; \" still exists.\"End If<\/pre>\n<p>And then it\u2019s back to the top of the loop, where we repeat the process with the next line in the text file.<\/p>\n<p>When all is said and done, we should get back a report similar to this:<\/p>\n<pre class=\"codeSample\">atl-fs-001 does not exist.atl-fs-002 still exists.atl-fs-003 still exists.atl-fs-004 does not exist.atl-fs-005 still exists.<\/pre>\n<p>That should do it, CL; we hope that helps. And we hope all of you will join us for the <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/funzone\/games\/default.mspx\"><b>2008 Winter Scripting Games<\/b><\/a>, February 15<sup>th<\/sup> through March 3<sup>rd<\/sup>. Did we mention that <a target=\"_blank\" href=\"http:\/\/www.activestate.com\/\"><b>ActiveState<\/b><\/a> has donated two really cool prize packages to the Games? Well, it\u2019s worth mentioning again. The Games are fun, the Games are challenging, and now the Games give you even more of a chance to win some really great prizes. What more could you ask for?<\/p>\n<p>We\u2019ll see you all on February 15<sup>th<\/sup>. And, needless to say, when we say \u201cWe\u2019ll see you,\u201d well, we mean it.<\/p>\n<table class=\"dataTable\" id=\"E3AAC\" cellspacing=\"0\" cellpadding=\"0\">\n<thead><\/thead>\n<tbody>\n<tr class=\"record\" valign=\"top\">\n<td>\n<p><b>Note<\/b>. That was just a joke; like we said before, we can\u2019t really see what you\u2019re doing. And, to be honest, in most cases we probably don\u2019t even <i>want<\/i> to see what you\u2019re doing.<\/p>\n<p>Hey, you in Mexico City: don\u2019t you roll your eyes at us like that!<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I read a list of computer names from a text file, and then check to see if those computers still exist in Active Directory? &#8212; CL You know, that\u2019s a good question, CL, we \u2013 hold on a second. Some of you out there look so sad; why the long [&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,9,46,3,4,14,5],"class_list":["post-56263","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-active-directory","tag-adsi","tag-computer-accounts","tag-scripting-guy","tag-scripting-techniques","tag-text-files","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I read a list of computer names from a text file, and then check to see if those computers still exist in Active Directory? &#8212; CL You know, that\u2019s a good question, CL, we \u2013 hold on a second. Some of you out there look so sad; why the long [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/56263","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=56263"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/56263\/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=56263"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=56263"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=56263"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}