diff --git a/app/models/manageiq/providers/microsoft/infra_manager.rb b/app/models/manageiq/providers/microsoft/infra_manager.rb index 7a08609be5f..cf710f66d1b 100644 --- a/app/models/manageiq/providers/microsoft/infra_manager.rb +++ b/app/models/manageiq/providers/microsoft/infra_manager.rb @@ -24,8 +24,7 @@ def self.raw_connect(connect_params) require 'winrm' connect_params[:operation_timeout] = 1800 - winrm = WinRM::Connection.new(connect_params) - winrm + WinRM::Connection.new(connect_params) end def self.auth_url(hostname, port = nil) diff --git a/app/models/manageiq/providers/microsoft/infra_manager/powershell.rb b/app/models/manageiq/providers/microsoft/infra_manager/powershell.rb index b7e28fb3b42..449e4c88ebe 100644 --- a/app/models/manageiq/providers/microsoft/infra_manager/powershell.rb +++ b/app/models/manageiq/providers/microsoft/infra_manager/powershell.rb @@ -14,14 +14,19 @@ def execute_powershell(connection, script) def run_powershell_script(connection, script) log_header = "MIQ(#{self.class.name}.#{__method__})" script_string = IO.read(script) + results = [] + begin - results = connection.shell(:powershell).run(script_string) - log_dos_error_results(results) - results + with_winrm_shell(connection) do |shell| + results = shell.run(script_string) + log_dos_error_results(results.stderr) + end rescue Errno::ECONNREFUSED => err - $scvmm_log.error "MIQ(#{log_header} Unable to connect to SCVMM. #{err.message})" + $scvmm_log.error "MIQ(#{log_header} Unable to connect to SCVMM: #{err.message})" raise end + + results.stdout end def powershell_results_to_hash(results) @@ -70,6 +75,24 @@ def parse_xml_error_string(str) string << element end end + + def with_winrm_shell(connection, shell_type = :powershell) + shell = connection.shell(shell_type) + yield shell + ensure + shell.close + end + end + + def with_winrm_shell(shell_type = :powershell) + with_provider_connection do |connection| + begin + shell = connection.shell(shell_type) + yield shell + ensure + shell.close + end + end end def run_dos_command(command) @@ -78,15 +101,15 @@ def run_dos_command(command) results = [] _result, timings = Benchmark.realtime_block(:execution) do - with_provider_connection do |connection| - results = connection.shell(:cmd).run(command) - self.class.log_dos_error_results(results) + with_winrm_shell(:cmd) do |shell| + results = shell.run(command) + self.class.log_dos_error_results(results.stderr) end end $scvmm_log.debug("#{log_header} Execute DOS command <#{command}>...Complete - Timings: #{timings}") - results + results.stdout end def run_powershell_script(script) @@ -95,16 +118,16 @@ def run_powershell_script(script) results = [] _result, timings = Benchmark.realtime_block(:execution) do - with_provider_connection do |connection| + with_winrm_shell do |shell| script_string = IO.read(script) - results = connection.shell(:powershell).run(script_string) - self.class.log_dos_error_results(results) + results = shell.run(script_string) + self.class.log_dos_error_results(results.stderr) end end $scvmm_log.debug("#{log_header} Execute Powershell script... Complete - Timings: #{timings}") - results + results.stdout end end end