Class: Formatron::Util::WinRM

Inherits:
Object
  • Object
show all
Defined in:
lib/formatron/util/winrm.rb

Overview

Perform commands on chef nodes over WinRM

Class Method Summary collapse

Class Method Details

.exec(hostname:, administrator_name:, administrator_password:, command:) ⇒ Object

rubocop:disable Metrics/MethodLength



9
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
# File 'lib/formatron/util/winrm.rb', line 9

def self.exec(
  hostname:,
  administrator_name:,
  administrator_password:,
  command:
)
  endpoint = "http://#{hostname}:5985/wsman"
  winrm = ::WinRM::WinRMWebService.new(
    endpoint,
    :negotiate,
    user: administrator_name,
    pass: administrator_password
  )
  output = winrm.create_executor do |executor|
    executor.run_powershell_script(command) do |stdout, stderr|
      stdout.each_line do |line|
        Formatron::LOG.info { line.chomp }
      end unless stdout.nil?
      stderr.each_line do |line|
        Formatron::LOG.warn { line.chomp }
      end unless stderr.nil?
    end
  end
  exitcode = output[:exitcode]
  fail(
    "`#{command}` exited with code #{exitcode}"
  ) unless exitcode == 0
end