Class: Train::Extras::PowerShellCommand

Inherits:
CommandWrapperBase show all
Defined in:
lib/train/extras/command_wrapper.rb

Overview

this is required if you run locally on windows, winrm connections provide a PowerShell shell automatically TODO: only activate in local mode

Instance Method Summary collapse

Methods inherited from CommandWrapperBase

#verify

Constructor Details

#initialize(backend, options) ⇒ PowerShellCommand

Returns a new instance of PowerShellCommand.



106
107
108
109
# File 'lib/train/extras/command_wrapper.rb', line 106

def initialize(backend, options)
  @backend = backend
  validate_options(options)
end

Instance Method Details

#encoded(script) ⇒ String

Encodes the script so that it can be passed to the PowerShell –EncodedCommand argument.

Returns:

  • (String)

    The UTF-16LE base64 encoded script



127
128
129
130
# File 'lib/train/extras/command_wrapper.rb', line 127

def encoded(script)
  encoded_script = safe_script(script).encode('UTF-16LE', 'UTF-8')
  Base64.strict_encode64(encoded_script)
end

#run(script) ⇒ Object



111
112
113
114
115
116
117
# File 'lib/train/extras/command_wrapper.rb', line 111

def run(script)
  # wrap the script to ensure we always run it via powershell
  # especially in local mode, we cannot be sure that we get a Powershell
  # we may just get a `cmd`.
  # TODO: we may want to opt for powershell.exe -command instead of `encodeCommand`
  "powershell -encodedCommand #{encoded(safe_script(script))}"
end

#safe_script(script) ⇒ Object

suppress the progress stream from leaking to stderr



120
121
122
# File 'lib/train/extras/command_wrapper.rb', line 120

def safe_script(script)
  "$ProgressPreference='SilentlyContinue';" + script
end

#to_sObject



132
133
134
# File 'lib/train/extras/command_wrapper.rb', line 132

def to_s
  'PowerShell CommandWrapper'
end