How Can I Determine the Signal Strength of a Wireless Connection?

ScriptingGuy1

Hey, Scripting Guy! Question

Hey, Scripting Guy! Any time Iā€™m connected to a wireless network I can open the Wireless Network Connection Status dialog box and see a series of bars ā€“ 1 through 5 ā€“ that indicates the signal strength. How can I get that same information using a script?

— RK

SpacerHey, Scripting Guy! AnswerScript Center

Hey, RK. Before we answer todayā€™s question we need to make one slight clarification. For the past week or so, the Scripting Guy who writes this column has noted that, thanks to the NCAA menā€™s basketball tournament, this is his favorite time of the year. As it turns out, he was mistaken. Instead, thanks to the Girl Scouts of America, this is his least favorite time of the year.

Not that the Scripting Guy who writes this column has anything against the Girl Scouts, mind you; after all, the Girl Scouts of America is a fine, upstanding organization that does a lot of good things for both its members and for the community. Itā€™s just that this is the time of year when the Girl Scouts hold their annual cookie sale. And that can only mean one thing: every penny that the Scripting Guy who writes this column has (along with a number of pennies that he doesnā€™t have) is going to end up being spent on Girl Scout cookies he doesnā€™t want, doesnā€™t need, and doesnā€™t even take with him. (The Girl Scouts give you the option of donating the cookies to troops serving overseas.)

The Scripting Guy who writes this column is always a sucker for kids selling things, but in years past it was a little easier; thatā€™s because the Girl Scouts only sold their cookies door-to-door. That meant that, sooner or later, a Girl Scout would show up on your doorstep, youā€™d buy a bunch of cookies, and then youā€™d be home free for the rest of the year. But now the Girl Scouts donā€™t bother with door-to-door sales; instead, they stake out the entrances to grocery stores, drug stores, shopping centers and other places that you simply canā€™t avoid. (Editorā€™s Note: Weā€™re not sure where the Scripting Guy who writes this column has been for most of his life, because the Scripting Editor distinctly remembers standing outside a bank for several hours selling Girl Scout cookies, and that wasā€¦severalā€¦years ago.)

And, truth be told, they get the Scripting Guy who writes this column every single time. On one fateful occasion this Scripting Guy entered the local grocery store through door A, buying a bunch of cookies along the way. Without even thinking he then exited the store through door B, where a different pair of Girl Scouts were selling cookies. And yes, he bought cookies from them, too. Talk about getting you coming or going.

But donā€™t blame the Scripting Guy who writes this column; instead, this is all the Girl Scoutsā€™ fault. After all, somewhere in this world thereā€™s a 17-year-old Girl Scout who spends her time smoking cigarettes, listening to her iPod, and text-messaging her friends. The Scripting Guy who writes this column might be able to walk past someone like that without giving her a second thought. But you donā€™t see girls like that selling Girl Scout cookies. No, the Girl Scouts who sell the cookies are all tiny little 7- and 8-year-olds, with huge brown eyes and tremulous little voices, and theyā€™re just so cute and so shy when they ask if youā€™d like to buy some cookies that ā€“ well, you can figure out the rest for yourself. Itā€™s not fair!

Worst of all, the Scripting Guy who writes this column needs to stop at the store on his way home tonight. (Yes, the smart thing to do would be to stock up on food and other supplies ahead of time, and thus avoid the stores during the cookie sale. But itā€™s been so long since the Scripting Guy who writes this column has done the smart thing that he no longer even knows how to go about doing something like that.) That means that this Scripting Guy is going to need plenty of money tonight, and the only way for him to make some money is to come up with a script that can report the strength of a wireless network connection. You know, something along these lines:

strComputer = “.”

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

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

For Each objItem in colItems intStrength = objItem.NDIS80211ReceivedSignalStrength

If intStrength > -57 Then strBars = “5 Bars” ElseIf intStrength > -68 Then strBars = “4 Bars” ElseIf intStrength > -72 Then strBars = “3 Bars” ElseIf intStrength > -80 Then strBars = “2 Bars” ElseIf intStrength > -90 Then strBars = “1 Bar” Else strBars = “Strength cannot be determined” End If

