I am trying to write a script that gives me the Version of Microsoft Office that is on all of the computers at the office (along with a few other things). I can get it to output everything except which Office it is (i.e Office 200). If I take out the objItem.ProductName part it runs fine. I put it back in and I get an "Invalid Query Error, code 80041017. What am I doing wrong? Here is the entire script. Thanks
If WScript.Arguments.Count = 0 Then
MsgBox "Please drag a text file that contains computer names onto this script file for processing."
WScript.Quit
End If
strInputFile = WScript.Arguments.Item(0)
Set objFSO = CreateObject("Scripting.FileSystemObject")
Const intForReading = 1
Set objInputFile = objFSO.OpenTextFile(strInputFile, intForReading, False)
strResultsFile = Replace(WScript.ScriptFullName, WScript.ScriptName, "") & "Results_" & Replace(TimeNow(Now), "-", "") & ".csv"
strNoContactFile = Replace(WScript.ScriptFullName, WScript.ScriptName, "") & "NoContact_" & Replace(TimeNow(Now), "-", "") & ".txt"
Set objOutputFile = objFSO.CreateTextFile(strResultsFile, True)
objOutputFile.Write "Host Name,IP Address(es),Serial Number,Model, Office Path, Install Date, Last Accessed, Product Name, Version"
Set objNoContactFile = objFSO.CreateTextFile(strNoContactFile, True)
While Not objInputFile.AtEndOfStream
strComputer = objInputFile.ReadLine
If Ping(strComputer) = True Then
On Error Resume Next
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonat e}!\\" & strComputer & "\root\cimv2")
If Err.Number = 0 Then
On Error GoTo 0
' GET THE IP ADDRESS(ES)
Set colComputerIP = objWMIService.ExecQuery _
("Select IPAddress from Win32_NetworkAdapterConfiguration")
strIPAddress = ""
For Each IPConfig in colComputerIP
If Not IsNull(IPConfig.IPAddress) Then
For intIPCount = LBound(IPConfig.IPAddress) To UBound(IPConfig.IPAddress)
If strIPAddress = "" Then
strIPAddress = IPConfig.IPAddress(intIPCount)
Else
strIPAddress = strIPAddress & ";" & IPConfig.IPAddress(intIPCount)
End If
Next
End If
Next
' GET THE SERIAL NUMBER
Set colBIOS = objWMIService.ExecQuery _
("Select SerialNumber from Win32_BIOS")
For Each objBIOS In colBIOS
strSN = objBIOS.SerialNumber
Next
' GET THE COMPUTER MODEL AND HOSTNAME
Set colComputer = objWMIService.ExecQuery _
("Select Model, Name from Win32_ComputerSystem")
For Each objComp In colComputer
strModel = objComp.Model
strHostname = objComp.Name
Next
' GET THE OFFICE VERSION, INSTALL DATE, AND LAST ACCESSED DATE
Set colItems = objWMIService.ExecQuery("SELECT Name,Drive,InstallDate,LastAccessed,Version,and ProductName FROM CIM_DataFile WHERE (Name = 'c:\\Program Files\\Microsoft Office\\Office\\WINWORD.EXE'or Name = 'd:\\Program Files\\Microsoft Office\\Office\\WINWORD.EXE'or Name = 'c:\\Program Files\\Microsoft Office\\Office10\\WINWORD.EXE'or Name = 'd:\\Program Files\\Microsoft Office\\Office10\\WINWORD.EXE' or Name = 'c:\\Program Files\\Microsoft Office\\Office11\\WINWORD.EXE'or Name = 'd:\\Program Files\\Microsoft Office\\Office11\\WINWORD.EXE'or Name = 'c:\\Program Files\\Microsoft Office\\Office12\\WINWORD.EXE'or Name = 'd:\\Program Files\\Microsoft Office\\Office12\\WINWORD.EXE')", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly)
OfficeFound = False
For Each ObjItem In colItems
strVersion = objItem.Name
strInstall = objItem.InstallDate
strAccessed = objItem.LastAccessed
strSWVersion = objItem.Version
strProductName = objItem.ProductName
OfficeFound = True
Next
If (OfficeFound = True) Then
objOutputFile.Write VbCrLf & Trim(strHostname) & "," & Trim(strIPAddress) & "," & Trim(strSN) & "," & Trim(strModel) & "," & Trim(strVersion) & "," & WMIDStringToDate(strInstall) & "," & WMIDStringToDate(strAccessed) & "," & Trim(strProductName)& "," & Trim(strSWVersion)
Else
objOutputFile.Write VbCrLf & Trim(strHostname) & "," & Trim(strIPAddress) & "," & Trim(strSN) & "," & Trim(strModel)
End If
Else
Err.Clear
On Error GoTo 0
objNoContactFile.WriteLine Trim(strComputer)
End If
Else
objNoContactFile.WriteLine Trim(strComputer)
End If
Wend
objInputFile.Close
objOutputFile.Close
objNoContactFile.Close
Set objInputFile = Nothing
Set objOutputFile = Nothing
Set objNoContactFile = Nothing
MsgBox "Finished processing the input file." & VbCrLf & "Results are in: " & strResultsFile & VbCrLf & "PCs not found: " & strNoContactFile
Function Ping(strComputer)
Dim objShell, boolCode
Set objShell = CreateObject("WScript.Shell")
boolCode = objShell.Run("Ping -n 1 -w 300 " & strComputer, 0, True)
If boolCode = 0 Then
Ping = True
Else
Ping = False
End If
End Function
Function TimeNow(dDateTime)
TimeNow = Year(Now) & "-" &_
Right("00" & Month(Now), 2) & "-" &_
Right("00" & Day(Now), 2) & "-" &_
Right("00" & Hour(Now), 2) & "-" &_
Right("00" & Minute(Now), 2) & "-" &_
Right("00" & Second(Now), 2)
End Function
Function WMIDStringToDate(dtmDate)
'WScript.Echo dtm:
WMIDStringToDate = (Mid(dtmDate, 5, 2) & "/" & _
Mid(dtmDate, 7, 2) & "/" & Left(dtmDate, 4) _
& " " & Mid (dtmDate, 9, 2) & ":" & Mid(dtmDate, 11, 2) & ":" & Mid(dtmDate,13, 2))
End Function