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: http://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
416
417
418
419
420
421
422
# 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 WOW6432Node paths for 32-bit apps on 64-bit Windows.
  # Include these unless the system is explicitly 32-bit (i386) — on 32-bit
  # Windows, WOW6432Node does not exist since all apps register under the
  # main Uninstall key.
  # When os.arch is nil (e.g., train falls back to read_cmd_os in a PowerShell
  # WinRM session where %PROCESSOR_ARCHITECTURE% is not expanded), we still
  # include the WOW6432Node paths since virtually all modern Windows systems
  # are 64-bit.
  unless inspec.os.arch == "i386"
    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-ItemProperty (@(\"\#{search_paths.join('\", \"')}\") | Where-Object { Test-Path $_ }) |\n    Where-Object { $_.DisplayName -like \"\#{package_name}\" -or $_.PSChildName -like \"\#{package_name}\" } |\n    Select-Object -Property DisplayName,DisplayVersion | ConvertTo-Json\n  EOF\n\n  # We cannot rely on `exit_status` since PowerShell always exits 0 from the\n  # above command. Instead, if no package is found the output of the command\n  # will be `''` so we can use that to return `{}` to match the behavior of\n  # other package managers.\n  return {} if cmd.stdout == \"\"\n\n  begin\n    package = JSON.parse(cmd.stdout)\n  rescue JSON::ParserError => e\n    raise Inspec::Exceptions::ResourceFailed,\n          \"Failed to parse JSON from PowerShell. Error: \#{e}\"\n  end\n\n  # What if we match multiple packages?  just pick the first one for now.\n  package = package[0] if package.is_a?(Array)\n\n  {\n    name: package[\"DisplayName\"],\n    installed: true,\n    version: package[\"DisplayVersion\"],\n    type: \"windows\",\n    only_version_no: package[\"DisplayVersion\"],\n  }\nend\n".gsub(/^\s*/, "")

#latest_version(package_name) ⇒ Object



424
425
426
427
# File 'lib/inspec/resources/package.rb', line 424

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