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.



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

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



151
152
153
154
# File 'lib/train/extras/command_wrapper.rb', line 151

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

#run(script) ⇒ Object



135
136
137
138
139
140
141
# File 'lib/train/extras/command_wrapper.rb', line 135

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 -NoProfile -encodedCommand #{encoded(safe_script(script))}"
end

#safe_script(script) ⇒ Object

suppress the progress stream from leaking to stderr



144
145
146
# File 'lib/train/extras/command_wrapper.rb', line 144

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

#to_sObject



156
157
158
# File 'lib/train/extras/command_wrapper.rb', line 156

def to_s
  'PowerShell CommandWrapper'
end