How Can I Tell If a Wireless Network Adapter is Connected to the Network?

ScriptingGuy1

Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I tell if a specific wireless network adapter is connected to the network?

— MS

SpacerHey, Scripting Guy! AnswerScript Center

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’s 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.

One last zeppole? Excuse us for a moment; the Scripting Guy who writes this column is about to cry.

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:

strComputer = “.”

Set objWMIService = GetObject(“winmgmts:\\” & strComputer & “\root\cimv2”)

Set colItems = objWMIService.ExecQuery _ (“Select * from Win32_NetworkAdapter Where Name = ‘Broadcom 802.11a/b/g WLAN'”)

For Each objItem in colItems Select Case objItem.NetConnectionStatus Case 0 strStatus = “Disconnected” Case 1 strStatus = “Connecting” Case 2 strStatus = “Connected” Case 3 strStatus = “Disconnecting” Case 4 strStatus = “Hardware not present” Case 5 strStatus = “Hardware disabled” Case 6 strStatus = “Hardware malfunction” Case 7 strStatus = “Media disconnected” Case 8 strStatus = “Authenticating” Case 9 strStatus = “Authentication succeeded” Case 10 strStatus = “Authentication failed” Case 11 strStatus = “Invalid address” Case 12 strStatus = “Credentials required” End Select Wscript.Echo “Net Connection Status: ” & strStatus Next

As you can see, this isn’t a particularly complicated script; as it is, most of the code involves a Select Case statement that converts return values (such as 0) into more meaningful string values (such as Disconnected). Other than that, there isn’t all that much to it.

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 attempt to connect to the remote computer; to do that we simply assign the name of that computer to the variable strComputer:

strComputer = “atl-fs-01”

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’t currently connected then it goes without saying (although we’ll say it anyway) that we won’t 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’t running at all. Most likely that distinction won’t matter to you, but it’s something to keep in mind, just in case.

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:

Set colItems = objWMIService.ExecQuery _
    (“Select * from Win32_NetworkAdapter Where Name = ‘Broadcom 802.11a/b/g WLAN'”)

As you probably already figured out for yourself, the preceding line of code retrieves information only about network adapters (instances of the Win32_NetworkAdapter class) that have the Name value Broadcom 802.11a/b/g WLAN. What if you use a different wireless card? That’s no problem; just change the name of the card in the WQL query. For example, if your wireless card is named My Wireless Adapter then modify the WQL query to look like this:

Set colItems = objWMIService.ExecQuery _
    (“Select * from Win32_NetworkAdapter Where Name = ‘My Wireless Adapter'”)

And what if your organization uses all make and manner of wireless adapters? Don’t worry; at the end of this article we’ll show you a script that returns network connection information for all the wireless adapters on a computer, regardless of their name.

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 Broadcom 802.11a/b/g WLAN. From there we set up a For Each loop to loop through this collection. Inside that loop, we use a Select Case statement to grab the value of the NetConnectionStatus property; this value will be an integer corresponding to one of the following states:

Value

State

0

Disconnected

1

Connecting

2

Connected

3

Disconnecting

4

Hardware not present

5

Hardware disabled

6

Hardware malfunction

7

Media disconnected

8

Authenticating

9

Authentication succeeded

10

Authentication failed

11

Invalid address

12

Credentials required

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:

Case 0 strStatus = “Disconnected”

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:

Wscript.Echo “Net Connection Status: ” & strStatus

In turn, that means we’ll see a report similar to this:

Net Connection status: Authentication succeeded

As for a generic script, one that returns the network connection status of all the wireless adapters on a computer, well, this following bit of code seems to do the trick:

strComputer = “.”

Set objWMIService = GetObject(“winmgmts:\\” & strComputer & “\root\wmi”)

Set colItems = objWMIService.ExecQuery(“Select * From MSNdis_80211_Configuration”)

For Each objItem in colItems

strName = objItem.InstanceName

Set objWMIService2 = GetObject(“winmgmts:\\” & strComputer & “\root\cimv2”)

Set colAdapters = objWMIService2.ExecQuery _ (“Select * from Win32_NetworkAdapter Where Name = ‘” & strName & “‘”)

For Each objAdapter in colAdapters Select Case objAdapter.NetConnectionStatus Case 0 strStatus = “Disconnected” Case 1 strStatus = “Connecting” Case 2 strStatus = “Connected” Case 3 strStatus = “Disconnecting” Case 4 strStatus = “Hardware not present” Case 5 strStatus = “Hardware disabled” Case 6 strStatus = “Hardware malfunction” Case 7 strStatus = “Media disconnected” Case 8 strStatus = “Authenticating” Case 9 strStatus = “Authentication succeeded” Case 10 strStatus = “Authentication failed” Case 11 strStatus = “Invalid address” Case 12 strStatus = “Credentials required” End Select

Wscript.Echo strName Wscript.Echo “Net Connection Status: ” & strStatus Wscript.Echo Next Next

We won’t discuss this in any detail today. Instead, we’ll simply note that we start out by using the following line of code to bind us to the root\wmi namespace:

Set objWMIService = GetObject(“winmgmts:\\” & strComputer & “\root\wmi”)

We then select all instances of the MSNdis_80211_Configuration class, a class that returns information about all the wireless adapters (including virtual adapters) installed on the computer:

Set colItems = objWMIService.ExecQuery(“Select * From MSNdis_80211_Configuration”)

For each wireless adapter we grab the value of the InstanceName 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:

Set objWMIService2 = GetObject(“winmgmts:\\” & strComputer & “\root\cimv2”)

Set colAdapters = objWMIService2.ExecQuery _ (“Select * from Win32_NetworkAdapter Where Name = ‘” & strName & “‘”)

We’ll let you try the script on your own to see what happens from there.

Uh-oh: it looks like our water taxi is here. We’ll 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’s put it this way: he’s as excited about seeing them as they are about seeing him.

0 comments

Discussion is closed.

Feedback usabilla icon