{"id":64193,"date":"2007-08-21T01:38:00","date_gmt":"2007-08-21T01:38:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2007\/08\/21\/how-can-i-tell-if-a-wireless-network-adapter-is-connected-to-the-network\/"},"modified":"2007-08-21T01:38:00","modified_gmt":"2007-08-21T01:38:00","slug":"how-can-i-tell-if-a-wireless-network-adapter-is-connected-to-the-network","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-tell-if-a-wireless-network-adapter-is-connected-to-the-network\/","title":{"rendered":"How Can I Tell If a Wireless Network Adapter is Connected to the Network?"},"content":{"rendered":"<p><IMG 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\"> \n<P>Hey, Scripting Guy! How can I tell if a specific wireless network adapter is connected to the network?<BR><BR>&#8212; MS <\/P><IMG height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\"><IMG 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 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> \n<P>Hey, MS. You know, this is a sad day for the Scripting Guy who writes this column. After 10 days of eating Italian pastries, visiting the ruins of ancient cities, and flitting about Venice on water taxis, well, it\u2019s time to go home. In fact, even as we speak, the Scripting Family is preparing to take one final water taxi ride back to the Marco Polo Airport. And the Scripting Guy who writes this column is preparing to eat one last zeppole: fried dough topped with cinnamon and sugar.<\/P>\n<P><I>One last zeppole?<\/I> Excuse us for a moment; the Scripting Guy who writes this column is about to cry.<\/P>\n<P>Oh, well. You know what they say: all good things must come to an end. Before his Italian vacation comes to an end, however, the Scripting Guy who writes this column has vowed to do two things: eat a few more zeppoles, and show everyone a script that can tell you whether a specific wireless network adapter is connected to the network:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;<\/p>\n<p>Set objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>Set colItems = objWMIService.ExecQuery _\n    (&#8220;Select * from Win32_NetworkAdapter Where Name = &#8216;Broadcom 802.11a\/b\/g WLAN'&#8221;)<\/p>\n<p>For Each objItem in colItems\n    Select Case objItem.NetConnectionStatus\n        Case 0 strStatus = &#8220;Disconnected&#8221;\n        Case 1 strStatus = &#8220;Connecting&#8221; \n        Case 2 strStatus = &#8220;Connected&#8221; \n        Case 3 strStatus = &#8220;Disconnecting&#8221; \n        Case 4 strStatus = &#8220;Hardware not present&#8221; \n        Case 5 strStatus = &#8220;Hardware disabled&#8221; \n        Case 6 strStatus = &#8220;Hardware malfunction&#8221; \n        Case 7 strStatus = &#8220;Media disconnected&#8221; \n        Case 8 strStatus = &#8220;Authenticating&#8221; \n        Case 9 strStatus = &#8220;Authentication succeeded&#8221; \n        Case 10 strStatus = &#8220;Authentication failed&#8221; \n        Case 11 strStatus = &#8220;Invalid address&#8221; \n        Case 12 strStatus = &#8220;Credentials required&#8221;\n    End Select \n    Wscript.Echo &#8220;Net Connection Status: &#8221; &amp; strStatus\nNext\n<\/PRE>\n<P>As you can see, this isn\u2019t a particularly complicated script; as it is, most of the code involves a Select Case statement that converts return values (such as <I>0<\/I>) into more meaningful string values (such as <I>Disconnected<\/I>). Other than that, there isn\u2019t all that much to it.<\/P>\n<P>The script starts out by connecting to the WMI service on the local computer. Could we check the status of a network adapter on a remote computer? Sure, but with one caveat. We can definitely <I>attempt<\/I> to connect to the remote computer; to do that we simply assign the name of that computer to the variable strComputer:<\/P><PRE class=\"codeSample\">strComputer = &#8220;atl-fs-01&#8221;\n<\/PRE>\n<P>The only catch, of course, is that if this computer has only a single network adapter (the wireless card) and if the wireless card isn\u2019t currently connected then it goes without saying (although we\u2019ll say it anyway) that we won\u2019t be able to connect to that computer, let alone to the WMI service on that computer. At that point we have no way of knowing: 1) whether the computer is running, but not connected to the network, or 2) whether the computer isn\u2019t running at all. Most likely that distinction won\u2019t matter to you, but it\u2019s something to keep in mind, just in case.<\/P>\n<P>According to MS, their organization has standardized on a single wireless card; that makes it easy to extract information about only that particular network adapter:<\/P><PRE class=\"codeSample\">Set colItems = objWMIService.ExecQuery _\n    (&#8220;Select * from Win32_NetworkAdapter Where Name = &#8216;Broadcom 802.11a\/b\/g WLAN'&#8221;)\n<\/PRE>\n<P>As you probably already figured out for yourself, the preceding line of code retrieves information only about network adapters (instances of the <B>Win32_NetworkAdapter<\/B> class) that have the <B>Name<\/B> value <I>Broadcom 802.11a\/b\/g WLAN<\/I>. What if you use a different wireless card? That\u2019s no problem; just change the name of the card in the WQL query. For example, if your wireless card is named <I>My Wireless Adapter<\/I> then modify the WQL query to look like this:<\/P><PRE class=\"codeSample\">Set colItems = objWMIService.ExecQuery _\n    (&#8220;Select * from Win32_NetworkAdapter Where Name = &#8216;My Wireless Adapter'&#8221;)\n<\/PRE>\n<P>And what if your organization uses all make and manner of wireless adapters? Don\u2019t worry; at the end of this article we\u2019ll show you a script that returns network connection information for all the wireless adapters on a computer, regardless of their name. <\/P>\n<P>But first, the script we wrote for MS. After we issue our query we get back a recordset containing all the network adapters that have the Name <I>Broadcom 802.11a\/b\/g WLAN<\/I>. From there we set up a For Each loop to loop through this collection. Inside that loop, we use a <B>Select Case<\/B> statement to grab the value of the <B>NetConnectionStatus<\/B> property; this value will be an integer corresponding to one of the following states:<\/P>\n<TABLE class=\"dataTable\" id=\"ENE\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><B>Value<\/B><\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\"><B>State<\/B><\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">0<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">Disconnected<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">1<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">Connecting<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">2<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">Connected<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">3<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">Disconnecting<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">4<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">Hardware not present<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">5<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">Hardware disabled<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">6<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">Hardware malfunction<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">7<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">Media disconnected<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">8<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">Authenticating<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">9<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">Authentication succeeded<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">10<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">Authentication failed<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">11<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">Invalid address<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">12<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">Credentials required<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>In our Select Case statement we simply take a peek at the value of NetConnectionStatus and then assign the corresponding state to the variable strStatus. For example, if NetConnectionStatus is equal to 0 then we execute this line of code:<\/P><PRE class=\"codeSample\">Case 0 strStatus = &#8220;Disconnected&#8221;\n<\/PRE>\n<P>After that all we have to do is to echo back the connection status, using the value we assigned to the variable strStatus rather than the value of the NetConnectionStatus property:<\/P><PRE class=\"codeSample\">Wscript.Echo &#8220;Net Connection Status: &#8221; &amp; strStatus\n<\/PRE>\n<P>In turn, that means we\u2019ll see a report similar to this:<\/P><PRE class=\"codeSample\">Net Connection status: Authentication succeeded\n<\/PRE>\n<P>As for a generic script, one that returns the network connection status of <I>all<\/I> the wireless adapters on a computer, well, this following bit of code seems to do the trick:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;<\/p>\n<p>Set objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\wmi&#8221;)<\/p>\n<p>Set colItems = objWMIService.ExecQuery(&#8220;Select * From MSNdis_80211_Configuration&#8221;)<\/p>\n<p>For Each objItem in colItems<\/p>\n<p>    strName = objItem.InstanceName<\/p>\n<p>    Set objWMIService2 = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>    Set colAdapters = objWMIService2.ExecQuery _\n        (&#8220;Select * from Win32_NetworkAdapter Where Name = &#8216;&#8221; &amp; strName &amp; &#8220;&#8216;&#8221;)<\/p>\n<p>    For Each objAdapter in colAdapters\n        Select Case objAdapter.NetConnectionStatus\n            Case 0 strStatus = &#8220;Disconnected&#8221;\n            Case 1 strStatus = &#8220;Connecting&#8221; \n            Case 2 strStatus = &#8220;Connected&#8221; \n            Case 3 strStatus = &#8220;Disconnecting&#8221; \n            Case 4 strStatus = &#8220;Hardware not present&#8221; \n            Case 5 strStatus = &#8220;Hardware disabled&#8221; \n            Case 6 strStatus = &#8220;Hardware malfunction&#8221; \n            Case 7 strStatus = &#8220;Media disconnected&#8221; \n            Case 8 strStatus = &#8220;Authenticating&#8221; \n            Case 9 strStatus = &#8220;Authentication succeeded&#8221; \n            Case 10 strStatus = &#8220;Authentication failed&#8221; \n            Case 11 strStatus = &#8220;Invalid address&#8221; \n            Case 12 strStatus = &#8220;Credentials required&#8221;\n        End Select <\/p>\n<p>        Wscript.Echo strName\n        Wscript.Echo &#8220;Net Connection Status: &#8221; &amp; strStatus\n        Wscript.Echo\n    Next\nNext\n<\/PRE>\n<P>We won\u2019t discuss this in any detail today. Instead, we\u2019ll simply note that we start out by using the following line of code to bind us to the <B>root\\wmi<\/B> namespace:<\/P><PRE class=\"codeSample\">Set objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\wmi&#8221;)\n<\/PRE>\n<P>We then select all instances of the <B>MSNdis_80211_Configuration<\/B> class, a class that returns information about all the wireless adapters (including virtual adapters) installed on the computer:<\/P><PRE class=\"codeSample\">Set colItems = objWMIService.ExecQuery(&#8220;Select * From MSNdis_80211_Configuration&#8221;)\n<\/PRE>\n<P>For each wireless adapter we grab the value of the <B>InstanceName<\/B> property, store it in a variable named strName, then use this block of code to retrieve information about the actual (physical) network adapters that have that name:<\/P><PRE class=\"codeSample\">Set objWMIService2 = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>Set colAdapters = objWMIService2.ExecQuery _\n    (&#8220;Select * from Win32_NetworkAdapter Where Name = &#8216;&#8221; &amp; strName &amp; &#8220;&#8216;&#8221;)\n<\/PRE>\n<P>We\u2019ll let you try the script on your own to see what happens from there.<\/P>\n<P>Uh-oh: it looks like our water taxi is here. We\u2019ll see everyone tomorrow, when the Scripting Guy who writes this column returns to work. Is he excited about once again seeing all his colleagues from Microsoft? Let\u2019s put it this way: he\u2019s as excited about seeing them as they are about seeing him.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I tell if a specific wireless network adapter is connected to the network?&#8212; MS Hey, MS. You know, this is a sad day for the Scripting Guy who writes this column. After 10 days of eating Italian pastries, visiting the ruins of ancient cities, and flitting about Venice on water [&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":[34,332,3,5],"class_list":["post-64193","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-hardware","tag-network-adapters","tag-scripting-guy","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I tell if a specific wireless network adapter is connected to the network?&#8212; MS Hey, MS. You know, this is a sad day for the Scripting Guy who writes this column. After 10 days of eating Italian pastries, visiting the ruins of ancient cities, and flitting about Venice on water [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/64193","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=64193"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/64193\/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=64193"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=64193"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=64193"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}