# Powershell\_exec

**URL:** <https://discourse.chef.io/t/powershell-exec/12962>\
**Category:** Chef Infra (archive)\
**Created:** [April 24, 2018, 12:42pm UTC](https://discourse.chef.io/t/powershell-exec/12962 "2018-04-24T12:42:44Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![AurimasNav](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/aurimasnav/32/2246_2.png) [@AurimasNav](https://discourse.chef.io/u/AurimasNav)\
**Post date:** [April 24, 2018, 12:42pm UTC](https://discourse.chef.io/t/powershell-exec/12962/1 "2018-04-24T12:42:44Z")

</div>

Hi,  
I’ve seen powershell\_exec method in [release notes](https://docs.chef.io/release_notes.html), 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!
```

---

<div class="post-metadata">

**Author:** ![simark](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/simark/32/2099_2.png) [@simark](https://discourse.chef.io/u/simark)\
**Post date:** [April 24, 2018, 1:06pm UTC](https://discourse.chef.io/t/powershell-exec/12962/2 "2018-04-24T13:06:34Z")

</div>

Hi @AurimasNav,

You could also use `powershell_out` mixin. There was already a discussion around this here:

> [@Cant read powershell output to variable](https://discourse.chef.io/t/cant-read-powershell-output-to-variable/12103/):
>
> Hi, Problem statement : I want to store powershell cmd output to variable and use the variable in the whole recipe wherever it is required. i tried but not able to do it. kindly help me. powershell\_script 'get-python-version' do code 'version = (Get-WmiObject -Class Win32\_Product | Select-Object -Property Name | Select-String -Pattern "Python") -replace "@{Name=", "" -replace "}", ""' guard\_interpreter :powershell\_script end windows\_package "#{version}" do action :remove end Rega…

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:

```ruby
require "chef/mixin/powershell_out" 
include Chef::Mixin::PowershellOut

script = <<-EOH 
  # your_awsome_ps_script 
EOH 
result = powershell_out(script)

```

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

```

---

<div class="post-metadata">

**Author:** ![thommay](https://avatars.discourse-cdn.com/v4/letter/t/f08c70/32.png) [@thommay](https://discourse.chef.io/u/thommay)\
**Post date:** [April 24, 2018, 1:08pm UTC](https://discourse.chef.io/t/powershell-exec/12962/3 "2018-04-24T13:08:30Z")

</div>

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](https://downloads.chef.io/chef/stable#windows) it’ll be available to you. @simark is correct in his note about usage.

---

<div class="post-metadata">

**Author:** ![AurimasNav](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/aurimasnav/32/2246_2.png) [@AurimasNav](https://discourse.chef.io/u/AurimasNav)\
**Post date:** [April 25, 2018, 5:29am UTC](https://discourse.chef.io/t/powershell-exec/12962/4 "2018-04-25T05:29:10Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![AurimasNav](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/aurimasnav/32/2246_2.png) [@AurimasNav](https://discourse.chef.io/u/AurimasNav)\
**Post date:** [April 25, 2018, 9:00am UTC](https://discourse.chef.io/t/powershell-exec/12962/5 "2018-04-25T09:00:59Z")

</div>

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
```

---

<div class="post-metadata">

**Author:** ![simark](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/simark/32/2099_2.png) [@simark](https://discourse.chef.io/u/simark)\
**Post date:** [April 25, 2018, 9:47am UTC](https://discourse.chef.io/t/powershell-exec/12962/6 "2018-04-25T09:47:57Z")

</div>

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](https://discourse.chef.io/t/cant-read-powershell-output-to-variable/12103/4)  
What is new to me that there is no need for the include at all in your case…

---

<div class="post-metadata">

**Author:** ![stuartpreston](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/stuartpreston/32/1771_2.png) [@stuartpreston](https://discourse.chef.io/u/stuartpreston)\
**Post date:** [April 25, 2018, 10:11am UTC](https://discourse.chef.io/t/powershell-exec/12962/7 "2018-04-25T10:11:18Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![simark](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/simark/32/2099_2.png) [@simark](https://discourse.chef.io/u/simark)\
**Post date:** [April 25, 2018, 11:04am UTC](https://discourse.chef.io/t/powershell-exec/12962/8 "2018-04-25T11:04:09Z")

</div>

This is from chef-client version 14?

---

<div class="post-metadata">

**Author:** ![stuartpreston](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/stuartpreston/32/1771_2.png) [@stuartpreston](https://discourse.chef.io/u/stuartpreston)\
**Post date:** [April 25, 2018, 12:06pm UTC](https://discourse.chef.io/t/powershell-exec/12962/9 "2018-04-25T12:06:51Z")

</div>

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