6

I found Determining the network connection link speed and now I am trying to correlate the data from Win32_PerfRawData_Tcpip_NetworkInterface with Win32_NetworkAdapter (or Win32_NetworkAdapterConfiguration).

On the class Win32_PerfRawData_Tcpip_NetworkInterface I don't see any index or unique key that I can use to reference Win32_NetworkAdapterConfiguration or Win32_NetworkAdapter. I tried to use theNameinWin32_PerfRawData_Tcpip_NetworkInterface andWin32_NetworkAdapter`, but still they look different.

e.g.

Name: Intel(R) PRO/1000 PL Network Connection

vs

Name: Intel[R] PRO_1000 PL Network Connection

Any hints?

Thank you in advance,

Milde

===

Maybe that piece of code will help you to help me guys :)

# I got the DeviceID of a NIC and use it to get the "NetConnection ID":

$objWMIService = Win32::OLE->GetObject("winmgmts:\\\\$computer\\root\\CIMV2") or die "Exit: WMI connection failed. \n";
$colNicSetts = $objWMIService->ExecQuery(
              "SELECT * FROM Win32_NetworkAdapter Where DeviceID = '$ID'", "WQL", wbemFlagReturnImmediately | wbemFlagForwardOnly);

foreach my $objItem (in $colNicSetts){
    $NetConnID = $objItem->{NetConnectionID};    
}

# => $NetConnID stores "Intel(R) PRO/1000 PL Network Connection".
# Now I tried to get the Link Speed with sth. like that:

$collItems = $objWMIService->ExecQuery(
             "SELECT * FROM Win32_PerfRawData_Tcpip_NetworkInterface Where Name = '$NetConnID'", "WQL", wbemFlagReturnImmediately | wbemFlagForwardOnly);
foreach my $objItem (in $collItems){
    $LinkSpeed = $objItem->{CurrentBandwidth};
}
# "Win32_PerfRawData_Tcpip_NetworkInterface" contains "Intel[R] PRO_1000 PL Network" Connection
# "Intel(R) PRO/1000 PL Network Connection" != Intel[R] PRO_1000 PL Network Connection
# => $LinkSpeed empty
Community
  • 1
  • 1
Milde
  • 2,144
  • 3
  • 17
  • 15
  • Can you post a short Perl script that shows how you get the information? As it is, there is very little useful information in your post in terms of helping others who are not working on the same problem get started. You do not even tell us which adapter string is coming from which source etc. You could try normalizing the strings by converting all non-word characters to `_`, but that would be a last resort. – Sinan Ünür Dec 02 '09 at 13:33
  • Added some code, hope it helps. I thought about wildcards too, but it seems a bit unsecure for me. – Milde Dec 02 '09 at 14:26
  • I believe, it's a little bit late... but there are some notes about naming convention at http://msdn.microsoft.com/en-us/library/system.diagnostics.performancecounter.instancename(v=vs.110).aspx - "If the instance name is automatically generated and might contain the characters "(", ")", "#", "\", or "/", use the character mapping in the following table." However this won't help mapping crazy names like isatap.{GUID} or index suffixes. – mikalai Sep 30 '14 at 21:43

3 Answers3

4

OK. Thanks for posting the short script. While you were working on that, I was following a different track using DBD::WMI and digging through the docs to see if you had missed anything.

I could not find a better way (there must be one) than canonicalizing the names:

#!/usr/bin/perl

use strict; use warnings;

use DBI;
use Data::Dumper;

my $computer = '.';
($computer) = @ARGV if @ARGV;

my $dbh = DBI->connect("dbi:WMI:$computer", undef, undef,
    { RaiseError => 1},
);

print "=== From Win32_NetworkAdapter ===\n";

my $name = $dbh->selectall_arrayref(
    'SELECT * FROM Win32_NetworkAdapter WHERE DeviceID = 11'
)->[0]->[0]->{Name};

(my $canonname = $name) =~ s/[^A-Za-z0-9]/_/g;

print "Name: $name\nCanonical name: $canonname\n\n";

my $sth = $dbh->prepare(
    'SELECT * FROM Win32_PerfRawData_Tcpip_NetworkInterface'
);

$sth->execute;

print "=== From Win32_PerfRawData_Tcpip_NetworkInterface ===\n";

while (defined (my $adapter = $sth->fetchrow_arrayref )) {
    my $conf = $adapter->[0];
    my $perfname = $conf->{Name};
    (my $canonperfname = $perfname) =~ s/[^A-Za-z0-9]/_/g;
    if ( $canonperfname =~ /^$canonname/ ) {
        print "Name: $perfname\nCanonical name: $canonperfname\n";
        print $conf->{CurrentBandwidth}, "\n\n";
        last;
    }
}

