How to pass an array Chef node attribute to powershell_script

This is a very short post on the magic of syntax in Chef + PowerShell .

My goal here is to pass an array of strings from Chef node attribute to powershell_script. The problem is that if I define a string array in ruby it fails with a syntax error in powershell. The answer on how to do it turned out to be fairly simple – if you define the node attribute to be a string using a combination of single and double quotes (see the example below) it is converted into an array in PowerShell code.

Attribute:

default['cookbook-name']['attribute-name'] = "'value1', 'value2', 'value3'"

And recipe:

powershell_script 'my script' do
  code <<-EOH
  $array = #{node['cookbook-name']['attribute-name']}
  ...
  EOH
  action :run
  guard_interpreter :powershell_script
  not_if "..."
end

 
That’s it! Happy cooking 😉

Leave a Reply

Your email address will not be published. Required fields are marked *