Attempting to get some information from a WMI query but it keeps returning incorrect results. I use this kind of script for a few things and yet to figure out why it does this. I figured out part of the problem is that when it can not query that computer is just uses the last good return as the result. I want it to return either a null result or show me something saying it did not return rather then just the last good result. If you are admin and run this on your system you will see what I am saying. I am not concerned about the PCs that will not return, I can figure those out in the future when they do not return. let me know if have any ideas or advice. Thank you.
I attempted to comment the code as much as possible to help people read it
'--------------Cbrand.vbs
'header---------------------------------------------------------------------------------------------------------------------------------------------
on error resume next
'AD request var for reference
Dim objRootDSE, adoCommand, adoConnection, strDNSDomain
Dim strBase, strFilter, strAttributes, strQuery, adoRecordset
'AD work field
Dim strNTName
'excel reference
Dim objExcel, objWorkbook
'WMI referenc
Dim Prod, ComV, colv, wmiquery, objwmi, wmiroot, strcomputer, Computer
'other var
Dim aMachines, i, T
'reference Information------------------------------------------------------------------------------------------------------------------------
'AD request----start
' Determine DNS domain name.
Set objRootDSE = GetObject("LDAP://RootDSE")
strDNSDomain = objRootDSE.Get("defaultNamingContext")
' Use ADO to search Active Directory.
Set adoCommand = CreateObject("ADODB.Command")
Set adoConnection = CreateObject("ADODB.Connection")
adoConnection.Provider = "ADsDSOObject"
adoConnection.Open "Active Directory Provider"
adoCommand.ActiveConnection = adoConnection
' Search entire domain.
strBase = "<LDAP://" & strDNSDomain & ">"
' Filter on requesting place object. fill in "computer"
strFilter = "(objectCategory=computer)"
' Comma delimited list of attributes.
strAttributes = "sAMAccountName"
' Construct LDAP syntax query.
strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
' Execute the query.
adoCommand.CommandText = strQuery
adoCommand.Properties("Page Size") = 100
adoCommand.Properties("Timeout") = 30
adoCommand.Properties("Cache Results") = False
Set adoRecordset = adoCommand.Execute
'AD request----end
'build an excel app----start
Set objExcel = CreateObject("Excel.Application")'create the excel file
Set objWorkbook = objExcel.Workbooks.Open("C:\Documents and Settings\thuffman.HHGREGG\Desktop\TEST\computer\test.xls") 'file lication
objExcel.Visible = True 'True -if you want to see it run. False to run in background.
objExcel.Cells(1, 1).Value = "computer name" 'cell values
objExcel.Cells(1, 2).Value = "computer system product"
objExcel.Cells(1, 3).Value = "model"
objExcel.Cells(1, 4).Value = "manufacturer"
objExcel.cells(1, 5).Value = "value"
'build an excel app----end
'set variables
i=2
'worker---------------------------------------------------------------------------------------------------------------------------------------------
Do Until adoRecordset.EOF ' loop through till there are no more items in list
strNTName = adoRecordset.Fields("sAMAccountName").Value 'items to query in AD
aMachines = split(Left(strNTName,Len(strNTName)-1), ";") 'remove the final character off of the return $ is added to values in AD
for each Computer in aMachines
'Wmi reference----start
wmiroot = "winmgmts:\\" & Computer & "\root\cimv2"
set objwmi = Getobject(wmiroot)
wmiquery = "select * from Win32_ComputerSystemProduct"
set colv = objwmi.execQuery (wmiQuery)
'Wmi reference----end
'output-----------------------------------------------------------------------------------------------------------------------------------------------
for each ComV in colv
T=1 'CELL A
objExcel.Cells(i, T).Value = Computer
objExcel.Cells(i, T).Font.Bold = TRUE
objExcel.Cells(i, T).Interior.ColorIndex = 0
objExcel.Cells(i, T).Font.ColorIndex = 1
objexcel.cells(i, T).font.size = 12
T=T+1 'CELL B
objExcel.Cells(i, T).Value = (ComV.Name)
objExcel.Cells(i, T).Font.Bold = TRUE
objExcel.Cells(i, T).Interior.ColorIndex = 0
objExcel.Cells(i, T).Font.ColorIndex = 1
objexcel.cells(i, T).font.size = 12
next ' for each ComV in colv
next 'for each computer in aMachines
'WMI reference -------- start
for each Computer in aMachines
wmiroot = "winmgmts:\\" & Computer & "\root\cimv2"
wmiquery = "select * from Win32_SystemEnclosure"
set colsnnm = objwmi.execQuery (wmiQuery)
'WMI reference -------- end
for each item in colsnnm
T=T+1 'CELL C
objExcel.Cells(i, T).Value = (item.model)
objExcel.Cells(i, T).Font.Bold = TRUE
objExcel.Cells(i, T).Interior.ColorIndex = 0
objExcel.Cells(i, T).Font.ColorIndex = 1
objexcel.cells(i, T).font.size = 12
T=T+1
objExcel.Cells(i, T).Value = (item.manufacturer)
objExcel.Cells(i, T).Font.Bold = TRUE
objExcel.Cells(i, T).Interior.ColorIndex = 0
objExcel.Cells(i, T).Font.ColorIndex = 1
objexcel.cells(i, T).font.size = 12
next 'for each item in colsnnm
next 'for each computer in aMachines
i=i+1 ' increase cells by 1 for loops
adoRecordset.MoveNext 'move to next item in AD before loop
loop 'loop do until adoRecordset.EOF
'functions and subs-------------------------------------------------------------------------------------------------------------------------------
'close-------------------------------------------------------------------------------------------------------------------------------------------------
objExcel.Workbooks(1).Save 'Save the workbook, not excel file
objExcel.Workbooks(1).close 'close the workbook
objexcel.quit 'close excel file.
adoRecordset.Close ' close your query
adoConnection.Close
' Clean up.
Set objRootDSE = Nothing
Set adoCommand = Nothing
Set adoConnection = Nothing
Set adoRecordset = Nothing
'echo to know it is finished
wscript.echo "finished
I attempted to comment the code as much as possible to help people read it
'--------------Cbrand.vbs
'header---------------------------------------------------------------------------------------------------------------------------------------------
on error resume next
'AD request var for reference
Dim objRootDSE, adoCommand, adoConnection, strDNSDomain
Dim strBase, strFilter, strAttributes, strQuery, adoRecordset
'AD work field
Dim strNTName
'excel reference
Dim objExcel, objWorkbook
'WMI referenc
Dim Prod, ComV, colv, wmiquery, objwmi, wmiroot, strcomputer, Computer
'other var
Dim aMachines, i, T
'reference Information------------------------------------------------------------------------------------------------------------------------
'AD request----start
' Determine DNS domain name.
Set objRootDSE = GetObject("LDAP://RootDSE")
strDNSDomain = objRootDSE.Get("defaultNamingContext")
' Use ADO to search Active Directory.
Set adoCommand = CreateObject("ADODB.Command")
Set adoConnection = CreateObject("ADODB.Connection")
adoConnection.Provider = "ADsDSOObject"
adoConnection.Open "Active Directory Provider"
adoCommand.ActiveConnection = adoConnection
' Search entire domain.
strBase = "<LDAP://" & strDNSDomain & ">"
' Filter on requesting place object. fill in "computer"
strFilter = "(objectCategory=computer)"
' Comma delimited list of attributes.
strAttributes = "sAMAccountName"
' Construct LDAP syntax query.
strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
' Execute the query.
adoCommand.CommandText = strQuery
adoCommand.Properties("Page Size") = 100
adoCommand.Properties("Timeout") = 30
adoCommand.Properties("Cache Results") = False
Set adoRecordset = adoCommand.Execute
'AD request----end
'build an excel app----start
Set objExcel = CreateObject("Excel.Application")'create the excel file
Set objWorkbook = objExcel.Workbooks.Open("C:\Documents and Settings\thuffman.HHGREGG\Desktop\TEST\computer\test.xls") 'file lication
objExcel.Visible = True 'True -if you want to see it run. False to run in background.
objExcel.Cells(1, 1).Value = "computer name" 'cell values
objExcel.Cells(1, 2).Value = "computer system product"
objExcel.Cells(1, 3).Value = "model"
objExcel.Cells(1, 4).Value = "manufacturer"
objExcel.cells(1, 5).Value = "value"
'build an excel app----end
'set variables
i=2
'worker---------------------------------------------------------------------------------------------------------------------------------------------
Do Until adoRecordset.EOF ' loop through till there are no more items in list
strNTName = adoRecordset.Fields("sAMAccountName").Value 'items to query in AD
aMachines = split(Left(strNTName,Len(strNTName)-1), ";") 'remove the final character off of the return $ is added to values in AD
for each Computer in aMachines
'Wmi reference----start
wmiroot = "winmgmts:\\" & Computer & "\root\cimv2"
set objwmi = Getobject(wmiroot)
wmiquery = "select * from Win32_ComputerSystemProduct"
set colv = objwmi.execQuery (wmiQuery)
'Wmi reference----end
'output-----------------------------------------------------------------------------------------------------------------------------------------------
for each ComV in colv
T=1 'CELL A
objExcel.Cells(i, T).Value = Computer
objExcel.Cells(i, T).Font.Bold = TRUE
objExcel.Cells(i, T).Interior.ColorIndex = 0
objExcel.Cells(i, T).Font.ColorIndex = 1
objexcel.cells(i, T).font.size = 12
T=T+1 'CELL B
objExcel.Cells(i, T).Value = (ComV.Name)
objExcel.Cells(i, T).Font.Bold = TRUE
objExcel.Cells(i, T).Interior.ColorIndex = 0
objExcel.Cells(i, T).Font.ColorIndex = 1
objexcel.cells(i, T).font.size = 12
next ' for each ComV in colv
next 'for each computer in aMachines
'WMI reference -------- start
for each Computer in aMachines
wmiroot = "winmgmts:\\" & Computer & "\root\cimv2"
wmiquery = "select * from Win32_SystemEnclosure"
set colsnnm = objwmi.execQuery (wmiQuery)
'WMI reference -------- end
for each item in colsnnm
T=T+1 'CELL C
objExcel.Cells(i, T).Value = (item.model)
objExcel.Cells(i, T).Font.Bold = TRUE
objExcel.Cells(i, T).Interior.ColorIndex = 0
objExcel.Cells(i, T).Font.ColorIndex = 1
objexcel.cells(i, T).font.size = 12
T=T+1
objExcel.Cells(i, T).Value = (item.manufacturer)
objExcel.Cells(i, T).Font.Bold = TRUE
objExcel.Cells(i, T).Interior.ColorIndex = 0
objExcel.Cells(i, T).Font.ColorIndex = 1
objexcel.cells(i, T).font.size = 12
next 'for each item in colsnnm
next 'for each computer in aMachines
i=i+1 ' increase cells by 1 for loops
adoRecordset.MoveNext 'move to next item in AD before loop
loop 'loop do until adoRecordset.EOF
'functions and subs-------------------------------------------------------------------------------------------------------------------------------
'close-------------------------------------------------------------------------------------------------------------------------------------------------
objExcel.Workbooks(1).Save 'Save the workbook, not excel file
objExcel.Workbooks(1).close 'close the workbook
objexcel.quit 'close excel file.
adoRecordset.Close ' close your query
adoConnection.Close
' Clean up.
Set objRootDSE = Nothing
Set adoCommand = Nothing
Set adoConnection = Nothing
Set adoRecordset = Nothing
'echo to know it is finished
wscript.echo "finished