lib/soap/winrm_service.rb in winrm-0.0.2 vs lib/soap/winrm_service.rb in winrm-0.0.3
- old
+ new
@@ -151,31 +151,28 @@
end
# Get the Output of the given shell and command
# @param [String] shell_id The shell id on the remote machine. See #open_shell
# @param [String] command_id The command id on the remote machine. See #run_command
- # @return [Hash] :stdout and :stderr
+ # @return [Hash] Returns a Hash with a key :exitcode and :data. Data is an Array of Hashes where the cooresponding key
+ # is either :stdout or :stderr. The reason it is in an Array so so we can get the output in the order it ocurrs on
+ # the console.
def get_command_output(shell_id, command_id)
header = {}.merge(resource_uri_cmd).merge(action_receive).merge(selector_shell_id(shell_id))
# Get Command Output
resp = invoke("#{NS_WIN_SHELL}:Receive", {:soap_action => :auto, :http_options => nil, :soap_header => header}) do |rec|
rec.add("#{NS_WIN_SHELL}:DesiredStream",'stdout stderr') do |ds|
ds.set_attr('CommandId', command_id)
end
end
- cmd_stdout = ''
- cmd_stderr = ''
- (resp/"//*[@Name='stdout']").each do |n|
+ output = {:data => []}
+ (resp/"//#{NS_WIN_SHELL}:Stream").each do |n|
next if n.to_s.nil?
- cmd_stdout << Base64.decode64(n.to_s)
+ output[:data] << {n['Name'].to_sym => Base64.decode64(n.to_s)}
end
- (resp/"//*[@Name='stderr']").each do |n|
- next if n.to_s.nil?
- cmd_stderr << Base64.decode64(n.to_s)
- end
# We may need to get additional output if the stream has not finished.
# The CommandState will change from Running to Done like so:
# @example
# from...
@@ -183,16 +180,17 @@
# to...
# <rsp:CommandState CommandId="495C3B09-E0B0-442A-9958-83B529F76C2C" State="http://schemas.microsoft.com/wbem/wsman/1/windows/shell/CommandState/Done">
# <rsp:ExitCode>0</rsp:ExitCode>
# </rsp:CommandState>
if((resp/"//#{NS_WIN_SHELL}:ExitCode").empty?)
- more_out = get_command_output(shell_id,command_id)
- cmd_stdout << more_out[:stdout]
- cmd_stderr << more_out[:stderr]
+ output.merge!(get_command_output(shell_id,command_id)) do |key, old_data, new_data|
+ old_data += new_data
+ end
+ else
+ output[:exitcode] = (resp/"//#{NS_WIN_SHELL}:ExitCode").first.to_i
end
-
- {:stdout => cmd_stdout, :stderr => cmd_stderr}
+ output
end
# Clean-up after a command.
# @see #run_command
# @param [String] shell_id The shell id on the remote machine. See #open_shell
@@ -216,16 +214,32 @@
# Delete the Shell reference
resp = invoke(:nil_body, {:soap_action => nil, :soap_body => true, :http_options => nil, :soap_header => header})
true
end
+ # Run a CMD command
+ # @param [String] command The command to run on the remote system
+ # @return [Hash] :stdout and :stderr
+ def run_cmd(command)
+ shell_id = open_shell
+ command_id = run_command(shell_id, command)
+ command_output = get_command_output(shell_id, command_id)
+ cleanup_command(shell_id, command_id)
+ close_shell(shell_id)
+ command_output
+ end
+
+
# Run a Powershell script that resides on the local box.
# @param [String] script_file The string representing the path to a Powershell script
# @return [Hash] :stdout and :stderr
def run_powershell_script(script_file)
script = File.read(script_file)
- script = script.chars.to_a.join("\x00").chomp.encode('ASCII-8BIT')
- script = Base64.strict_encode64(script)
+ script = script.chars.to_a.join("\x00").chomp
+ if(defined?(script.encode))
+ script = script.encode('ASCII-8BIT')
+ end
+ script = Base64.encode64(script)
shell_id = open_shell
command_id = run_command(shell_id, "powershell -encodedCommand #{script}")
command_output = get_command_output(shell_id, command_id)
cleanup_command(shell_id, command_id)