Speed up windows package lookup - maybe

This commit is contained in:
Kartik Null Cating-Subramanian 2016-08-23 19:37:52 -04:00 committed by Christoph Hartmann
parent 53a545ed8f
commit db032e437e

View file

@ -184,14 +184,27 @@ module Inspec::Resources
end
end
# Determines the installed packages on Windows
# Currently we use 'Get-WmiObject -Class Win32_Product' as a detection method
# TODO: evaluate if alternative methods as proposed by Microsoft are still valid:
# Determines the installed packages on Windows using the Windows package registry entries.
# @see: http://blogs.technet.com/b/heyscriptingguy/archive/2013/11/15/use-powershell-to-find-installed-software.aspx
class WindowsPkg < PkgManagement
def info(package_name)
search_paths = [
'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*',
'HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*'
]
# add 64 bit search paths
if inspec.os.arch == "x86_64"
search_paths << 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*'
search_paths << 'HKCU:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*'
end
# Find the package
cmd = inspec.command("Get-WmiObject -Class Win32_Product | Where-Object {$_.Name -eq '#{package_name}'} | Select-Object -Property Name,Version,Vendor,PackageCode,Caption,Description | ConvertTo-Json")
cmd = inspec.command <<-EOF.gsub /^\s*/, ''
Get-ItemProperty (@("#{search_paths.join('", "')}") | Where-Object { Test-Path $_ }) |
Where-Object { $_.DisplayName -like "#{package_name}*" -or $_.PSChildName -like "#{package_name}" } |
Select-Object -Property DisplayName,DisplayVersion | ConvertTo-Json
EOF
begin
package = JSON.parse(cmd.stdout)
@ -199,10 +212,13 @@ module Inspec::Resources
return nil
end
# What if we match multiple packages? just pick the first one for now.
package = package[0] if package.kind_of?(Array)
{
name: package['Name'],
name: package['DisplayName'],
installed: true,
version: package['Version'],
version: package['DisplayVersion'],
type: 'windows',
}
end