class WinRM::FS::Core::CommandExecutor

Executes commands used by the WinRM file management module

Public Class Methods

new(service) click to toggle source
# File lib/winrm-fs/core/command_executor.rb, line 21
def initialize(service)
  @service = service
end

Public Instance Methods

close() click to toggle source
# File lib/winrm-fs/core/command_executor.rb, line 30
def close
  @service.close_shell(@shell) if @shell
  @shell_open = false
end
open() click to toggle source
# File lib/winrm-fs/core/command_executor.rb, line 25
def open
  @shell = @service.open_shell
  @shell_open = true
end
run_cmd(command, arguments = []) click to toggle source
# File lib/winrm-fs/core/command_executor.rb, line 40
def run_cmd(command, arguments = [])
  assert_shell_is_open
  result = nil
  @service.run_command(@shell, command, arguments) do |command_id|
    result = @service.get_command_output(@shell, command_id)
  end
  assert_command_success(command, result)
  result.stdout
end
run_powershell(script) click to toggle source
# File lib/winrm-fs/core/command_executor.rb, line 35
def run_powershell(script)
  assert_shell_is_open
  run_cmd('powershell', ['-encodedCommand', encode_script(safe_script(script))])
end

Private Instance Methods

assert_command_success(command, result) click to toggle source
# File lib/winrm-fs/core/command_executor.rb, line 56
def assert_command_success(command, result)
  return if result[:exitcode] == 0 && result.stderr.length == 0
  fail WinRMUploadError, command + '\n' + result.output
end
assert_shell_is_open() click to toggle source
# File lib/winrm-fs/core/command_executor.rb, line 52
def assert_shell_is_open
  fail 'You must call open before calling any run methods' unless @shell_open
end
encode_script(script) click to toggle source
# File lib/winrm-fs/core/command_executor.rb, line 61
def encode_script(script)
  encoded_script = script.encode('UTF-16LE', 'UTF-8')
  Base64.strict_encode64(encoded_script)
end
safe_script(script) click to toggle source

suppress the progress stream from leaking to stderr

# File lib/winrm-fs/core/command_executor.rb, line 67
def safe_script(script)
  "$ProgressPreference='SilentlyContinue';" + script
end