Get Link Speed - Win32_PerfRawData_Tcpip_NetworkInterface

佐手、 提交于 2019-11-30 10:03:28

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

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

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'
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!