Solved

Problems getting NetworkInterface information using WMI with VB.NET.

Posted on 2006-07-14
5
1,788 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

763 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now