Sunday, August 31, 2008

Retrieve list of installed programs

"Hi Dave, I'd like to list all installed software to text file for later parsing. My final point is to check if any of VMware software is installed."

OK, no problem for me. You have a lot of ways how to do that. Let me show you some of them.

1) From WMI via VBS
In this case we use Win32_Product WMI class and search it for specific software name:

Set objWMIService = GetObject("winmgmts:\\VMMORAVEC\root\CIMV2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Product WHERE name LIKE '%vmware%'", "WQL")

For Each objItem In colItems
WScript.Echo objItem.Name
Next

2) From WMI via wmic
This way is easy but takes long time. especially when we ran at as "runas" command it takes five minutes per computer which was not accepted by requestor. Anyway this is very useful method how retrieve data from WMI.

wmic product find /I "VMware"

3) From Registry via VBS
In this case we'll check registry but through WMI StdRegProv class.

Const HKLM = &H80000002
Const strBaseKey = "Software\Microsoft\Windows\CurrentVersion\Uninstall\"
Set objReg = GetObject("winmgmts://vmmoravec/root/default:StdRegProv")
objReg.EnumKey HKLM,strBaseKey,arrSubKeys

For Each strSubKey In arrSubKeys
intRet = objReg.GetStringValue(HKLM, strBaseKey & strSubKey, "DisplayName", strValue)
if InStr(strValue, "VMware") > 0 Then
WScript.Echo strvalue
End If
Next


4) From Registry via reg.exe
The way we used for our case. Fast and easy (and showing only VMware software):

reg query HKLM\Software\Microsoft\Windows\CurrentVersion\Uninstall /s ¦ find /I "VMware" ¦ find /I "DisplayName"

Output:

DisplayName REG_SZ VMware Infrastructure Client 2.5
DisplayName REG_SZ VMware Workstation
DisplayName REG_SZ VMware VI Toolkit (for Windows)

5) Do you want PowerShell version?
In PowerShell just use Get-WmiObject and pipe it like this:

Get-WmiObject Win32_Product ¦ Where {$_.name -match "vmware"} ¦ fl name

Conclusion
It's up to you which method you'd like to use. WMI is very powerful but slow and registry is sometimes hard to parse. As I mentioned before: for this case we used reg.exe because it fits perfectly our needs. Of course for other type of work you will choose different method.

Useful links

No comments: