{"id":71443,"date":"2004-09-14T13:34:00","date_gmt":"2004-09-14T13:34:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2004\/09\/14\/why-doesnt-my-ping-script-run-on-windows-2000-computers\/"},"modified":"2004-09-14T13:34:00","modified_gmt":"2004-09-14T13:34:00","slug":"why-doesnt-my-ping-script-run-on-windows-2000-computers","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/why-doesnt-my-ping-script-run-on-windows-2000-computers\/","title":{"rendered":"Why Doesn&#039;t My Ping Script Run on Windows 2000 Computers?"},"content":{"rendered":"<p><img decoding=\"async\" class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" height=\"34\" alt=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"><\/p>\n<p>Hey, Scripting Guy! I have a script that pings a computer before it tries to connect to it. The script works fine on Windows XP, but doesn\u2019t work at all on my Windows 2000 computers. Do you have any idea why?<\/p>\n<p>&#8212; AK<\/p>\n<p><img decoding=\"async\" height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\"><img decoding=\"async\" class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" height=\"34\" alt=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"><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\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" align=\"right\" border=\"0\"><\/a><\/p>\n<p>Hey, AK. As a matter of fact, we <i>do<\/i> have an idea why. Here\u2019s a simplified version of your script, which pings the computer with the IP address 192.168.1.1:<\/p>\n<pre class=\"codeSample\">strComputer = \"192.168.1.1\"\nSet objWMIService = GetObject(\"winmgmts:\\\\.\\root\\cimv2\")\nSet colItems = objWMIService.ExecQuery _\n    (\"Select * from Win32_PingStatus \" &amp; _\n        \"Where Address = '\" &amp; strComputer &amp; \"'\")\nFor Each objItem in colItems\n    If objItem.StatusCode = 0 Then\n        WScript.Echo \"Reply received.\"\n    End If\nNext\n<\/pre>\n<p>Why does this work on Windows XP but not Windows 2000? Simple: the Win32_PingStatus class is supported on Windows XP and Windows Server 2003, but not on any other versions of Windows. If you run this script on an XP machine it works; that\u2019s because XP knows what the Win32_PingStatus class is. If you run it on Windows 2000 it fails; that because Windows 2000 has no idea what you\u2019re talking about when you reference a class named Win32_PingStatus. Why, Windows 2000 has never even <i>heard<\/i> of such a thing!<\/p>\n<p>Fortunately, there\u2019s a way to work around this. After all, you don\u2019t need the Win32_PingStatus class to ping a computer; Ping.exe works just fine. In this revised script, we just run Ping.exe, capture the output, and then check to see if the word <i>Reply<\/i> is found anywhere within that output. If it is, we assume that a reply was received from the remote machine. That\u2019s not a 100% foolproof method, but it\u2019ll work most of the time:<\/p>\n<pre class=\"codeSample\">Set objShell = CreateObject(\"WScript.Shell\")\nSet objExecObject = objShell.Exec _\n    (\"%comspec% \/c ping -n 3 -w 1000 192.168.1.1\")\nDo While Not objExecObject.StdOut.AtEndOfStream\n    strText = objExecObject.StdOut.ReadAll()\n    If Instr(strText, \"Reply\") &gt; 0 Then\n        Wscript.Echo \"Reply received.\"\n    Else\n        Wscript.Echo \"No reply received.\"\n    End If\nLoop\n<\/pre>\n<p>This script uses the Windows Script Host Exec method, which was introduced with WSH 5.6. Why do you care about that? Well, that means, if you haven\u2019t done so already, you\u2019ll have to upgrade to WSH 5.6 to get this script to run on your Windows 2000 computers. For more information about downloading and installing WSH 5.6, click <a href=\"http:\/\/www.microsoft.com\/technet\/\/scriptcenter\/scrptfaq.mspx\"><b>here<\/b><\/a>. And if you\u2019d like to know a little bit more about the Exec method, check out this portion of the <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/guide\/sas_wsh_pkoy.mspx\" target=\"_blank\"><b>Microsoft Windows 2000 Scripting Guide<\/b><\/a>.<\/p>\n<p>Of course, if for some reason you can\u2019t install or don\u2019t want to install WSH 5.6 (and we\u2019ve actually encountered a few people in that position), well, even <i>then<\/i> there\u2019s no reason to despair. Here\u2019s a script that will work on WSH 2.0. It\u2019s a bit crazy, to say the least: it pings a computer, saves the output to a temporary text file, opens the text file and looks for the word <i>Reply<\/i>, deletes the file, and then reports the results. But, crazy or not, it\u2019ll still do the trick:<\/p>\n<pre class=\"codeSample\">Set objFSO = CreateObject(\"Scripting.FileSystemObject\")\nSet objShell = CreateObject(\"Wscript.Shell\")\nobjName = objFSO.GetTempName\nobjTempFile = objName\nobjShell.Run \"cmd \/c ping -n 3 -w 1000 192.168.1.1 &gt;\" &amp; _\n    objTempFile, 0, True\nSet objTextFile = objFSO.OpenTextFile(objTempFile, 1)\nDo While objTextFile.AtEndOfStream &lt;&gt; True\n    strText = objTextFile.ReadLine\n    If Instr(strText, \"Reply\") &gt; 0 Then\n        Wscript.Echo \"Reply received.\"\n        Exit Do\n    End If\nLoop\nobjTextFile.Close\nobjFSO.DeleteFile(objTempFile)\n<\/pre>\n<p><br><\/p>\n<div>\n<table class=\"\" cellSpacing=\"0\" cellPadding=\"0\" width=\"100%\" border=\"0\">\n<tbody>\n<tr>\n<td class=\"\"><a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/sept04\/hey0914.mspx#top\"><img decoding=\"async\" height=\"9\" alt=\"Top of page\" src=\"http:\/\/www.microsoft.com\/technet\/mnplibrary\/templates\/MNP2.Common\/images\/arrow_px_up.gif\" width=\"7\" border=\"0\"><\/a><a class=\"topOfPage\" href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/sept04\/hey0914.mspx#top\">Top of page<\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! I have a script that pings a computer before it tries to connect to it. The script works fine on Windows XP, but doesn\u2019t work at all on my Windows 2000 computers. Do you have any idea why? &#8212; AK Hey, AK. As a matter of fact, we do have an idea [&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":[36,37,3,5],"class_list":["post-71443","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-client-side-management","tag-networking","tag-scripting-guy","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! I have a script that pings a computer before it tries to connect to it. The script works fine on Windows XP, but doesn\u2019t work at all on my Windows 2000 computers. Do you have any idea why? &#8212; AK Hey, AK. As a matter of fact, we do have an idea [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/71443","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=71443"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/71443\/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=71443"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=71443"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=71443"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}