Output:

=== From Win32_NetworkAdapter ===
Name: Intel(R) PRO/Wireless 3945ABG Network Connection
Canonical name: Intel_R__PRO_Wireless_3945ABG_Network_Connection

=== From Win32_PerfRawData_Tcpip_NetworkInterface ===
Name: Intel[R] PRO_Wireless 3945ABG Network Connection - Packet Scheduler Miniport
Canonical name: Intel_R__PRO_Wireless_3945ABG_Network_Connection___Packet_Scheduler_Miniport
54000000
Sinan Ünür
  • 116,958
  • 15
  • 196
  • 339
  • 1
    @Milde I know, there **must** be a better way. – Sinan Ünür Dec 02 '09 at 15:09
  • 1
    thank you Sinan. Your code will work as long as ($canonperfname =~ /^$canonname/) is true :) . Unfortunately, I wont be able to use "DBD::WMI", but that's not the problem. This "simple" task drives me crazy... Hoping for more suggestions. – Milde Dec 02 '09 at 15:11
  • @Milde Looking at the pages returned by http://www.google.com/search?q=Win32_PerfRawData_Tcpip_NetworkInterface I see that this is a commonly recurring issue and *some* kind of canonicalization of the name seems to be the only solution. You could use some character that is unlikely to occur as part of the name such as `chr(254)` instead of `_` when canonicalizing and strip off the ` - Packet Scheduler Miniport` so that you can use `eq`. – Sinan Ünür Dec 02 '09 at 15:25
  • The `DBD::WMI` part is not necessary. I just used it to get off the ground because I wanted to minimize the amount of docs I needed to lookup (your sample code was not available when I started working on this). – Sinan Ünür Dec 02 '09 at 15:26
  • 1
    @Sinan: I recalculated your reputation, as requested. It actually went up a few points! Don't see that often. :) – Bill the Lizard Dec 02 '09 at 16:29
  • @Bill the Lizard: Thanks. I had thought it would have gone down as well. – Sinan Ünür Dec 02 '09 at 16:31
  • I tryed that as well but it only works for certain interfaces. Most of them doesn't seem to have a pattern, for istance I have *Intel[R] PRO_1000 MT Network Connection _2 - Network Load Balancing Filter Device* and *Intel(R) PRO/1000 MT Network Connection*. I can't figure out a better solution however. This is driving me crazy... – raz3r Feb 14 '12 at 10:09
3

I just looked to my machine withe the WMI-Tools, because I thought, it must be easy ... ;-)
but it's not ...

But what I found on my machine was, the a concatenantion of the "Win32_NetworkAdapter.Name" + " __" + "Win32_NetworkAdapter.InterfaceIndex" results in the "Win32_PerfFormattedData_Tcpip_NetworkInterface.Name="NVIDIA nForce Networking Controller _2" [Regard the space too!].

Example from my machine:

Win32_NetworkAdapter.DeviceID="13"
Win32_NetworkAdapter.NetConnectionID="Local Area Connection 2"
Win32_NetworkAdapter.InterfaceIndex="2"
Win32_NetworkAdapter.Name="NVIDIA nForce Networking Controller"
Win32_PerfFormattedData_Tcpip_NetworkInterface="NVIDIA nForce Networking Controller _2"

I hope, I've understood your question right and this may help?!

br--mabra

mabra
  • 383
  • 5
  • 10
1

The only approach I was able to find was to use the Win32_PnPEntity class to get the DeviceName for the network adapter, then convert it into the InstanceName. This allows you to find a key value that you can use on other WMI tables (I used InterfaceIndex, but there are other choices in the Win32_NetworkAdapter class.

So at a high level:

  1. Get an instance of Win32_NetworkAdapter
  2. Use one of the two below WMI WQL queries to get the PnpEntity
  3. Convert the Win32_PNPEntity.Name into the InstanceName by replacing:
    1. ( with [
    2. ) with ]
    3. # \ / all with _
  4. Use that InstanceName to query the Win32_PerfFormattedData_Tcpip_NetworkInterface class

It is pretty convoluted, but since InstanceName is derrived from the PnPEntity Name, that is the only way I could find to get accurate mappings.

Here are the two ways I was able to get the PnPEntity instance for a NetworkAdapter:

ASSOCIATORS OF {Win32_NetworkAdapter.DeviceID='12'} WHERE ResultClass=Win32_PnPEntity

SELECT * FROM Win32_PNPEntity where DeviceID='PCI\\VEN_14E4&DEV_1684&SUBSYS_1309103C&REV_10\\4&11050A08&0&00E5'
Greg Bray
  • 14,929
  • 12
  • 80
  • 104