Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / VB10

Use WMI to obtain remote computer details

4.90/5 (6 votes)
21 Nov 2011CPOL2 min read 69.3K   8K  
Utility to check for logged on user prior to using Remote Desktop Connection.

Introduction

Do you need to know if another user is logged on a remote computer (before connecting with Remote Desktop Connection)?

Do you have enough disk space to install patches?

This utility answers those questions and uses WMI to query basic system information (system name and manufacturer, total system memory, logged on user, OS details, networking details, type of computer, drive details, and BIOS details).

ComputerDetails.JPG

Background

I needed a tool like this to support remote computers on the domain at work. I had to ensure no other users were currently using the computer (or still logged on) before remoting into their system to install software/patches or troubleshoot an issue.

Using the Code

Start by pinging the local or remote system:

VB
' Ping system
Private Function Valid_Ping(ByVal SystemName As String)
    Dim PingReplied As Boolean = False

    Try
        Dim PingSender As New Ping
        Dim Options As New PingOptions
        ' Use default TTL of 128
        ' Change to not fragment
        Options.DontFragment = True
        ' Create 32 byte data buffer to send
        Dim PingData As String = "******Computer*****Details******"
        Dim Pingbuffer() As Byte = Encoding.ASCII.GetBytes(PingData)

        Dim PingTimeout As Integer = 120
        Dim PingReply As PingReply = PingSender.Send(SystemName, PingTimeout, Pingbuffer)
        If PingReply.Status = IPStatus.Success Then
            PingReplied = True
        Else
            PingReplied = False
        End If

        Return PingReplied
    Catch ex As Exception
        Return PingReplied
    End Try
End Function

Check if you have the correct permissions to obtain the WMI information.

VB
' Get the selected details
Private Sub Lookup_Details(ByVal SystemName As String)
    PermChar_Label.Text = String.Empty
    Me.Update()

    Try
        Dim MyConOptions As New System.Management.ConnectionOptions
        With MyConOptions
            .Impersonation = System.Management.ImpersonationLevel.Impersonate
            ' This entry required for Windows XP and newer
            .Authentication = System.Management.AuthenticationLevel.Packet
            ' Replace above code line with following code line for Windows systems prior to XP
            ' .Authentication = System.Management.AuthenticationLevel.Connect
        End With

        ' Connect to WMI namespace
        Dim MyMgtScope As System.Management.ManagementScope
        MyMgtScope = New System.Management.ManagementScope("\\" & _
                     SystemName & "\root\cimv2", MyConOptions)
        MyMgtScope.Connect()

        If MyMgtScope.IsConnected = False Then
            ' Error connecting to computer 
            PermChar_Label.ForeColor = Color.Red
            PermChar_Label.Text = "r" ' Display webdings font X
            Me.Update()
            Exit Sub
        End If

        ' Connection successful
        PermChar_Label.ForeColor = Color.Green
        PermChar_Label.Text = "a" ' Display webdings font X
        Me.Update()

Obtain the WMI information (see the full source code).

Configuration options can be accessed via a curtain type dropdown screen (the panel height is incremented to open the panel). Read the Computer_Details_Article.txt file above to see how I made this drop down window.

ConfigurationOptions.JPG

VB
' Show or hide configuration panel
Private Sub ConfigurationOptions_Button_Click(sender As System.Object, _
        e As System.EventArgs) Handles ConfigurationOptions_Button.Click
    If ConfigurationOptions_Button.Text = "Show Configuration Options" Then
        ' Send the main form groupbox behind the opening panel
        Details_GroupBox.SendToBack()
        ' Configuration panel was minimized and needs to be expanded
        While Configuration_Panel.Height < 461
            ' Run in a loop adding to the panel height until the desired open height is met
            Configuration_Panel.Size = _
               New Size(Configuration_Panel.Width, Configuration_Panel.Height + 1)
            ' Update the form display each time to display a smooth opening panel 
            Me.Update()
        End While
        ' Change the button text as it is now fully open
        ConfigurationOptions_Button.Text = "Hide Configuration Options"
    Else
        ' Configuration panel was maximized and needs to be closed
        While Configuration_Panel.Height > 22
            ' Run in a loop subtracting from the panel height until the desired closed height is met
            Configuration_Panel.Size = _
               New Size(Configuration_Panel.Width, Configuration_Panel.Height - 1)
            ' Update the form display each time to display a smooth closing panel 
            Me.Update()
        End While
        ' Change the button text as it is now fully closed
        ConfigurationOptions_Button.Text = "Show Configuration Options"

        ' If checked to remember these settings,
        ' save them to registry upon configuration panel closing
        If RememberSettings_CheckBox.Checked Then
            Dim RegVer As RegistryKey
            RegVer = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Code_Project\\Computer_Details\\1.0", True)
            If RegVer Is Nothing Then
                ' Create this entry
                Registry.CurrentUser.CreateSubKey("SOFTWARE\\Code_Project\\Computer_Details\\1.0")
            End If

            ' Save current checked options
            If (Not RegVer Is Nothing) Then
                RegVer.SetValue("MFR_Model", MfrModel_CheckBox.Checked)
                RegVer.SetValue("Memory", Memory_CheckBox.Checked)
                RegVer.SetValue("User", User_CheckBox.Checked)
                RegVer.SetValue("OS", OS_CheckBox.Checked)
                RegVer.SetValue("Network", Net_CheckBox.Checked)
                RegVer.SetValue("Case", Case_CheckBox.Checked)
                RegVer.SetValue("Drive", HD_CheckBox.Checked)
                RegVer.SetValue("BIOS", BIOS_CheckBox.Checked)
                RegVer.Close()
            End If
        End If
    End If
End Sub

Selected options can be written to the Registry then read upon starting the application next time.

Points of Interest

You can use this utility on the local computer to obtain system information, but you must right click on the application and select "Run as administrator" (using a user account that has administrator rights on the remote computer) in order to obtain information from remote systems.

Here are the check sums of the Computer_Details.exe compiled application (verify file integrity prior to executing, or recompile to be on the safe side).

                MD5                             SHA-1
-------------------------------- ----------------------------------------
dfb1db9d1fe816cd70f2bc8a3e3e1851 f2751eb2c7e2a6154e0def178bcc4d7bf8f53710

History

  • Version 1.0 - Published 20 Nov 2011.

License

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