Class: Inspec::Resources::WindowsPkg

Inherits:
PkgManagement show all
Defined in:
lib/inspec/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, #missing_requirements

Constructor Details

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

Instance Method Details

#info(package_name) ⇒ Object



373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
# File 'lib/inspec/resources/package.rb', line 373

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

  # We cannot rely on `exit_status` since PowerShell always exits 0 from the
  # above command. Instead, if no package is found the output of the command
  # will be `''` so we can use that to return `{}` to match the behavior of
  # other package managers.
  return {} if cmd.stdout == ""

  begin
    package = JSON.parse(cmd.stdout)
  rescue JSON::ParserError => e
    raise Inspec::Exceptions::ResourceFailed,
          "Failed to parse JSON from PowerShell. Error: #{e}"
  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",
    only_version_no: package["DisplayVersion"],
  }
end

#latest_version(package_name) ⇒ Object



417
418
419
420
# File 'lib/inspec/resources/package.rb', line 417

def latest_version(package_name)
  cmd_string = "Get-Package #{package_name} -AllVersions"
  fetch_latest_version(cmd_string)
end