0

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">"]

1 Answer 1

1

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)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.