MrDarkness96
MrDarkness96

Reputation: 57

Powershell_out not saving powershell output to a variable in chef

I'm trying to test PowershellOut in Chef in order to get some values from PS code and then save it into a ruby variable in order to use it within chef code. I have created a very simple scenario in order to test this:

Chef code:

::Chef::Resource::PowershellScript.send(:include,    Chef::Mixin::PowershellOut)

    testPO = <<-EOH
  $varo = "new22"
  $varo
EOH

newvar = powershell_out(testPO)

directory "C:\\Users\\Foo\\Desktop\\#{newvar}" do

  action :delete
end

There are no "red" errors, however I've found out that there is some sort of error code within the variable itself:

directory[C:\Users\foo\Desktop#"<"Mixlib::ShellOut:0x58a8140">"]

Upvotes: 0

Views: 1658

Answers (1)

Tensibai
Tensibai

Reputation: 15784

You should try newvar = powershell_out(testPO).stdout.chomp().

powershell_out works like shell_out which is better documented here.

Quote from the Readme above:

Simple Shellout Invoke find(1) to search for .rb files:

require 'mixlib/shellout'
find = Mixlib::ShellOut.new("find . -name '*.rb'")
find.run_command

If all went well, the results are on stdout

puts find.stdout

Keep in mind in chef recipe you don't need to require 'mixlib/shellout' as chef already did it for you and shell_out is a a kind of helper for Mixlib::ShellOut.new().

Your method to include powershell_out in the recipe sounds fine (I'm unsure it is neede but that depends on your version of chef client)

Upvotes: 1

Related Questions