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.



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

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



53
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
# File 'lib/resources/windows_feature.rb', line 53

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)


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

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

#to_sObject



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

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