Class: WindowsFeature

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

Overview

check for a Windows feature Usage: describe windows_feature(‘DHCP Server’) do

it{ should be_installed }

end

deprecated serverspec syntax: describe windows_feature(‘IIS-Webserver’) do

it{ should be_installed.by("dism") }

end

describe windows_feature(‘Web-Webserver’) do

it{ should be_installed.by("powershell") }

end

This implementation uses the Get-WindowsFeature commandlet: Get-WindowsFeature | Where-Object -eq ‘XPS Viewer’ -or $_.DisplayName -eq ‘XPS Viewe r’ | Select-Object -Property Name,DisplayName,Description,Installed,InstallState | ConvertTo-Json

"Name":  "XPS-Viewer",
"DisplayName":  "XPS Viewer",
"Description":  "The XPS Viewer is used to read, set permissions for, and digitally sign XPS documents.",
"Installed":  false,
"InstallState":  0

Instance Method Summary collapse

Constructor Details

#initialize(feature) ⇒ WindowsFeature

Returns a new instance of WindowsFeature.



33
34
35
36
37
38
39
# File 'lib/resources/windows_feature.rb', line 33

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[:family] != 'windows'
end

Instance Method Details

#infoObject

returns the package description



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/resources/windows_feature.rb', line 47

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)


42
43
44
# File 'lib/resources/windows_feature.rb', line 42

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

#to_sObject



74
75
76
# File 'lib/resources/windows_feature.rb', line 74

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