Solved

Problems getting NetworkInterface information using WMI with VB.NET.

Posted on 2006-07-14
5
1,802 Views
Last Modified: 2012-06-27
I am successfully getting logical disk, memory, processor, etc information using WMI on a Windows XP SP2 computer. I am also getting the server uptime using:
strComputer = "."

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colOperatingSystems = objWMIService.ExecQuery _
    ("Select * From Win32_PerfFormattedData_PerfOS_System")
 
For Each objOS in colOperatingSystems
    intSystemUptime = Int(objOS.SystemUpTime / 60)
    Wscript.Echo intSystemUptime & " minutes"
Next

but when I use

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_PerfRawData_Tcpip_NetworkInterface",,48)
For Each objItem in colItems
    Wscript.Echo "BytesReceivedPersec: " & objItem.BytesReceivedPersec
    Wscript.Echo "BytesSentPersec: " & objItem.BytesSentPersec
    Wscript.Echo "BytesTotalPersec: " & objItem.BytesTotalPersec
    Wscript.Echo "Name: " & objItem.Name
Next

it fails... I must be missing something in my application.. Any idea on why the Win32_PerfRawData_Tcpip_NetworkInterface request would fail?
0
Comment
Question by:Sc0ttRB
  • 3
  • 2
5 Comments
 
LVL 41

Expert Comment

by:graye
ID: 17110310
No, that looks right...

I suspect that your network card doesn't support the type of instrumentation that's required by WMI.

BTW: BytesTotalPerSec is a 64-bit integer...
0
 
LVL 2

Author Comment

by:Sc0ttRB
ID: 17110802
If I use Microsofts WMI Code Creator on that same machine and run a query against the Win32_PerfRawData_Tcpip_NetworkInterface I get results for the built in LAN card, a Wireless G card, a built in Wireless B and the Loopback. So the cards all support the WMI.

I am not the best programmer. Could I be missing Classes or something along those lines. I added a reference to Systems.Management and added the line Imports Systems.Management to my code.

0
 
LVL 2

Author Comment

by:Sc0ttRB
ID: 17110995
Ok.. going back to WMI Code Creator I noticed I was using VB Script and not VB.NET if I change the coding to :

Try
                Dim connection As New ConnectionOptions
                connection.Username = userNameBox.Text
                connection.Password = passwordBox.Text
                connection.Authority = "ntlmdomain:DOMAIN"

                Dim scope As New ManagementScope( _
                    "\\FullComputerName\root\CIMV2", connection)
                scope.Connect()

                Dim query As New ObjectQuery( _
                    "SELECT * FROM Win32_PerfFormattedData_Tcpip_NetworkInterface")

                Dim searcher As New ManagementObjectSearcher(scope, query)

                For Each queryObj As ManagementObject in searcher.Get()

                    Console.WriteLine("-----------------------------------")
                    Console.WriteLine("Win32_PerfFormattedData_Tcpip_NetworkInterface instance")
                    Console.WriteLine("-----------------------------------")
                    Console.WriteLine("Name: {0}", queryObj("Name"))
                Next

                Close()
            Catch err As ManagementException
                MessageBox.Show("An error occurred while querying for WMI data: " & err.Message)
            Catch unauthorizedErr As System.UnauthorizedAccessException

                MessageBox.Show("Connection error (user name or password might be incorrect): " & unauthorizedErr.Message)
            End Try

Should that work? ALso if the above is correct how to set it to access local computer and not remote (possibly nondomain pc)
0
 
LVL 41

Accepted Solution

by:
graye earned 500 total points
ID: 17111070
That looks right to me...

To get information for the local PC, just change the "FullComputerName" to "." (just like the code creator does!)

So what do you get when you run the VB.Net application?
0
 
LVL 2

Author Comment

by:Sc0ttRB
ID: 17111230
I changed my code to use the VB.NET instead of the VB Script and success...  I want to get this data for example 5 times over 5 minutes and then average the data. How can I loop the script with a 60 sec pause before each iteration?
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question