Solved

Problems getting NetworkInterface information using WMI with VB.NET.

Posted on 2006-07-14
5
1,805 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

739 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