0

I'm trying to run a windows_firewall_rule InSpec test against multiple Windows servers, however whilst the Displayname of the rule is 'My Rule' on every machine I want to run it against, the 'Name' field is different on every single server and is in the format "{A9753595-752E-4F26-83EE-E4A7376601A1}". I can get the actual name of the rule on a machine with this Powershell command,

(Get-NetFirewallRule | Where-Object {($_.DisplayName -eq "My Rule") -and ($_.Direction -eq "Inbound")}).Name

But I need a way to store the output in a variable and use it in my control. Using the Display Name that is common to all servers in the control just results in the test failing as the rule not existing. If I use the unique name then the test passes, but that requires the name to be hardcoded and the test to be changed every single time it is run.

Is it at all possible to run a Powershell script in a Chef InSpec control, store the output of that script in a variable and then use that variable in a test within the control?

This is my current test which fails due to using the Display Name rather than the name, so I'd like to substitite 'My Rule' for a variable.

describe windows_firewall_rule('My Rule') do
    it { should exist }
    it { should be_enabled }
    it { should be_inbound }
    it { should be_tcp }
end

Ideally I'd like to do something like this,

Get the name of the Firewall rule (We cannot use Displayname during the test)

script = <<-EOH
    (Get-NetFirewallRule | Where-Object {($_.DisplayName -eq "My Rule") -and ($_.Direction -eq "Inbound")}).Name
EOH

##Windows Firewall Rule checks
describe windows_firewall_rule(script.output) do
    it { should exist }
    it { should be_enabled }
    it { should be_inbound }
    it { should be_tcp }
    its('local_port') { should eq "5666" }
end
Lagamorph
  • 1
  • 3

1 Answers1

0

I achieved it with following code:

firewall_rule_name = powershell('(Get-NetFirewallRule | Where-Object {($_.DisplayName -eq "My Rule") -and ($_.Direction -eq "Inbound")}).Name').stdout.strip

describe windows_firewall_rule(firewall_rule_name) do
  it { should exist }
  it { should be_enabled }
  it { should be_inbound }
  it { should be_tcp }
  its('local_port') { should eq "5666" }
end

Without strip it does not work, because of new line symbol at the end of the output.

inspec version: 4.56.20

whyte624
  • 312
  • 1
  • 15