Created
November 4, 2011 20:01
-
-
Save glesica/1340333 to your computer and use it in GitHub Desktop.
A VBScript to enumerate applications installed on a machine and output a tab-delimited CSV file, one for each machine. The idea is for this to be slightly less awful than all the similar scripts out there on the webz.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'******************************************************************************* | |
' software.vbs | |
' Create a list of software installed on one or more | |
' machines in TSV format. | |
' | |
' Usage: cscript.exe software.vbs [/path:<path>] [machine names] | |
' | |
' If <path> is given, the script will attempt to store logs | |
' in that location. It is treated as a relative path and must | |
' end in a backslash. | |
'******************************************************************************* | |
' Define a bunch of constants VBScript should define for us but doesn't | |
Const ForReading = 1, ForWriting = 2, ForAppending = 8 | |
' Get shell and network | |
Set objShell = CreateObject("Wscript.Shell") | |
Set objNet = CreateObject("WScript.Network") | |
Set objArgs = WScript.Arguments | |
' Array to hold computer names | |
Dim arrComputers() | |
' Get attributes, if we got command line arguments, use those | |
' as the hostnames. Otherwise use the local machine name. | |
If objArgs.Unnamed.Length > 0 Then | |
Dim i | |
i = 0 | |
While i < objArgs.Unnamed.Length | |
ReDim Preserve arrComputers(i) | |
arrComputers(i) = objArgs.Unnamed.Item(i) | |
i = i + 1 | |
Wend | |
Else | |
ReDim arrComputers(0) | |
arrComputers(0) = objNet.ComputerName | |
End If | |
' Get named arguments if they were passed | |
Dim strLogPath | |
If objArgs.Named.Length > 0 Then | |
strLogPath = objArgs.Named.Item("path") | |
Else | |
strLogPath = "" | |
End If | |
' Create a FSO to write the output to a text file | |
Set objFSO = CreateObject("Scripting.FileSystemObject") | |
' Get a list of software from each machine and format it as TSV. | |
For Each strComputer In arrComputers | |
' Put some error handling in here, if we can't contact the computer | |
' then we log the failure and move on. | |
On Error Resume Next | |
' Get the list of installed software | |
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") | |
Set colItems = objWMIService.ExecQuery("Select * FROM Win32_Product",,48) | |
If Err.Number <> 0 Then | |
' An error occurred trying to contact the machine, skip this computer | |
' and log the error so it can be noted and investigated. | |
objShell.LogEvent 1, "software.vbs: Could not contact computer: " & _ | |
strComputer | |
On Error Goto 0 | |
Else | |
' No error contacting the machine so write the log file. | |
On Error Goto 0 | |
' Create and/or open the log file | |
Dim currentDate | |
currentDate = Date | |
Dim currentTime | |
currentTime = Time | |
Set objLogFile = objFSO.OpenTextFile(_ | |
strLogPath & _ | |
strComputer & _ | |
"_" & _ | |
Year(currentDate) & _ | |
"-" & _ | |
Month(currentDate) & _ | |
"-" & _ | |
Day(currentDate) & _ | |
"-" & _ | |
Hour(currentTime) & _ | |
"-" & _ | |
Minute(currentTime) & _ | |
"-" & _ | |
Second(currentTime) & _ | |
"_" & _ | |
"log.csv", ForWriting, True) | |
' Start the tsv | |
objLogFile.WriteLine _ | |
"computer" & _ | |
vbTab & _ | |
"application" & _ | |
vbTab & _ | |
"vendor" & _ | |
vbTab & _ | |
"version" | |
' Build the tsv | |
For Each objItem In colItems | |
objLogFile.WriteLine _ | |
strComputer & vbTab & _ | |
objItem.Name & vbTab & _ | |
objItem.Vendor & vbTab & _ | |
objItem.Version | |
Next | |
' Close the log file | |
objLogFile.Close | |
End If | |
Next | |
WScript.Echo "Finished: software.vbs has finished. Check event log for errors." | |
WScript.Quit(0) |
I'd like to filter the result to exclude "Microsoft Corporation" or other vendors.
Do you think you can implement an option such as [/exc:"Microsoft Corporation","Intel Corporation",...] to exclude editors from report ?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
awesome