Passing attributes and variables into Powershell

Hi,
I'm writing an InSpec test with a Powershell script, and attempting to inject attributes (or other defined variables) into the PS1 code. Found an example here, in which somebody is doing likewise with a batch script: https://chef.readthedocs.io/en/latest/resources.html (in this case they defined "extract_path" and called it in the script)

Should I be doing the same for Powershell? I've searched all over and not finding any examples. Here's example of my test...

foo_url = attribute("Foo", description: 'String containing Foo details')
ipaddress = attribute("IPAddress", description: 'String containing ipaddress')

title 'Config Foo Section'

control 'config-footest-1.0' do
impact 0.7
title 'Config FooTest Compliance Section'
desc 'Config FooTest Compliance ...'

hostname = command("hostname").stdout.chop.to_s.downcase
puts "hostname: #{hostname}"

	script = <<-EOH
    $GETRECORD = "#{foo_url}/wapi/v2.0/record:host?ipv4addr=#{ipaddress}"

function readHost {
		$WebRequest = ((Invoke-WebRequest -Uri "$GETRECORD" -Method Get -ContentType "application/json" -UseBasicParsing).Content)
		$WebRequest
}
    Write-Host #{foo_url}
    EOH

	describe powershell(script) do
  		its('stdout') { should include ipaddress }
	end

Thus far, I don't think the variables are getting successfully translated when the script runs....as it just prints out the characters when the test runs. For debugging purposes, I inserted a line: Write-Host ${foo_url}
But it doesn't seem to print the variable. Is there anything else I should try, to help debug this further?

Ah, one difficulty you may be having is that we recently renamed this feature - "attributes" are now called "inputs" (this is because Chef Infra (the config management tool) has a prominent but very different feature also named Attributes, and we wanted to reduce long-term confusion...).

So, you can learn about InSpec Inputs here.
A few things to note:

  • you can call input() anywhere (you don't have to do it at the top of your profile code). input() replaces the attribute() call. You can call it within controls, etc. You can choose to assign its output to a Ruby variable, or use it directly inline.
  • I notice that while you are providing an optional description, you're not providing a value. You can provide a value right in code using the value: option, or there are several ways of providing input values externally, including --input NAME=VALUE, --input-file INPUTS.yaml , or more exotic approaches. If you don't provide a value at all, you may get expected results, like a string indicating no value was set.
  • I'm not a PowerShell expert, but I would start by making sure that the input values are available and working as Ruby variables in InSpec before you try them in PowerShell. You can do that in InSpec - you can test Strings!
control "check an input" do
  describe input("my-input", value: "my-expected-value") do
    it { should cmp "my-expected-value" }
  end
end

While developing your code, check your inputs, and then you can determine if it is an input problem or a problem with interpolation in PowerShell code.

I did check the inputs to confirm they're coming in fine, used the "puts" to print them out...issue seems to be interpolation with PowerShell script portion

I don't think this involves the powershell resource or anything yet. You can check the string interpolation into the script variable (which is what you seem to be reporting is the problem) be adding another puts in between...

Write-Host #{foo_url}
EOH

puts script # <--- here

describe powershell(script) do