3

Is there a way to log to the console from chef when using a powershell_script block.

An oversimplified example:

powershell_script "Something Cool" do
  ignore_failure true
  code <<-EOH
    write-host "Hello World"
  EOH
end

2 Answers 2

1

You want to mixin powershell_out, which reads output from the powershell in the same way shell_out reads from other shells. According to the chef changelog as of client 12.4.0 powershell_out now lives in core chef https://github.com/chef/chef/blob/master/CHANGELOG.md

EDIT: Finally got this to work in my environment. Bare in mind, I'm locked to omnibus version 12.3.0 so your experience may differ.

In order to expose powershell_out you need to do a couple things.

metadata.rb
...
depends 'windows'

<recipe that will be using powershell_out>.rb
...
::Chef::Recipe.send(:include, Chef::Mixin::PowershellOut)
#example usage
should_exist = powershell_out('$true').stdout #=> ['true']

Not exactly a full tutorial but many Bothans died to bring you this information and I hope it's a useful jumping off point. Oh, and doing this method throws this warning:

The powershell_out library in the windows cookbook is deprecated.
Please upgrade to Chef 12.4.0 or later where it is built-in to core chef.

so in 12.4.0+ you should be able to use the powershell_out resource directly.

5
  • 1
    An example on the usage would improve this answer. (and some formatting on the methods names too)
    – Tensibai
    Aug 21, 2015 at 8:18
  • Agreed; but in my environment I'm stuck on 12.3.0 and its only after quite a bit of pain and frustration that I discovered 12.4.0 is where it got pulled into core..
    – JackChance
    Aug 21, 2015 at 18:10
  • IIRC The powershell cookbook brings it to any chef version above 11.0 (as many windows features migrated from Windows and powershell cookbook to core chef client since Chef client 10.8)
    – Tensibai
    Aug 21, 2015 at 19:48
  • That's not a criticism but an idea to improve your answer to something that is really an answer and I don't want to steal rep by doing the search on docs.chef.io based on your answer
    – Tensibai
    Aug 21, 2015 at 19:51
  • Tensibai, you're absolutely correct. Edit now has the information on actually using this. Hope it saves some people out there time!
    – JackChance
    Aug 28, 2015 at 17:15
-4

Since ruby code can be written inside cookbooks, you can use "puts" to log to the console.

1
  • And you think you can call a ruby method within a powershell script ? you should have misread the question.
    – Tensibai
    Aug 21, 2015 at 13:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.