Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Returning all WMI values

Status
Not open for further replies.

jsneed

MIS
Mar 10, 2003
3
US
This code is working but it only returns the first value that is queried into the database. How do I make it write all info it finds into the database?

Sub subWriteSoftwareInfo
set colItems = GetObject(strWinMgt).ExecQuery("select * from Win32_Product")
objRS.CursorType = iCursorType
objRS.LockType = iLockType
objRS.Source = "tblSoftware"
objRS.ActiveConnection = objCon
objRS.Open
for each objItem in colItems
objRS.AddNew
objRS("intCompID") = intCompID
objRS("txtCaption") = objItem.Caption
objRS("txtDescription") = objItem.Description
objRS("txtName")= objItem.Name
objRS("txtVendor")= objItem.Vendor
objRS("txtVersion")= objItem.Version
objRS.Update

next
objRS.Close
End Sub
 
I am not sure about this because this is ADO but I don't see a Set statement for objItem You have set the variable colItems but there is no reference, except in your For Each statement, to objItem.

Let me know. I am a little shaky on ADO yet so maybe I will learn something here.

Bob Scriver
 
Dim strComputerName ' The Computer Name to be queried via WMI
Dim strWinMgt ' The WMI management String
Dim objCon ' A Connection Object for database connectivity
Dim objRS ' A Recordset Object for database connectivity
Dim sProviderName ' The OLE Provider Type
Dim iCursorType ' The Cursor Type for the Recordset
Dim iLockType ' The Lock Type for the Recordset
Dim sDataSource ' The name and location of the database
Dim intCompID ' A computer ID asssigned when the computer is added to the database
Dim intRam ' The amount of RAM in the computer.

'
' Get the Computer Name to Collect Information For
'
strComputerName = InputBox("Enter the name of the computer you wish to query")
strWinMgt = "winmgmts://" & strComputerName &""

'
' Write Computer Info to Database
'

call subConnectionOpen
call subWriteComputerInfo
call subWriteDiskInfo
call subWriteIPInfo
call subWriteSoftwareInfo
call subConnectionClose

wscript.echo "Finished Collection Information for the System."


Sub subConnectionOpen
Set objCon = CreateObject("ADODB.Connection")
Set objRS = CreateObject("ADODB.Recordset")
sProviderName = "Microsoft.Jet.OLEDB.4.0"
iCursorType = 1
iLockType = 3
sDataSource = "\\d01svr67\desktop\jeff_tools\wmi_database_current\dbassets.mdb"
objCon.Provider = sProviderName
objCon.Properties("Data Source") = sDataSource
objCon.Open
End Sub

Sub subConnectionClose
set objRS = nothing
set objCon = nothing
End Sub


Sub subWriteComputerInfo
Set CompSysSet = GetObject(strWinMgt).ExecQuery("select * from Win32_ComputerSystem")
for each CompSys in CompSysSet
strDescription = CompSys.Description
strModel = CompSys.Model
strName = CompSys.Name
intRAM = Clng(CompSys.TotalPhysicalMemory/1000000)
next

objRS.CursorType = iCursorType
objRS.LockType = iLockType
objRS.Source = "tblComputer"
objRS.ActiveConnection = objCon
objRS.Open
objRS.AddNew
objRS("strName") = strName
objRS("strModel") = strModel
objRS("strDescription")= strDescription
objRS.Update
intCompID = objRS("ID")
objRS.Close

objRS.CursorType = iCursorType
objRS.LockType = iLockType
objRS.Source = "tblMemory"
objRS.ActiveConnection = objCon
objRS.Open
objRS.AddNew
objRS("intCompID") = intCompID
objRS("intRAM")= intRam
objRS.Update
objRS.Close

End Sub


Sub subWriteDiskInfo
'
' Get Disk Information
'
Set DiskSet = GetObject(strWinMgt).ExecQuery("select FreeSpace,Size,Name from Win32_LogicalDisk where DriveType=3")
objRS.CursorType = iCursorType
objRS.LockType = iLockType
objRS.Source = "tblDisk"
objRS.ActiveConnection = objCon
objRS.Open
For each Disk in DiskSet
strDisplayDiskInfo = strDisplayDiskInfo & "Drive " & Disk.Name & " Free Space: " & Clng(Disk.FreeSpace/1000000) & " MB Total Size: " & Clng(Disk.Size/1000000) & " MB"& vbCrlf
if len(Left(disk.name,1)) >0 then
objRS.AddNew
objRS("intCompID") = intCompID
objRS("strDriveLetter") = Left(disk.name,1)
objRS("strFreeSpace") = Clng(Disk.FreeSpace/1000000)
objRS("strTotalSpace")= Clng(Disk.Size/1000000)
objRS.Update
end if
Next
objRS.Close
' Wscript.echo strDisplayDiskInfo
End Sub

Sub subWriteIPInfo
set IPConfigSet = GetObject(strWinMgt).ExecQuery("select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")
objRS.CursorType = iCursorType
objRS.LockType = iLockType
objRS.Source = "tblIPInfo"
objRS.ActiveConnection = objCon
objRS.Open
for each IPConfig in IPConfigSet
if Not IsNull(IPConfig.IPAddress) then
for i=LBound(IPConfig.IPAddress) to UBound(IPConfig.IPAddress)
' WScript.Echo "IPAddress: " & IPConfig.IPAddress(i) & " Subnet: " &_
' IPConfig.IPSubnet(i) & "MAC Address " & IPConfig.MACAddress
objRS.AddNew
objRS("intCompID") = intCompID
objRS("intNICID") = i
objRS("txtIPAddress") = IPConfig.IPAddress(i)
objRS("txtSubnetMask")= IPConfig.IPSubnet(i)
objRS("txtMACAddress")= IPConfig.MACAddress
objRS("txtServiceName")= IPConfig.ServiceName
objRS("txtDescription")= IPConfig.Description
objRS.Update
next
end if
next
objRS.Close
End sub

Sub subWriteSoftwareInfo
Dim rsPackage, sSQL, iPackageID
set colItems = GetObject(strWinMgt).ExecQuery("select * from Win32_Product")

for Each objItem In colItems
sSQL = "SELECT * FROM tblPackage WHERE txtName = '" & objItem.Name & "'"
sSQL = sSQL & " AND txtVendor = '" & objItem.Vendor & "'"
sSQL = sSQL & " AND txtVersion = '" & objItem.Version & "'"

Set rsPackage = objCon.Execute(sSQL)

If rsPackage.EOF Then
sSQL = "INSERT INTO tblPackage (txtCaption,txtDescription,txtName,txtVendor,txtVersion) "
sSQL = sSQL & " VALUES ('" & objItem.Caption & "','" & objItem.Description & "','" & objItem.Name & "','" & objItem.Vendor & "','" & objItem.Version & "')"
objCon.Execute sSQL
sSQL = "SELECT max(intPackageID) As intPackageID FROM tblPackage"
Set rsPackage = objCon.Execute(sSQL)
iPackageID = rsPackage("intPackageID")
Else
iPackageID = rsPackage("intPackageID")
End If
sSQL = "INSERT INTO tblComputerPackage(intCompID, intPackageID) VALUES (" & intCompID & "," & iPackageID & ")"
objCon.Execute sSQL
Next

rsPackage.Close
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top