{"id":71573,"date":"2004-08-25T15:11:00","date_gmt":"2004-08-25T15:11:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2004\/08\/25\/why-am-i-getting-an-error-when-trying-to-determine-an-ip-address\/"},"modified":"2004-08-25T15:11:00","modified_gmt":"2004-08-25T15:11:00","slug":"why-am-i-getting-an-error-when-trying-to-determine-an-ip-address","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/why-am-i-getting-an-error-when-trying-to-determine-an-ip-address\/","title":{"rendered":"Why am I Getting an Error when Trying to Determine an IP Address?"},"content":{"rendered":"<p><img decoding=\"async\" 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\"><\/p>\n<p>Hey, Scripting Guy! I\u2019m trying to determine the IP address on a computer, but I keep getting a <b>Type Mismatch<\/b> error. Do you know why? <\/p>\n<p>&#8212; AQ, Jacksonville, FL<\/p>\n<p><img decoding=\"async\" border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><img decoding=\"async\" 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 decoding=\"async\" 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><\/p>\n<p>Hey, AQ. As a matter of fact, we <i>do<\/i> know why you\u2019re getting a Type Mismatch error, and it\u2019s a common problem when working with the WMI class Win32_NetworkAdapterConfiguration. The line in your script that\u2019s causing the problem most likely looks something like this:<\/p>\n<pre class=\"codeSample\">Wscript.Echo \"IP Address: \" &amp; objItem.IPAddress\n<\/pre>\n<p>So what\u2019s the problem? Well, the IPAddress property is actually stored as an array; that\u2019s because it\u2019s possible for multiple IP addresses to be assigned to a single network adapter. You\u2019re treating IPAddress as though it was a string or numeric variable and it\u2019s not; an array is a totally different datatype. Hence the Type Mismatch error. This is the same kind of error you get with this script, which tries to multiply dog times cat:<\/p>\n<pre class=\"codeSample\">A = \"dog\"\nB = \"cat\"\nC = A * B\n<\/pre>\n<p>So now that we know the problem, what\u2019s the solution? It\u2019s actually pretty easy: you just need to loop through the IPAddress array and echo all the values found there. Admittedly, most of the time there will only be one IP address per network adapter, but that doesn\u2019t matter: whether there\u2019s one IP address, 100 IP addresses, or even 0 IP addresses you still need to use a For Each loop to iterate all the items in the array. A simplified version of your script would thus look like this:<\/p>\n<pre class=\"codeSample\">strComputer = \".\"\nSet objWMIService = GetObject _\n    (\"winmgmts:\\\\\" &amp; strComputer &amp; \"\\root\\cimv2\")\nSet colItems = objWMIService.ExecQuery _\n    (\"Select * From Win32_NetworkAdapterConfiguration \" &amp; _\n        \"Where IPEnabled = True\")\nFor Each objItem in colItems\n    Wscript.Echo \"Caption: \" &amp; objItem.Caption\n    For Each objAddress in objItem.IPAddress\n        Wscript.Echo \"IP Address: \" &amp; objAddress\n    Next\nNext\n<\/pre>\n<p><b>Bonus answer to a question you didn\u2019t even ask<\/b>: So how did we know that IPAddress is stored as an array? Well, the Type Mismatch error message gave us a pretty good clue, and then we verified that by opening up Wbemtest and locating the Win32_NetworkAdapterConfiguration class. As you can see from the following screenshot, IP address is, indeed, stored as an array:<\/p>\n<p><img decoding=\"async\" border=\"0\" alt=\"Hey, Scripting Guy!\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/netcard.jpg\" width=\"483\" height=\"451\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! I\u2019m trying to determine the IP address on a computer, but I keep getting a Type Mismatch error. Do you know why? &#8212; AQ, Jacksonville, FL Hey, AQ. As a matter of fact, we do know why you\u2019re getting a Type Mismatch error, and it\u2019s a common problem when working with the [&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":[3,4,5,6],"class_list":["post-71573","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-scripting-guy","tag-scripting-techniques","tag-vbscript","tag-wmi"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! I\u2019m trying to determine the IP address on a computer, but I keep getting a Type Mismatch error. Do you know why? &#8212; AQ, Jacksonville, FL Hey, AQ. As a matter of fact, we do know why you\u2019re getting a Type Mismatch error, and it\u2019s a common problem when working with the [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/71573","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=71573"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/71573\/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=71573"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=71573"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=71573"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}