mirror of
https://github.com/inspec/inspec
synced 2025-02-17 06:28:40 +00:00
Speed up windows package lookup - maybe
This commit is contained in:
parent
53a545ed8f
commit
db032e437e
1 changed files with 22 additions and 6 deletions
|
@ -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
|
||||
|
|
Loading…
Add table
Reference in a new issue