Wscript.Echo objItem.InstanceName & ” — ” & strBars Next

Note: Unfortunately, this script does not work on Windows Vista. How do you get this information on Windows Vista? Well, weā€™re not sure about that. But for those of you who havenā€™t upgraded yet, read on for an explanation of the pre-Windows Vista solution.

Letā€™s see if we can figure out how this script works. (After all, if we stall long enough maybe the Girl Scouts will go home and go to bed before we get to the store.) We begin by connecting to the WMI service on the local computer (although we could also run this script against a remote computer). Note, however, that with this script we donā€™t connect to the root\cimv2 namespace; instead, we make a connection to the root\wmi namespace:

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

After making the connection we then use this line of code to retrieve all the instances of the delightfully-named MSNdis_80211_ReceivedSignalStrength class:

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

Thatā€™s going to give us back a collection of all our wireless connections and their signal strengths.

Once thatā€™s done our next step is to set up a For Each loop to loop through all the items in that collection; inside that loop we use this line of code to assign the value of the NDIS80211ReceivedSignalStrength property to a variable named intStrength:

intStrength = objItem.NDIS80211ReceivedSignalStrength

This is where things start to get a tiny bit weird.

As it turns out, the NDIS80211ReceivedSignalStrength property is going to be a negative value (e.g., -55). To be perfectly honest, we ran into a lot of problems when we tried to find out exactly what that value means and how it is derived. However, we did find a couple of references that provided at least a rough approximation of how these values translate to the signal strength bars found in the Wireless Network Connection Status dialog box. Based on those suggested values (and your values might vary slightly), we decided to next use the following If-Then block to assign a value to a variable named strBars:

If intStrength > -57 Then
    strBars = “5 Bars”
ElseIf intStrength > -68 Then
    strBars = “4 Bars”
ElseIf intStrength > -72 Then
    strBars = “3 Bars”
ElseIf intStrength > -80 Then
    strBars = “2 Bars”
ElseIf intStrength > -90 Then
    strBars = “1 Bar”
Else
    strBars = “Strength cannot be determined”
End If

It should be fairly clear what weā€™re doing here: weā€™re simply taking action based on the value of the variable intStrength. For example, suppose intStrength is greater than -57. (And, of course, -55 is greater than -57; donā€™t let those negative numbers throw you off.) If intStrength is greater than -57 we assign the value 5 bars to the variable strBars and then exit the code block. If intStrength isnā€™t greater than -57 we then check to see if the value happens to be greater than -68. If it is, then strBars is assigned the value 4 bars and we then exit the code block. Etc., etc.

When weā€™re done we echo back the value of both the InstanceName property and the variable strBars, then loop around and repeat the process with the next wireless connection.

The one drawback to this script is that youā€™re likely to end up with multiple reports for a single wireless connection. For example:

Broadcom 802.11a/b/g WLAN — 5 Bars
Broadcom 802.11a/b/g WLAN – Virtual Machine Network Services Driver — 5 Bars
Broadcom 802.11a/b/g WLAN – Packet Scheduler Miniport — 5 Bars

Thatā€™s due to the fact that WMI considers both physical network adapters and virtual network adapters to be one and the same. If you wanted to, you could write code that limits the returned data to ā€œrealā€ network adapters (perhaps by first getting a list of IP-enabled adapters from the Win32_NetworkAdapterConfiguration class). We didnā€™t see any need to go to all that trouble, but if anyone would like to see an example of how you might go about doing that just let us know.

As for the Girl Scouts of America, the Scripting Guy who writes this column has decided that enough is enough: come this time next year heā€™s just going to have his paycheck direct-deposited to the Girl Scouts. That should save everyone a lot of time and a lot of trouble. And, for once at least, his money will actually be put to good use.

0 comments

Discussion is closed.

Feedback usabilla icon