Class: Inspec::Resources::WindowsFeature

Inherits:
Object
  • Object
show all
Defined in:
lib/resources/windows_feature.rb

Instance Method Summary collapse

Constructor Details

#initialize(feature) ⇒ WindowsFeature

Returns a new instance of WindowsFeature.



40
41
42
43
44
45
46
# File 'lib/resources/windows_feature.rb', line 40

def initialize(feature)
  @feature = feature
  @cache = nil

  # verify that this resource is only supported on Windows
  return skip_resource 'The `windows_feature` resource is not supported on your OS.' if !inspec.os.windows?
end

Instance Method Details

#infoObject

returns the package description



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/resources/windows_feature.rb', line 54

def info
  return @cache if !@cache.nil?
  features_cmd = "Get-WindowsFeature | Where-Object {$_.Name -eq '#{@feature}' -or $_.DisplayName -eq '#{@feature}'} | Select-Object -Property Name,DisplayName,Description,Installed,InstallState | ConvertTo-Json"
  cmd = inspec.command(features_cmd)

  @cache = {
    name: @feature,
    type: 'windows-feature',
  }

  # cannot rely on exit code for now, successful command returns exit code 1
  # return nil if cmd.exit_status != 0
  # try to parse json
  begin
    params = JSON.parse(cmd.stdout)
  rescue JSON::ParserError => _e
    return @cache
  end

  @cache = {
    name: params['Name'],
    description: params['Description'],
    installed: params['Installed'],
    type: 'windows-feature',
  }
end

#installed?(_provider = nil, _version = nil) ⇒ Boolean

returns true if the package is installed

Returns:

  • (Boolean)


49
50
51
# File 'lib/resources/windows_feature.rb', line 49

def installed?(_provider = nil, _version = nil)
  info[:installed] == true
end

#to_sObject



81
82
83
# File 'lib/resources/windows_feature.rb', line 81

def to_s
  "Windows Feature '#{@feature}'"
end