Win32_PerfFormattedData_PerfOS_Memory in VB.net

I am trying to use Win32_PerfFormattedData_PerfOS_Memory wmi in VB.net and I am getting a class error. The error occurs at this line. For Each oReturn As ManagementObject In oReturnCollection


This is the code.
 
Public Shared Function GetServerMemory(ByVal PCname As String) As clsServer
        Dim returncls As New clsserver
        Try
            Dim oConn As New ConnectionOptions()

            oConn.Username = mstruser
            oConn.Password = mstrpass
            oConn.Authority = "ntlmdomain:DOMAIN"

            Dim strNameSpace As String = "\\"

            If PCname <> "" Then
                strNameSpace += PCname
            Else
                strNameSpace += "."
            End If

            strNameSpace += "\root\cimv2"

            Dim oMs As New System.Management.ManagementScope(strNameSpace, oConn)


            Dim oQuery As New System.Management.ObjectQuery("Select * From Win32_PerfFormattedData_PerfOS_Memory")


            Dim oSearcher As New ManagementObjectSearcher(oMs, oQuery)
            Dim oReturnCollection As ManagementObjectCollection = oSearcher.[Get]()

            For Each oReturn As ManagementObject In oReturnCollection
                returncls.CommitLimit = oReturn.GetPropertyValue("CommitLimit")
                returncls.CommitTotal = oReturn.GetPropertyValue("CommittedBytes")

            Next

        Catch ex As Exception
            'Write event to ErrLog.txt
            Dim oLog As New System.IO.StreamWriter("C:\LHSystemHealth\ErrLog.txt", True)
            oLog.WriteLine(PCname & " - An error occurred while querying for WMI data: Class Win32_PerfFormattedData_PerfOS_Memory: " & ex.Message & " - " & Now)

            oLog.Close()

            Return Nothing
        End Try


        Return returncls
    End Function

Open in new window


Now I use similar code to access diffrent wmi objects without issue. This code works great and I used it for the code above and just modied it for the Win32_PerfFormattedData_PerfOS_Memory

 
Private Shared Function ComputerSystem(ByVal PCname As String) As List(Of String)
        Dim lstReturn As New List(Of String)
        'Get the data related to the computer system.

        Try
            Dim oConn As New ConnectionOptions()

            oConn.Username = mstruser
            oConn.Password = mstrpass
            oConn.Authority = "ntlmdomain:DOMAIN"

            Dim strNameSpace As String = "\\"

            If PCname <> "" Then
                strNameSpace += PCname
            Else
                strNameSpace += "."
            End If

            strNameSpace += "\root\cimv2"

            Dim oMs As New System.Management.ManagementScope(strNameSpace, oConn)


            Dim oQuery As New System.Management.ObjectQuery("Select * From Win32_ComputerSystem")


            Dim oSearcher As New ManagementObjectSearcher(oMs, oQuery)
            Dim oReturnCollection As ManagementObjectCollection = oSearcher.[Get]()

            For Each oReturn As ManagementObject In oReturnCollection
                lstReturn.Add(oReturn.GetPropertyValue("Caption"))
            Next

        Catch err As Exception
            'Write event to ErrLog.txt
            Dim oLog As New System.IO.StreamWriter("C:\LHSystemHealth\ErrLog.txt", True)
            oLog.WriteLine(PCname & " - An error occurred while querying for WMI data: Class Win32_ComputerSystem: " & err.Message & " - " & Now)

            oLog.Close()
        End Try

        Return lstReturn
    End Function

Open in new window

tybartonAsked:
Who is Participating?
 
tybartonConnect With a Mentor Author Commented:
Got it figured out. You have to use Win32_PerfRawData_PerfOS_Memory. It returns the same data and has the same members and works on Windows server 2000 and above as well as XP.

Thanks CodeCruiser for your help.

Tyler
0
 
CodeCruiserCommented:
And the error is ...
0
 
tybartonAuthor Commented:
As I stated in the first line of the post I get a invalid class error. Nothin More descriptive that

Tyler
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
CodeCruiserCommented:
>As I stated in the first line of the post

You said

I am getting a class error.

Try running

winmgmt /resyncperf

http://www.vistax64.com/powershell/30302-gwmi-invalid-class-error.html
0
 
tybartonAuthor Commented:
OK figured out what the problem is. It is failing on Windows 2000 servers but runs on 2003 or higher. Anyone know what the equivalent is for 2000 servers?

Tyler
0
 
CodeCruiserCommented:
I think these perfornce classes are XP and above.
0
 
tybartonAuthor Commented:
Solved myself please take back points.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.