class Inspec::Resources::WindowsFeature

def info_via_powershell(feature)

def info_via_powershell(feature)
  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)
  feature_info = {}
  # The `Get-WindowsFeature` command is not available on the Windows
  # non-server OS. This attempts to use the `dism` command to get the info.
  if cmd.stderr =~ /The term 'Get-WindowsFeature' is not recognized/
    feature_info[:name] = feature
    feature_info[:error] = "Could not find `Get-WindowsFeature`"
  else
    # We cannot rely on `cmd.exit_status != 0` because by default the
    # command will exit 1 even on success. So, if we cannot parse the JSON
    # we know that the feature is not installed.
    begin
      result = JSON.parse(cmd.stdout)
      feature_info = {
        name: result["Name"],
        description: result["Description"],
        installed: result["Installed"],
      }
    rescue JSON::ParserError => _e
      feature_info[:name] = feature
      feature_info[:installed] = false
    end
  end
  feature_info[:method] = :powershell
  feature_info
end