# Cant read powershell output to variable

**URL:** <https://discourse.chef.io/t/cant-read-powershell-output-to-variable/12103>\
**Category:** Chef Infra (archive)\
**Created:** [December 12, 2017, 6:41am UTC](https://discourse.chef.io/t/cant-read-powershell-output-to-variable/12103 "2017-12-12T06:41:06Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![vagga06](https://avatars.discourse-cdn.com/v4/letter/v/54ee81/32.png) [@vagga06](https://discourse.chef.io/u/vagga06)\
**Post date:** [December 12, 2017, 6:41am UTC](https://discourse.chef.io/t/cant-read-powershell-output-to-variable/12103/1 "2017-12-12T06:41:06Z")

</div>

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

```

Regards,  
vagga06

---

<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:** [December 12, 2017, 11:17am UTC](https://discourse.chef.io/t/cant-read-powershell-output-to-variable/12103/2 "2017-12-12T11:17:53Z")

</div>

HI @vagga06,

The `version` variable only lives within the code block of your `powershell_script` resource. It will not be a global variable and you will not be able to reference it by other resources.  
What you are looking for is the `powershell_out` mixin

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

script = <<-EOH
 # your_awsome_ps_script
EOH

result = powershell_out(script)

```

The source code:

> <https://github.com/chef/chef/blob/master/lib/chef/mixin/powershell_out.rb>

---

<div class="post-metadata">

**Author:** ![vagga06](https://avatars.discourse-cdn.com/v4/letter/v/54ee81/32.png) [@vagga06](https://discourse.chef.io/u/vagga06)\
**Post date:** [December 12, 2017, 12:07pm UTC](https://discourse.chef.io/t/cant-read-powershell-output-to-variable/12103/3 "2017-12-12T12:07:46Z")

</div>

Thank you @simark.

after adding above code , i am getting below error.

> ## NoMethodError
> 
> undefined method `include' for cookbook: python2.7.12, recipe: install :Chef::Recipe

Modified code :

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

script = <<-EOH
     	(Get-WmiObject -Class Win32_Product | Select-Object -Property Name | Select-String -Pattern "Python") -replace "@{Name=", "" -replace "}", ""
    EOH

result = powershell_out(script)

windows_package "#{result}" do
  action :remove
end 

```

is there anything i need to add, kindly suggest

Regards,  
Vagga06

---

<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:** [December 12, 2017, 12:51pm UTC](https://discourse.chef.io/t/cant-read-powershell-output-to-variable/12103/4 "2017-12-12T12:51:41Z")

</div>

Since you are in instance of a `Chef::Recipe` and the `include` is not part of the DSL you have to use

```ruby
::Chef::Recipe.send(:include, Chef::Mixin::PowershellOut)

```

instead of

```ruby
include Chef::Mixin::PowershellOut

```

further information:

> <https://github.com/chef/chef/issues/2234>
>
> A lot of people get trolled by the fact that Chef recipes look like regular Ruby, so they think they can...

---

<div class="post-metadata">

**Author:** ![spuder](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/spuder/32/1382_2.png) [@spuder](https://discourse.chef.io/u/spuder)\
**Post date:** [December 12, 2017, 4:23pm UTC](https://discourse.chef.io/t/cant-read-powershell-output-to-variable/12103/5 "2017-12-12T16:23:30Z")

</div>

Depending on how python was installed, ohai may already know the package version.

```
ohai packages
{
  "7-Zip 16.04 (x64)": {
    "version": "16.04",
    "publisher": "Igor Pavlov"
  },
  "Git version 2.15.0": {
    "version": "2.15.0",
    "publisher": "The Git Development Community",
    "installdate": "20171109"
  },
  "ConEmu 171025.x64": {
    "version": "11.171.0250",
    "publisher": "ConEmu-Maximus5",
    "installdate": "20171110"
  },
  "Chef Client v12.21.20": {
    "version": "12.21.20.1",
    "publisher": "Chef Software, Inc.",
    "installdate": "20171025"
  }
}

```

You can access the packages attributes like this

```
node['packages']['Git version 2.15.0']['version']
```

---

<div class="post-metadata">

**Author:** ![joerod](https://avatars.discourse-cdn.com/v4/letter/j/df788c/32.png) [@joerod](https://discourse.chef.io/u/joerod)\
**Post date:** [December 13, 2017, 1:16am UTC](https://discourse.chef.io/t/cant-read-powershell-output-to-variable/12103/6 "2017-12-13T01:16:57Z")

</div>

I believe using Ohai is the best way to do this however using PowerShell in this way like the OP wants to is not a bad idea for edge cases and something that I’ve done. In addition if you’re not great with Ruby this might be useful.

---

<div class="post-metadata">

**Author:** ![vagga06](https://avatars.discourse-cdn.com/v4/letter/v/54ee81/32.png) [@vagga06](https://discourse.chef.io/u/vagga06)\
**Post date:** [December 15, 2017, 7:13am UTC](https://discourse.chef.io/t/cant-read-powershell-output-to-variable/12103/7 "2017-12-15T07:13:23Z")

</div>

Hi,

Thank you for your reply 😄

I am trying to find the version of python installed (manual) in windows server and compare with the one which i am going to install through chef. if versions are different then chef will uninstall old python and install specified version in cookbook.

I need to find out the old version of python and remove it from windows machine, as i mentioned in the above thread , i am not able to read the output (old python version) into variable from powershell and use that variable to remove package.

Below is my installation script , if possible please modify it and help.

```
powershell_script 'check-python-status' do
    code "$true"
    flags '-NoProfile -NoLogo -NonInteractive -ExecutionPolicy Bypass'
    guard_interpreter :powershell_script
    only_if 'If ((Get-WmiObject -Class Win32_Product | Where-Object { $_.Name -eq "Python 2.7.12 (64-bit)"}) -eq $null) { Return $True } Return $False'
    notifies :install, 'windows_package node['python2.7.12']['package_name']', :immediate
end

windows_package node['python2.7.12']['package_name'] do
  source node['python2.7.12']['url']
  checksum "#{node['python2.7.12']['checksum']}"
end

#Add python installation path to environment variable 'path'
env 'Path' do
   delim ";"
   value "#{node['python2.7.12']['path']}"
   :modify
end

#add python scirpts path to environment variable 'path'  
env 'Path' do
   delim ";"
   value "#{node['python2.7.12']['path']}\\Scripts"
   :modify
end
```
