65.9K
CodeProject 正在变化。 阅读更多。
Home

使用 WMI 获取远程计算机详细信息

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.90/5 (6投票s)

2011 年 11 月 21 日

CPOL

2分钟阅读

viewsIcon

69688

downloadIcon

8046

用于在利用远程桌面连接之前检查已登录用户的实用工具。

引言

您需要知道是否在连接远程桌面连接之前,有其他用户已登录到远程计算机吗?

您是否有足够的磁盘空间来安装补丁?

此实用工具可以回答这些问题,并使用 WMI 查询基本系统信息(系统名称和制造商、总系统内存、已登录用户、操作系统详细信息、网络详细信息、计算机类型、驱动器详细信息和 BIOS 详细信息)。

ComputerDetails.JPG

背景

我需要这样的工具来支持工作域中的远程计算机。在远程连接到他们的系统以安装软件/补丁或解决问题之前,我必须确保没有其他用户当前正在使用该计算机(或仍然登录)。

Using the Code

首先,ping 本地或远程系统。

' 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

检查您是否有权获取 WMI 信息。

' 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()

获取 WMI 信息(请参阅完整的源代码)。

配置选项可以通过帘式下拉屏幕访问(面板高度增加以打开面板)。请阅读上面的 *Computer_Details_Article.txt* 文件,了解我是如何制作此下拉窗口的。

ConfigurationOptions.JPG

' 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

选定的选项可以写入注册表,然后在下次启动应用程序时读取。

关注点

您可以使用此实用工具在本地计算机上获取系统信息,但您必须右键单击应用程序并选择“以管理员身份运行”(使用具有远程计算机上管理员权限的用户帐户),才能从远程系统获取信息。

以下是已编译应用程序 *Computer_Details.exe* 的校验和(**验证**文件完整性后再执行,或者重新编译以确保安全)。

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

历史

  • 版本 1.0 - 发布于 2011 年 11 月 20 日。
© . All rights reserved.