Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chef PowerShell Logging/Write-Host

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
like image 459
Matthew Wilson Avatar asked Jul 24 '26 02:07

Matthew Wilson


1 Answers

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.

like image 155
JackChance Avatar answered Jul 25 '26 16:07

JackChance



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!