Click here to Skip to main content
15,910,981 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am new to programming and i m working on a project to pull machine information remotly. I have been having issues with getting the machine name in the textbox i created work with the code. i m also having issues with the wmi part as well.

My over all goal is to have on button click the text from one textbox act as the computer name and pull specific wmi information. I have been trying just the os name for now.

my code:
VB
  Private Sub Search_Button_Header_Click(sender As System.Object, e As System.EventArgs) Handles Search_Button_Header.Click
      strComputer = Me.Machine_Name_Textbox_Header.Text
      If strComputer = "" Then
          strComputer = "localhost"
      End If

      Dim scope As ManagementScope = _
          New ManagementScope("\\" & strComputer & "\root\cimv2")

      scope.Connect()

      Dim query As ObjectQuery = _
          New ObjectQuery("SELECT * FROM Win32_OperatingSystem")
      Dim mos As ManagementObjectSearcher = _
          New ManagementObjectSearcher(scope, query)
      Dim queryCollection As ManagementObjectCollection = mos.Get

      For Each mo As ManagementObject In queryCollection
          Dim OsName As String = mo("name")

          OSTextBox.Text = mos.Get("OsName")
      Next

End Sub

I have searched and found others that are doing the same thing but every one of them have different ways. Any help would be great.
Posted
Updated 30-Nov-11 9:26am
v3
Comments
Sergey Alexandrovich Kryukov 30-Nov-11 15:11pm    
Please put essential information in the title of the question. You are the first one who is interested in that, as you want right people to pay attention to your post even before this page is open.

What's your problem?
--SA
Zachary.shupp 30-Nov-11 15:17pm    
my issue is when i use this code and hit the search button the coding has issues with ostextbox = mos.get("OsName"). I need help pulling the information i think im coding the wmi wrong.

1 solution

Try this code :
VB
'To use this class you must add a reference
'to System.Management from the Project | References
'menu
Imports System.Management

Public Class clsWMI
    Private objOS As ManagementObjectSearcher
    Private objCS As ManagementObjectSearcher
    Private objMgmt As ManagementObject
    Private m_strComputerName As String
    Private m_strManufacturer As String
    Private m_StrModel As String
    Private m_strOSName As String
    Private m_strOSVersion As String
    Private m_strSystemType As String
    Private m_strTPM As String
    Private m_strWindowsDir As String
    
    Public Sub New()
        objOS = New ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem")
        objCS = New ManagementObjectSearcher("SELECT * FROM Win32_ComputerSystem")
        For Each objMgmt In objOS.Get
           
            m_strOSName = objMgmt("name").ToString()
            m_strOSVersion = objMgmt("version").ToString()
            m_strComputerName = objMgmt("csname").ToString()
            m_strWindowsDir = objMgmt("windowsdirectory").ToString()
        Next
        For Each objMgmt In objCS.Get
            m_strManufacturer = objMgmt("manufacturer").ToString()
            m_StrModel = objMgmt("model").ToString()
            m_strSystemType = objMgmt("systemtype").ToString
            m_strTPM = objMgmt("totalphysicalmemory").ToString()
        Next
    End Sub
    Public ReadOnly Property ComputerName()
        Get
            ComputerName = m_strComputerName
        End Get
    End Property
    Public ReadOnly Property Manufacturer()
        Get
            Manufacturer = m_strManufacturer
        End Get
    End Property
    Public ReadOnly Property Model()
        Get
            Model = m_StrModel
        End Get
    End Property
    Public ReadOnly Property OsName()
        Get
            OsName = m_strOSName
        End Get
    End Property
    Public ReadOnly Property OSVersion()
        Get
            OSVersion = m_strOSVersion
        End Get
    End Property
    Public ReadOnly Property SystemType()
        Get
            SystemType = m_strSystemType
        End Get
    End Property
    Public ReadOnly Property TotalPhysicalMemory()
        Get
            TotalPhysicalMemory = m_strTPM
        End Get
    End Property
    Public ReadOnly Property WindowsDirectory()
        Get
            WindowsDirectory = m_strWindowsDir
        End Get
    End Property
End Class

How to use :
VB
 Dim objWMI As New clsWMI()
With objWMI
      Debug.WriteLine("Computer Name = " & .ComputerName)
      Debug.WriteLine("Computer Manufacturer = " & .Manufacturer)
      Debug.WriteLine("Computer Model = " & .Model)
      Debug.WriteLine("OS Name = " & .OsName)
      Debug.WriteLine("OS Version = " & .OSVersion)
      Debug.WriteLine("System Type = " & .SystemType)
      Debug.WriteLine("Total Physical Memory = " & .TotalPhysicalMemory)
      Debug.WriteLine("Windows Directory = " & .WindowsDirectory)
End With

I hope it will solve your problem.:)
 
Share this answer
 
Comments
fjdiewornncalwe 1-Dec-11 11:38am    
If you are going to copy/paste an answer pretty much directly from another source, that is fine, but be sure to put a link to the original source as well, otherwise you are going to get hammered for plagiarism.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900