Write hardware information to dataset with vb.net

Good afternoon all. Once again I had a difficult evening with my 17 year old. It didn’t even take 24 hours after being told she would lose driving, job and such privileges if she failed to follow rules that had clearly been set down. We were up until 1:30AM dealing with this. Top it off with what the Cubs did to the Brewers last night and ….. YUCK!

Anyway, we want to write various hardware information to a dataset. I chose to use WMI. It isn’t perfect but it works.

Make it a great day! And here’s to a better night!


Join me on Facebook

Imports Microsoft.VisualBasic
Imports System.Management

Public Class WMIClasses
        Public Const ComputerSystemInfo As String = "Win32_ComputerSystem"
        Public Const CDRomInfo As String = "Win32_CDRomdrive"
        Public Const VideoCardInfo As String = "Win32_VideoController"
        Public Const OperatingSystem As String = "Win32_OperatingSystem"
        Public Const EventLog As String = "Win32_NTLogEvent"
        Public Const DiskDrive As String = "Win32_DiskDrive"
        Public Const Processor As String = "Win32_Processor"
        Public Const LogicalDisk As String = "Win32_LogicalDisk"
        Public Const PrinterInfo As String = "Win32_Printer"
        Public Const WindowsActivation As String = "Win32_WindowsProductActivation"
        Public Const W1394ControllerDevice As String = "Win32_1394ControllerDevice"
        Public Const AccountClass As String = "Win32_Account"
        Public Const BaseBoard As String = "Win32_BaseBoard"
        Public Const BaseService As String = "Win32_BaseService"
        Public Const Bios As String = "Win32_BIOS"
    End Class

Public Shared Function WMIComputer(ByVal ComputerName As String, ByVal SelectClass As String) As Data.DataTable
        ‘Going to return a dataTable to add to the DataSet
        Dim retTable As New Data.DataTable(SelectClass)
        ‘Section 1: This section establishes a connection to the remote computer on the network
        Dim options As New ConnectionOptions
        Dim scope As ManagementScope
        Dim queryCollection As ManagementObjectCollection
        Dim ManageObject As ManagementObject
        Dim e As PropertyDataCollection.PropertyDataEnumerator
        Dim TableInitialized As Boolean = False
        Try
            scope = New ManagementScope("\\" & ComputerName & "\root\cimv2", options)
            scope.Connect()
        Catch e1 As Exception
            ‘Dont Care
        End Try
       
        Dim tempRow As Data.DataRow
        ‘Section 2 deals with the Query of the selected class
        Dim query As New ObjectQuery("SELECT * FROM " & SelectClass)
        Dim searcher As New ManagementObjectSearcher(scope, query)
        queryCollection = searcher.Get()
        For Each ManageObject In queryCollection

            ‘This is where the table columns get initialized for a typed dataset
            If Not TableInitialized Then
                e = ManageObject.Properties.GetEnumerator()
                While e.MoveNext
                    retTable.Columns.Add(e.Current.Name)
                End While
                TableInitialized = True
            End If
            tempRow = retTable.NewRow ‘Initialize a new row
            e = ManageObject.Properties.GetEnumerator()
            While e.MoveNext
                Try
                    tempRow.Item(e.Current.Name) = ManageObject(e.Current.Name)
                Catch ex As Exception
                 End Try

            End While
            retTable.Rows.Add(tempRow)
        Next
        Return retTable
    End Function

Private Function PageInit() As Data.DataSet
        retDS.Tables.Add(WMI.WMIComputer(System.Environment.MachineName, WMI.WMIClasses.ComputerSystemInfo))
        retDS.Tables.Add(WMI.WMIComputer(System.Environment.MachineName, WMI.WMIClasses.OperatingSystem))
        ‘retDS.Tables.Add(WMI.WMIComputer(System.Environment.MachineName, WMI.WMIClasses.EventLog))
        retDS.Tables.Add(WMI.WMIComputer(System.Environment.MachineName, WMI.WMIClasses.LogicalDisk))
        retDS.Tables.Add(WMI.WMIComputer(System.Environment.MachineName, WMI.WMIClasses.Processor))
        retDS.Tables.Add(WMI.WMIComputer(System.Environment.MachineName, WMI.WMIClasses.VideoCardInfo))
        retDS.Tables.Add(WMI.WMIComputer(System.Environment.MachineName, WMI.WMIClasses.PrinterInfo))
        retDS.Tables.Add(WMI.WMIComputer(System.Environment.MachineName, WMI.WMIClasses.WindowsActivation))
        retDS.Tables.Add(WMI.WMIComputer(System.Environment.MachineName, WMI.WMIClasses.W1394ControllerDevice))
        retDS.Tables.Add(WMI.WMIComputer(System.Environment.MachineName, WMI.WMIClasses.BaseBoard))
        retDS.Tables.Add(WMI.WMIComputer(System.Environment.MachineName, WMI.WMIClasses.BaseService))
        retDS.Tables.Add(WMI.WMIComputer(System.Environment.MachineName, WMI.WMIClasses.BIOS))
        Return retDS
    End Function

Consulting Requests

Feedback

Blog Front Page

Windows Live Tags: vb.net,.NET,WMI,hardware,information

 

Advertisements
  1. #1 by John on July 30, 2008 - 12:06 pm

    I will have to do some performance testing with this against vbscript and Powershell.

  2. #2 by Kelly on July 30, 2008 - 12:10 pm

    it would not surprise me at all to find out scripting is faster. I have bigger plans for what I am showing than what is here but there is a reason I was writing it to a dataset which is to eventually do some things iwth the data I could not do through scripting itself.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: