About This Code
Brief Description:
WMI Hardware interrogation
Contributor:
Scott Wichall
Notes Version:
R5.x, R6.x
Last Modified:
06 Jul 2004
OpenNTF Disclaimer
All of the program code and information presented in the OpenNTF.org Code Bin are provided "as-is", and should be used at your own risk. OpenNTF.org make no express or implied warranty about anything in the Code Bin, and OpenNTF.org will not be responsible or liable for any damage caused by the use or misuse of anything from this site. OpenNTF.org makes no guarantees about anything. Please thoroughly test all of the knowledge and code you find here before you attempt to use them in your production environment.
Code / Description
This code is for interrogating the WMI (Windows Management Interface) on WIN32 windows platforms. It will work succesfully on all versions of windows 2000 + (Including server OS).
The code will allow you to perform network based queries from the operating system of the target computer - eg IP addressing, service pack level etc. All objects/collections return as variants
OS root query (sComputer is the DNS name of the target machine)
Set objWMIService = GetObject("winmgmts:\\" & sComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_OperatingSystem",,48)
Software query (sComputer is the DNS name of the target machine)
Set objWMIService = GetObject("winmgmts:\\" & sComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select Caption, Version from Win32_Product",,48)
Network Query(s) (sComputer is the DNS name of the target machine)
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set IPConfigSet = objWMIService.ExecQuery _
("Select IPAddress, DefaultIPGateway, DNSServerSearchOrder from Win32_NetworkAdapterConfiguration ")
Usage / Example
Operating System Query - form based button. Uses DNS name of target machine from a field called NodeLetters. Outputs results into fields on document
Sub Click(Source As Button)
On Error Resume Next
Dim s As New notessession, w As New notesuiworkspace
Dim objWMIService As Variant
Dim colItems As Variant
Dim sCaption As String, sOrganisation As String, sSerialNumber As String, sServMajor As String, sServMinor As String
Dim sVersion As String, sTotalMemory As String, sServicePack As String, sBuildNo As String
strComputer = w.CurrentDocument.FieldGetText("NodeLetters")
Print "Getting WMI Information for " & strComputer
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_OperatingSystem",,48)
Forall objItem In colItems
sBuildNo = "" & objItem.BuildNumber
sCaption = "" & objItem.Caption
sOrganisation= "" & objItem.Organization
sSerialNumber= "" & objItem.SerialNumber
sServMajor = "" & objItem.ServicePackMajorVersion
sServMinor = "" & objItem.ServicePackMinorVersion
sTotalMemory = "" & objItem.TotalVisibleMemorySize
End Forall
If Trim(sCaption) <> "" Then
'a string has been returned
If w.CurrentDocument.EditMode = False Then w.CurrentDocument.EditMode = True
Call w.CurrentDocument.FieldSetText("OpSystem", sCaption)
Call w.CurrentDocument.FieldSetText("ServicePack", sServMajor & "." & sServMinor)
Call w.CurrentDocument.FieldSetText("OSserial", sSerialNumber)
Dim RamSize As Long
RamSize =Clng(sTotalMemory) / 1024
Call w.CurrentDocument.FieldSetText("RamSize", Str(RamSize))
End If
Print "Finished"
End Sub
Software WMI Query - Form based button. Uses target machine DNS name from field NodeLetters
Puts the results into a recordset and sorts them before writing out installed software into the current UI document
Sub Click(Source As Button)
On Error Resume Next
Dim s As New notessession, w As New notesuiworkspace
Dim objWMIService As Variant, colItems As Variant, rsSort As Variant
Dim sApp As String, sComputer As String
sComputer = w.CurrentDocument.FieldGetText("NodeLetters")
Print "Getting WMI Information for " & sComputer
Set objWMIService = GetObject("winmgmts:\\" & sComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select Caption, Version from Win32_Product",,48)
If w.CurrentDocument.EditMode = False Then w.CurrentDocument.EditMode = True
Call w.CurrentDocument.FieldClear("InstalledApps")
'create a recordset to hold our returned info so we can sort it
Set rsSort = CreateObject("ADODB.Recordset")
rsSort.CursorLocation = adUseClient
With rsSort.Fields
.Append "Caption", adVarChar, 255
.Append "Version", adVarChar, 255
End With
rsSort.Open
'add the WMI results into the recordset
Forall objItem In colItems
Print "Identified " & objItem.Caption & " .........Sorting"
With rsSort
.AddNew
.Fields("Caption").Value = Cstr(objItem.Caption)
.Fields("Version").Value = Cstr(objItem.Version)
.Update
End With
End Forall
'finished with the WMI = destroy it
Set colItems = Nothing
rsSort.Sort = "Caption ASC"
rsSort.MoveFirst
Do While Not rsSort.EOF
sApp = ""
If Trim(rsSort.Fields("Caption")) <> "" Then
sApp = rsSort.Fields("Caption").Value & " (" & rsSort.Fields("Version").Value & ")"
If w.CurrentDocument.FieldGetText("InstalledApps") = "" Then
Call w.CurrentDocument.FieldAppendText("InstalledApps",sApp)
Else
Call w.CurrentDocument.FieldAppendText("InstalledApps",Chr$(13) & Chr$(10) & sApp)
End If
End If
rsSort.MoveNext
Loop
rsSort.Close
Set rsSort = Nothing
Print "Finished"
End Sub
Network Query
Sub Click(Source As Button)
On Error Resume Next
Dim s As New notessession, w As New notesuiworkspace
Dim objWMIService As Variant
Dim colItems As Variant
Dim sCaption As String, sOrganisation As String, sSerialNumber As String, sServMajor As String, sServMinor As String
Dim sVersion As String, sTotalMemory As String, sServicePack As String, sBuildNo As String
strComputer = w.CurrentDocument.FieldGetText("NodeLetters")
Print "Getting WMI Information for " & strComputer
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set IPConfigSet = objWMIService.ExecQuery _
("Select IPAddress, DefaultIPGateway, DNSServerSearchOrder from Win32_NetworkAdapterConfiguration ")
Forall IPConfig In IPConfigSet
If Not Isnull(IPConfig.IPAddress) Then
If w.CurrentDocument.EditMode = False Then w.CurrentDocument.EditMode = True
Call w.CurrentDocument.FieldClear("IPAddress")
Call w.CurrentDocument.FieldClear("Gateway")
Call w.CurrentDocument.FieldClear("DNSServers")
For i=Lbound(IPConfig.IPAddress) To Ubound(IPConfig.IPAddress)
If w.CurrentDocument.FieldGetText("IPAddress") = "" Then
Call w.CurrentDocument.FieldAppendText("IPAddress",IPConfig.IPAddress(i))
Else
Call w.CurrentDocument.FieldAppendText("IPAddress",Chr$(13) & Chr$(10) & IPConfig.IPAddress(i))
End If
If w.CurrentDocument.FieldGetText("Gateway") = "" Then
If IPConfig.DefaultIPGateway(i) <> "" Then Call w.CurrentDocument.FieldAppendText("Gateway",IPConfig.DefaultIPGateway(i))
Else
If IPConfig.DefaultIPGateway(i) <> "" Then Call w.CurrentDocument.FieldAppendText("Gateway",Chr$(13) & Chr$(10) & IPConfig.DefaultIPGateway(i))
End If
Next
End If
End Forall
'get the DNS server details - do this seperately as it could be an array of IP addresses
Set IPConfigSet = objWMIService.ExecQuery _
("Select DNSServerSearchOrder from Win32_NetworkAdapterConfiguration ")
Forall IPConfig In IPConfigSet
If Not Isnull(IPConfig.DNSServerSearchOrder) Then
Call w.CurrentDocument.FieldClear("DNSServers")
For i=Lbound(IPConfig.DNSServerSearchOrder) To Ubound(IPConfig.DNSServerSearchOrder)
If w.CurrentDocument.FieldGetText("DNSServers") = "" Then
Call w.CurrentDocument.FieldAppendText("DNSServers",IPConfig.DNSServerSearchOrder(i))
Else
Call w.CurrentDocument.FieldAppendText("DNSServers",Chr$(13) & Chr$(10) & IPConfig.DNSServerSearchOrder(i))
End If
Next
End If
End Forall
Print "Finished"
End Sub