Class: Chef::Util::Powershell::Cmdlet

Inherits:
Object
  • Object
show all
Includes:
Mixin::WindowsArchitectureHelper
Defined in:
lib/chef/util/powershell/cmdlet.rb

Defined Under Namespace

Classes: CmdletStream

Instance Attribute Summary collapse

Instance Method Summary collapse

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


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_options = {})
  @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 = output_format_options
end

Instance Attribute Details

#output_formatObject (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
85
86
87
# File 'lib/chef/util/powershell/cmdlet.rb', line 50

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)

  json_depth = 5

  if @json_format && @output_format_options.has_key?(:depth)
    json_depth = @output_format_options[:depth]
  end

  json_command = if @json_format
                   " | convertto-json -compress -depth #{json_depth} > #{streams[:json].path}"
                 else
                   ""
                 end
  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)

  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


89
90
91
92
93
94
95
96
97
# File 'lib/chef/util/powershell/cmdlet.rb', line 89

def run!(switches = {}, execution_options = {}, *arguments)
  result = run(switches, execution_options, arguments)

  if ! result.succeeded?
    raise Chef::Exceptions::PowershellCmdletException, "Powershell Cmdlet failed: #{result.stderr}"
  end

  result
end