Powershell_exec

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!

Hi @AurimasNav,

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.

I have installed the chef 14 from the link and added:

require "chef/mixin/powershell_exec" 
include Chef::Mixin::PowershellExec

to my recipe, now I get the error:

[2018-04-25T08:28:02+03:00] FATAL: LoadError: cannot load such file -- chef/mixin/powershell_exec

P.S. it works fine with powershell_out even without require/include, however I am particulary interested in powershell_exec.

What I needed to do after installing chef 14 client was to use chef module from C:\opscode\chef\modules and not the C:\opscode\chefdk\modules.

I also had to remove include since it was giving me an error:

[2018-04-25T11:55:34+03:00] FATAL: NoMethodError: undefined method `include' for cookbook: (chef-apply cookbook), recipe ...

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

Hi @AurimasNav,

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.

Stuart

This is from chef-client version 14?

Correct, we only ship the interop required with Chef Client 14 upwards. Also note it is built for x64 Windows only.