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

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

  • Comments 1
  • Likes

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.

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • <p>I need to know the name SSID which a machine is conected by wifi for later run other script if the name SSID is correct. </p> <p>I have this script that work well for windows xp but not for windows 7 64 bits. </p> <p>do you know why doesn&#39;t work under windows 7? </p> <p>Private Sub GetWMI(WMIArray, WMIQuery) </p> <p>Set WMIClass = GetObject(&quot;winmgmts:{impersonationLevel=impersonate}!\\.\root\wmi&quot;) </p> <p> &nbsp;If not(WMIClass is nothing) Then </p> <p> &nbsp; Set WMIArray = WMIClass.ExecQuery(WMIQuery) </p> <p> &nbsp;end if</p> <p>End Sub </p> <p>Function SSID() </p> <p> &nbsp; &nbsp;Call GetWMI(objMSNdis_80211_ServiceSetIdentifierSet, &quot;Select * from MSNdis_80211_ServiceSetIdentifier Where active=true&quot;) </p> <p> &nbsp; For Each objMSNdis_80211_ServiceSetIdentifier in objMSNdis_80211_ServiceSetIdentifierSet </p> <p> &nbsp; &nbsp; &nbsp; ID = &quot;&quot; </p> <p> &nbsp; &nbsp; &nbsp; &nbsp;For i = 0 to objMSNdis_80211_ServiceSetIdentifier.Ndis80211SsId(0) </p> <p> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ID = ID &amp; chr(objMSNdis_80211_ServiceSetIdentifier.Ndis80211SsId(i + 4)) </p> <p> &nbsp; &nbsp; &nbsp; Next </p> <p> &nbsp; &nbsp; &nbsp; &nbsp;SSID = ID </p> <p> &nbsp; Next </p> <p>End Function </p> <p>wscript.echo SSID()</p>