Hi,
I’ve seen powershell_exec method in release notes, however I can’t find any examples how to use it, and I am completely new to chef therefore having trouble figuring it on my own.
So far I’ve installed latest ChefDK and trying to chef-apply a cookbook with this recipe:
result = powershell_exec("(Get-Item c:\\windows\\system32\\w32time.dll).VersionInfo").result["FileVersion"]
file 'failas' do
path 'C:\temp\aaa.txt'
content "#{result.result}"
end
This results in an error:
[2018-04-24T15:28:14+03:00] FATAL: NoMethodError: undefined method `powershell_exec' for cookbook: (chef-apply cookbook), recipe: (chef-apply recipe) :Chef::Recipe
Did you mean? powershell_out
powershell_out!
You could also use powershell_out mixin. There was already a discussion around this here:
The powershell_out is more expensive approach but since you are shelling out you can parse the stderr and return codes.
Either you choose the powershell_exec or the powershell_out you beed to include their proper mixin:
require "chef/mixin/powershell_out"
include Chef::Mixin::PowershellOut
script = <<-EOH
# your_awsome_ps_script
EOH
result = powershell_out(script)
require "chef/mixin/powershell_exec"
include Chef::Mixin::PowershellExec
ChefDK with Chef 14 is not yet released, so you can’t use powershell_exec in ChefDK yet - but if you install the chef 14 download from https://downloads.chef.io/chef/stable#windows it’ll be available to you. @simark is correct in his note about usage.
In the end this was what produced the result I wanted:
require "chef/mixin/powershell_exec"
result = powershell_exec("(Get-Item c:\\windows\\system32\\w32time.dll).VersionInfo").result["FileVersion"]
file 'failas' do
path 'C:\temp\aaa.txt'
content "#{result}"
end
Glad to hear you managed to solve the issue.
My comments were regarding general class include. Indeed in the recipe dsl there is no include method, for that you would need to try the approach that I have already posted in this thread: Cant read powershell output to variable
What is new to me that there is no need for the include at all in your case…
Just to add, there should be no need for the require statement either in most cases (writing cookbooks) as we pull in everything you need for powershell_exec “universally” to the client.