Class: Specinfra::Backend::Winrm

Inherits:
Base
  • Object
show all
Includes:
PowerShell::ScriptHelper
Defined in:
lib/specinfra/backend/winrm.rb

Instance Method Summary collapse

Methods included from PowerShell::ScriptHelper

#add_pre_command, #build_command, #check_running, #create_script, #encode_script

Methods inherited from Base

clear, #command, #get_config, #host_inventory, #initialize, instance, #set_config, #set_example, #stderr_handler=, #stdout_handler=

Constructor Details

This class inherits a constructor from Specinfra::Backend::Base

Instance Method Details

#os_infoObject



6
7
8
# File 'lib/specinfra/backend/winrm.rb', line 6

def os_info
  { :family => 'windows', :release => nil, :arch => nil }
end

#run_command(cmd, opts = {}) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/specinfra/backend/winrm.rb', line 10

def run_command(cmd, opts={})
  script = create_script(cmd)
  winrm = get_config(:winrm)

  stdout, stderr = ''
  exitcode = 0

  if Gem.loaded_specs['winrm'].version < Gem::Version.create('2.0')
    # Use winrm V1 API
    result = winrm.powershell(script)
    stdout, stderr = [:stdout, :stderr].map do |s|
      result[:data].select {|item| item.key? s}.map {|item| item[s]}.join
    end
    exitcode = result[:exitcode]
  else
    # Use winrm V2 API
    winrm.shell(:powershell) do |shell|
      result = shell.run(script)
      stdout = result.stdout.to_s
      stderr = result.stderr.to_s
      exitcode = result.exitcode
    end
  end

  exitcode = 1 if exitcode == 0 and !stderr.empty?

  if @example
    @example.[:command] = script
    @example.[:stdout]  = stdout + stderr
  end

  CommandResult.new :stdout => stdout, :stderr => stderr, :exit_status => exitcode
end