?
Solved

Problems getting NetworkInterface information using WMI with VB.NET.

Posted on 2006-07-14
5
Medium Priority
?
1,819 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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses
Course of the Month10 days, 22 hours left to enroll

770 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