Listing/Modifying Server Information with Visual Basic.NET and VBScript Using WMI - Listing minimum server information using WMI
(Page 2 of 5 )
How can you find out information about your server? By this I mean its name, domain, workgroup, and so on. The following VB.NET code should supply some minimum information on your server.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
Dim searcher As New ManagementObjectSearcher( _
"root\CIMV2", _
"SELECT * FROM Win32_ComputerSystem")
Dim en As ManagementObjectEnumerator = searcher.Get.GetEnumerator
If en.MoveNext Then
Dim queryObj As ManagementObject = en.Current
Dim dt As DataTable = globals.getStructure()
globals.addRow(dt, "Caption", queryObj
("Caption"))
globals.addRow(dt, "Name", queryObj("Name"))
globals.addRow(dt, "UserName", queryObj
("UserName"))
globals.addRow(dt, "DNSHostName", queryObj
("DNSHostName"))
globals.addRow(dt, "Description", queryObj
("Description"))
globals.addRow(dt, "PartOfDomain", queryObj
("PartOfDomain"))
globals.addRow(dt, "Domain", queryObj("Domain"))
globals.addRow(dt, "Workgroup", queryObj
("Workgroup"))
dt.AcceptChanges()
Me.DataGrid1.DataSource = dt
End If
Catch err As ManagementException
MessageBox.Show("An error occurred while querying for WMI data: " & err.Message)
End Try
End Sub
I excluded some of the properties (as it would make the program too long) from the existing WMI class (Win32_ComputerSystem) to give you only the most important ones. The above program would list the server name, user logged in, DNSHostName, description (if any), whether it's part of a domain or not, the domain it belongs to, and finally the workgroup it belongs to. You can further extend the above program with several other properties available in the “Win32_ComputerSystem” class. You can even refer to MSDN online for further properties.
You can achieve the same thing with VBScript by using the following code:
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery( _
"SELECT * FROM Win32_ComputerSystem",,48)
For Each objItem in colItems
Wscript.Echo "Caption: " & objItem.Caption
Wscript.Echo "Name: " & objItem.Name
Wscript.Echo "UserName: " & objItem.UserName
Wscript.Echo "DNSHostName: " & objItem.DNSHostName
Wscript.Echo "Description: " & objItem.Description
Wscript.Echo "PartOfDomain: " & objItem.PartOfDomain
Wscript.Echo "Domain: " & objItem.Domain
Wscript.Echo "Workgroup: " & objItem.Workgroup
Next
Next: How to retrieve all user account information using WMI >>
More Visual Basic.NET Articles
More By Jagadish Chaterjee