Module: Beaker::DSL::Wrappers

Included in:
Beaker::DSL, PSWindows::Exec
Defined in:
lib/beaker/dsl/wrappers.rb

Overview

These are wrappers to equivalent Command objects so that command line actions are executed within an appropriate and configurable environment.

I find most of these adapters of suspicious value and have deprecated many of them.

Instance Method Summary collapse

Instance Method Details

#encode_command(cmd) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Convert the provided command string to Base64 encoded UTF-16LE command

Parameters:

  • cmd (String)

    The command to convert to Base64

Returns:

  • (String)

    The converted string



81
82
83
84
# File 'lib/beaker/dsl/wrappers.rb', line 81

def encode_command(cmd)
  # use strict_encode because linefeeds are not correctly handled in our model
  Base64.strict_encode64(cmd.encode(Encoding::UTF_16LE)).chomp
end

#powershell(command, args = {}) ⇒ Command

Returns a Command object for executing powershell commands on a host

Examples:

Setting the contents of a file

powershell("Set-Content -path 'fu.txt' -value 'fu'")

Using an alternative execution policy

powershell("Set-Content -path 'fu.txt' -value 'fu'", {'ExecutionPolicy' => 'Unrestricted'})

Using an EncodedCommand (defaults to non-encoded)

powershell("Set Content -path 'fu.txt', -value 'fu'", {'EncodedCommand => true})

executing from a file

powershell("", {'-File' => '/path/to/file'})

Parameters:

  • command (String)

    The powershell command to execute

  • args (Hash) (defaults to: {})

    The commandline parameters to be passed to powershell

Returns:



38
39
40
41
42
43
44
45
46
47
48
49
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
# File 'lib/beaker/dsl/wrappers.rb', line 38

def powershell(command, args = {})
  ps_opts = {
    'ExecutionPolicy' => 'Bypass',
    'InputFormat' => 'None',
    'NoLogo' => '',
    'NoProfile' => '',
    'NonInteractive' => '',
  }
  encoded = false
  ps_opts.merge!(args)
  ps_args = []

  # determine if the command should be encoded
  if ps_opts.has_key?('EncodedCommand')
    v = ps_opts.delete('EncodedCommand')
    # encode the commend if v is true, nil or empty
    encoded = v || v.eql?('') || v.nil?
  end

  ps_opts.each do |k, v|
    ps_args << if v.eql?('') or v.nil?
                 "-#{k}"
               else
                 "-#{k} #{v}"
               end
  end

  # may not have a command if executing a file
  if command && !command.empty?
    ps_args << if encoded
                 "-EncodedCommand #{encode_command(command)}"
               else
                 "-Command #{command}"
               end
  end

  Command.new("powershell.exe", ps_args)
end