Class: Chef::Util::Powershell::Cmdlet
- Inherits:
-
Object
- Object
- Chef::Util::Powershell::Cmdlet
- Includes:
- Mixin::WindowsArchitectureHelper
- Defined in:
- lib/chef/util/powershell/cmdlet.rb
Defined Under Namespace
Classes: CmdletStream
Instance Attribute Summary collapse
-
#output_format ⇒ Object
readonly
Returns the value of attribute output_format.
Instance Method Summary collapse
-
#initialize(node, cmdlet, output_format = nil, output_format_options = {}) ⇒ Cmdlet
constructor
A new instance of Cmdlet.
- #run(switches = {}, execution_options = {}, *arguments) ⇒ Object
- #run!(switches = {}, execution_options = {}, *arguments) ⇒ Object
Methods included from Mixin::WindowsArchitectureHelper
#assert_valid_windows_architecture!, #disable_wow64_file_redirection, #forced_32bit_override_required?, #is_i386_process_on_x86_64_windows?, #node_supports_windows_architecture?, #node_windows_architecture, #restore_wow64_file_redirection, #valid_windows_architecture?, #with_os_architecture, #wow64_architecture_override_required?, #wow64_directory
Constructor Details
#initialize(node, cmdlet, output_format = nil, output_format_options = {}) ⇒ Cmdlet
Returns a new instance of Cmdlet.
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/chef/util/powershell/cmdlet.rb', line 27 def initialize(node, cmdlet, output_format = nil, = {}) @output_format = output_format @node = node case output_format when nil @json_format = false when :json @json_format = true when :text @json_format = false when :object @json_format = true else raise ArgumentError, "Invalid output format #{output_format} specified" end @cmdlet = cmdlet @output_format_options = end |
Instance Attribute Details
#output_format ⇒ Object (readonly)
Returns the value of attribute output_format.
48 49 50 |
# File 'lib/chef/util/powershell/cmdlet.rb', line 48 def output_format @output_format end |
Instance Method Details
#run(switches = {}, execution_options = {}, *arguments) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/chef/util/powershell/cmdlet.rb', line 50 def run(switches = {}, = {}, *arguments) streams = { :json => CmdletStream.new("json"), :verbose => CmdletStream.new("verbose"), } arguments_string = arguments.join(" ") switches_string = command_switches_string(switches) json_depth = 5 if @json_format && @output_format_options.has_key?(:depth) json_depth = @output_format_options[:depth] end 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 }" = { :returns => [0], :live_stream => false }.merge() command = Mixlib::ShellOut.new(command_string, ) status = nil with_os_architecture(@node) do status = command.run_command end CmdletResult.new(status, streams, @output_format) end |
#run!(switches = {}, execution_options = {}, *arguments) ⇒ Object
86 87 88 89 90 91 92 93 94 |
# File 'lib/chef/util/powershell/cmdlet.rb', line 86 def run!(switches = {}, = {}, *arguments) result = run(switches, , arguments) if ! result.succeeded? raise Chef::Exceptions::PowershellCmdletException, "Powershell Cmdlet failed: #{result.stderr}" end result end |