diff options
Diffstat (limited to 'lib/chef/util/powershell/cmdlet.rb')
-rw-r--r-- | lib/chef/util/powershell/cmdlet.rb | 48 |
1 files changed, 41 insertions, 7 deletions
diff --git a/lib/chef/util/powershell/cmdlet.rb b/lib/chef/util/powershell/cmdlet.rb index 40edbb13c6..47d63a2b85 100644 --- a/lib/chef/util/powershell/cmdlet.rb +++ b/lib/chef/util/powershell/cmdlet.rb @@ -20,7 +20,9 @@ require 'mixlib/shellout' require 'chef/mixin/windows_architecture_helper' require 'chef/util/powershell/cmdlet_result' -class Chef::Util::Powershell +class Chef +class Util +class Powershell class Cmdlet def initialize(node, cmdlet, output_format=nil, output_format_options={}) @output_format = output_format @@ -46,6 +48,10 @@ class Chef::Util::Powershell attr_reader :output_format def run(switches={}, execution_options={}, *arguments) + streams = { :json => CmdletStream.new('json'), + :verbose => CmdletStream.new('verbose'), + } + arguments_string = arguments.join(' ') switches_string = command_switches_string(switches) @@ -56,21 +62,25 @@ class Chef::Util::Powershell json_depth = @output_format_options[:depth] end - json_command = @json_format ? " | convertto-json -compress -depth #{json_depth}" : "" - command_string = "powershell.exe -executionpolicy bypass -noprofile -noninteractive -command \"trap [Exception] {write-error -exception ($_.Exception.Message);exit 1};#{@cmdlet} #{switches_string} #{arguments_string}#{json_command}\";if ( ! $? ) { exit 1 }" + json_command = @json_format ? " | convertto-json -compress -depth #{json_depth} "\ + "> #{streams[:json].path}" : "" + redirections = "4> '#{streams[:verbose].path}'" + command_string = "powershell.exe -executionpolicy bypass -noprofile -noninteractive "\ + "-command \"trap [Exception] {write-error -exception "\ + "($_.Exception.Message);exit 1};#{@cmdlet} #{switches_string} "\ + "#{arguments_string} #{redirections}"\ + "#{json_command}\";if ( ! $? ) { exit 1 }" augmented_options = {:returns => [0], :live_stream => false}.merge(execution_options) command = Mixlib::ShellOut.new(command_string, augmented_options) - os_architecture = "#{ENV['PROCESSOR_ARCHITEW6432']}" == 'AMD64' ? :x86_64 : :i386 - status = nil with_os_architecture(@node) do status = command.run_command end - CmdletResult.new(status, @output_format) + CmdletResult.new(status, streams, @output_format) end def run!(switches={}, execution_options={}, *arguments) @@ -131,6 +141,30 @@ class Chef::Util::Powershell command_switches.join(' ') end + + class CmdletStream + def initialize(name) + @filename = Dir::Tmpname.create(name) {} + ObjectSpace.define_finalizer(self, self.class.destroy(@filename)) + end + + def path + @filename + end + + def read + if File.exist? @filename + File.open(@filename, 'rb:bom|UTF-16LE') do |f| + f.read.encode('UTF-8') + end + end + end + + def self.destroy(name) + proc { File.delete(name) if File.exists? name } + end + end end end - +end +end |