pepoluan
pepoluan

Reputation: 6808

Programmatically detect connected network speed on Windows

How can I detect what kind of Ethernet connection my NIC made? That is, my NIC is a Gigabit Ethernet device, but since it's auto-sensing, it might be connected at Gigabit speed or Fast Ethernet speed.

How do I find out what speed it is being connected at?

I tried using WMI's Win32_NetworkAdapter, CIM_NetworkAdapter, even wmic NET get Name,Speed, but all of them return blank for Speed.

I'm using Windows XP Pro SP3, by the way. But I'd like a solution that will also work for Windows Server 2003 (Standard & Enterprise), Windows Server 2008, and Windows Server 2008 R2.

Upvotes: 3

Views: 4167

Answers (1)

Lizz
Lizz

Reputation: 1470

A mysterious person codenamed 'mystifeid' solved this puzzle on this site, third post down: http://social.msdn.microsoft.com/Forums/uk-UA/scripting/thread/e3936dff-7395-4a6a-ab35-aa1aab0bcd71

Here's their lovely code:

Dim strQuery, strQuery2, objLocator, objWMI, objItem, objItem2, colItems, colItems2, resultString, nicName
strQuery = "SELECT * FROM Win32_PerfFormattedData_Tcpip_NetworkInterface"
strQuery2 = "SELECT * FROM Win32_NetworkAdapter"
Set objLocator = CreateObject( "WbemScripting.SWbemLocator" )
Set objWMI = objLocator.ConnectServer( ".", "root\CIMV2" )
objWMI.Security_.ImpersonationLevel = 3
Set colItems2 = objWMI.ExecQuery( strQuery2, "WQL", 0 )
Set colItems = objWMI.ExecQuery( strQuery, "WQL", 0 )
resultString = ""
For Each objItem2 In colItems2
 If objItem2.NetConnectionStatus = 2 Then
  nicName = Mid(objItem2.Name, 1, 5)
  resultString = resultString & "Adapter Name : " & objItem2.Name & VbCrLf
  resultString = resultString & "MAC Address : " & objItem2.MACAddress & VbCrLf
  For Each objItem In colItems
   If Mid(objItem.Name, 1, 5) = nicName Then
    resultString = resultString & "CurrentBandwidth : " & objItem.CurrentBandwidth & VbCrLf
   End If
  Next
 End If
Next
Set objLocator = Nothing
Set objWMI = Nothing
Set colItems = Nothing
Set colItems2 = Nothing
WScript.Echo resultString

PS- I'll vet it against Win7 and Win2008 soon and let all know.

Upvotes: 1

Related Questions