Class: Inspec::Resources::WindowsPkg

Inherits:
PkgManagement show all
Defined in:
lib/resources/package.rb

Overview

Determines the installed packages on Windows using the Windows package registry entries. @see: blogs.technet.com/b/heyscriptingguy/archive/2013/11/15/use-powershell-to-find-installed-software.aspx

Instance Attribute Summary

Attributes inherited from PkgManagement

#inspec

Instance Method Summary collapse

Methods inherited from PkgManagement

#initialize

Constructor Details

This class inherits a constructor from Inspec::Resources::PkgManagement

Instance Method Details

#info(package_name) ⇒ Object



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/resources/package.rb', line 194

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 <<-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)
  rescue JSON::ParserError => _e
    return nil
  end

  # What if we match multiple packages?  just pick the first one for now.
  package = package[0] if package.is_a?(Array)

  {
    name: package['DisplayName'],
    installed: true,
    version: package['DisplayVersion'],
    type: 'windows',
  }
end