Click here to Skip to main content
15,881,715 members
Articles / Programming Languages / Visual Basic 10

Use WMI to obtain remote computer details

Rate me:
Please Sign up or sign in to vote.
4.90/5 (6 votes)
21 Nov 2011CPOL2 min read 68.3K   8K   30   8
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)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralWonderfull Pin
erol200730-Oct-23 21:50
erol200730-Oct-23 21:50 
QuestionThanks Pin
BLOCKCIRCUIT6-Apr-17 21:40
BLOCKCIRCUIT6-Apr-17 21:40 
QuestionQuestion. Pin
Member 1292874530-Dec-16 9:35
Member 1292874530-Dec-16 9:35 
QuestionPerfect Work Pin
aymancoo22-Sep-16 2:24
aymancoo22-Sep-16 2:24 
GeneralExcellent Pin
Rajesh Thampi29-Dec-14 19:09
Rajesh Thampi29-Dec-14 19:09 
QuestionProgram returning Full username Pin
anoble128-Mar-14 2:13
anoble128-Mar-14 2:13 
BugIssues with remote username Pin
Ajoypi22-Aug-12 17:18
Ajoypi22-Aug-12 17:18 
QuestionHow Pin
Member 270627628-Nov-11 10:01
Member 270627628-Nov-11 10:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.