Win32 classes can be used as shown in the simple example below to get the hardware and software specs of a computer:
Private Sub GetOperatingSystemDetails()
Dim mos As ManagementObjectSearcher = _
New ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem")
For Each mo As ManagementObject In mos.Get
Dim OsName As String = mo("name")
Dim Version As String = mo("version")
Dim SerialNumber As String = mo("serialNumber")
Next
End Sub
In the above example, we only used one of the Win32 classes (Win32_OpratingSystem
) and for that, we only read three properties out of more than 60.
To use this example code, you need to add a reference to System.Management
in your project and import that.
In a previous post, A VB.NET program to list all computers in your domain, I showed how you can get the list of computers in your domain. You can combine traversing through computers in your domain as shown in that post with the use of Win32 classes to get the details of those computers. To run Win32 related commands on remote computers, you need to have the required privilege. In addition to that, those computers also need to have RPC and WMI services running on them.
The following example shows how to read the above three values we had for the first example from another computer in your domain:
Public Sub GetOperatingSystemDetails (ByVal computer As String)
Dim options As ConnectionOptions = New ConnectionOptions()
options.Username = "USERNAME"
options.Password = "PASSWORD"
options.Authority = "ntlmdomain:Domain"
Dim scope As ManagementScope = _
New ManagementScope("\\" & computer & "\root\cimv2", options)
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")
Dim Version As String = mo("version")
Dim SerialNumber As String = mo("serialNumber")
Next
End Sub