[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 732
  • Last Modified:

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

0
tybarton
Asked:
tybarton
  • 4
  • 3
1 Solution
 
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
 
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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:
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
 
tybartonAuthor Commented:
Solved myself please take back points.
0

Featured Post

Industry Leaders: 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!

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now