winrm 2.2.3 → 2.3.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (127) hide show
  1. checksums.yaml +5 -5
  2. data/README.md +1 -1
  3. data/bin/rwinrm +90 -97
  4. data/lib/winrm/connection.rb +84 -86
  5. data/lib/winrm/connection_opts.rb +90 -91
  6. data/lib/winrm/exceptions.rb +14 -2
  7. data/lib/winrm/http/response_handler.rb +127 -96
  8. data/lib/winrm/http/transport.rb +462 -427
  9. data/lib/winrm/http/transport_factory.rb +1 -5
  10. data/lib/winrm/output.rb +1 -2
  11. data/lib/winrm/psrp/fragment.rb +0 -2
  12. data/lib/winrm/psrp/message.rb +128 -130
  13. data/lib/winrm/psrp/message_data/base.rb +0 -2
  14. data/lib/winrm/psrp/message_data/error_record.rb +2 -2
  15. data/lib/winrm/psrp/message_data/pipeline_host_call.rb +0 -2
  16. data/lib/winrm/psrp/message_data/pipeline_output.rb +48 -54
  17. data/lib/winrm/psrp/message_data/pipeline_state.rb +0 -2
  18. data/lib/winrm/psrp/message_data/runspacepool_host_call.rb +0 -2
  19. data/lib/winrm/psrp/message_data/runspacepool_state.rb +0 -2
  20. data/lib/winrm/psrp/message_data/session_capability.rb +0 -2
  21. data/lib/winrm/psrp/message_data.rb +0 -2
  22. data/lib/winrm/psrp/message_defragmenter.rb +2 -2
  23. data/lib/winrm/psrp/message_factory.rb +16 -5
  24. data/lib/winrm/psrp/message_fragmenter.rb +1 -3
  25. data/lib/winrm/psrp/powershell_output_decoder.rb +142 -144
  26. data/lib/winrm/psrp/receive_response_reader.rb +3 -5
  27. data/lib/winrm/psrp/uuid.rb +1 -2
  28. data/lib/winrm/shells/base.rb +7 -4
  29. data/lib/winrm/shells/cmd.rb +63 -65
  30. data/lib/winrm/shells/power_shell.rb +207 -202
  31. data/lib/winrm/shells/retryable.rb +44 -45
  32. data/lib/winrm/shells/shell_factory.rb +0 -2
  33. data/lib/winrm/version.rb +1 -3
  34. data/lib/winrm/wsmv/base.rb +0 -2
  35. data/lib/winrm/wsmv/cleanup_command.rb +1 -2
  36. data/lib/winrm/wsmv/close_shell.rb +1 -2
  37. data/lib/winrm/wsmv/command.rb +2 -3
  38. data/lib/winrm/wsmv/command_output.rb +2 -3
  39. data/lib/winrm/wsmv/command_output_decoder.rb +1 -2
  40. data/lib/winrm/wsmv/configuration.rb +0 -2
  41. data/lib/winrm/wsmv/create_pipeline.rb +0 -2
  42. data/lib/winrm/wsmv/create_shell.rb +2 -6
  43. data/lib/winrm/wsmv/header.rb +213 -215
  44. data/lib/winrm/wsmv/init_runspace_pool.rb +96 -95
  45. data/lib/winrm/wsmv/iso8601_duration.rb +0 -2
  46. data/lib/winrm/wsmv/keep_alive.rb +0 -2
  47. data/lib/winrm/wsmv/receive_response_reader.rb +128 -126
  48. data/lib/winrm/wsmv/send_data.rb +0 -2
  49. data/lib/winrm/wsmv/soap.rb +0 -2
  50. data/lib/winrm/wsmv/wql_pull.rb +54 -56
  51. data/lib/winrm/wsmv/wql_query.rb +98 -99
  52. data/lib/winrm/wsmv/write_stdin.rb +0 -2
  53. data/lib/winrm.rb +3 -5
  54. metadata +81 -135
  55. data/.gitignore +0 -10
  56. data/.rubocop.yml +0 -26
  57. data/.travis.yml +0 -11
  58. data/Gemfile +0 -3
  59. data/Rakefile +0 -34
  60. data/Vagrantfile +0 -6
  61. data/WinrmAppveyor.psm1 +0 -32
  62. data/appveyor.yml +0 -51
  63. data/changelog.md +0 -128
  64. data/preamble +0 -17
  65. data/tests/integration/auth_timeout_spec.rb +0 -18
  66. data/tests/integration/cmd_spec.rb +0 -131
  67. data/tests/integration/config-example.yml +0 -16
  68. data/tests/integration/issue_59_spec.rb +0 -26
  69. data/tests/integration/powershell_spec.rb +0 -165
  70. data/tests/integration/spec_helper.rb +0 -65
  71. data/tests/integration/transport_spec.rb +0 -99
  72. data/tests/integration/wql_spec.rb +0 -34
  73. data/tests/matchers.rb +0 -60
  74. data/tests/spec/configuration_spec.rb +0 -184
  75. data/tests/spec/connection_spec.rb +0 -39
  76. data/tests/spec/exception_spec.rb +0 -50
  77. data/tests/spec/http/transport_factory_spec.rb +0 -68
  78. data/tests/spec/http/transport_spec.rb +0 -44
  79. data/tests/spec/output_spec.rb +0 -127
  80. data/tests/spec/psrp/fragment_spec.rb +0 -62
  81. data/tests/spec/psrp/message_data/base_spec.rb +0 -13
  82. data/tests/spec/psrp/message_data/error_record_spec.rb +0 -41
  83. data/tests/spec/psrp/message_data/pipeline_host_call_spec.rb +0 -25
  84. data/tests/spec/psrp/message_data/pipeline_output_spec.rb +0 -32
  85. data/tests/spec/psrp/message_data/pipeline_state_spec.rb +0 -40
  86. data/tests/spec/psrp/message_data/runspace_pool_host_call_spec.rb +0 -25
  87. data/tests/spec/psrp/message_data/runspacepool_state_spec.rb +0 -16
  88. data/tests/spec/psrp/message_data/session_capability_spec.rb +0 -30
  89. data/tests/spec/psrp/message_data_spec.rb +0 -35
  90. data/tests/spec/psrp/message_defragmenter_spec.rb +0 -47
  91. data/tests/spec/psrp/message_fragmenter_spec.rb +0 -105
  92. data/tests/spec/psrp/powershell_output_decoder_spec.rb +0 -100
  93. data/tests/spec/psrp/psrp_message_spec.rb +0 -70
  94. data/tests/spec/psrp/recieve_response_reader_spec.rb +0 -172
  95. data/tests/spec/psrp/uuid_spec.rb +0 -28
  96. data/tests/spec/response_handler_spec.rb +0 -61
  97. data/tests/spec/shells/base_spec.rb +0 -225
  98. data/tests/spec/shells/cmd_spec.rb +0 -75
  99. data/tests/spec/shells/powershell_spec.rb +0 -175
  100. data/tests/spec/spec_helper.rb +0 -47
  101. data/tests/spec/stubs/clixml/error_record.xml.erb +0 -84
  102. data/tests/spec/stubs/clixml/pipeline_state.xml.erb +0 -88
  103. data/tests/spec/stubs/responses/get_command_output_response.xml.erb +0 -13
  104. data/tests/spec/stubs/responses/get_command_output_response_not_done.xml.erb +0 -10
  105. data/tests/spec/stubs/responses/get_powershell_keepalive_response.xml.erb +0 -10
  106. data/tests/spec/stubs/responses/get_powershell_output_response.xml.erb +0 -12
  107. data/tests/spec/stubs/responses/get_powershell_output_response_not_done.xml.erb +0 -9
  108. data/tests/spec/stubs/responses/open_shell_v1.xml +0 -19
  109. data/tests/spec/stubs/responses/open_shell_v2.xml +0 -20
  110. data/tests/spec/stubs/responses/soap_fault_v1.xml +0 -36
  111. data/tests/spec/stubs/responses/soap_fault_v2.xml +0 -42
  112. data/tests/spec/stubs/responses/wmi_error_v2.xml +0 -41
  113. data/tests/spec/wsmv/cleanup_command_spec.rb +0 -22
  114. data/tests/spec/wsmv/close_shell_spec.rb +0 -17
  115. data/tests/spec/wsmv/command_output_decoder_spec.rb +0 -37
  116. data/tests/spec/wsmv/command_output_spec.rb +0 -45
  117. data/tests/spec/wsmv/command_spec.rb +0 -19
  118. data/tests/spec/wsmv/configuration_spec.rb +0 -17
  119. data/tests/spec/wsmv/create_pipeline_spec.rb +0 -31
  120. data/tests/spec/wsmv/create_shell_spec.rb +0 -38
  121. data/tests/spec/wsmv/init_runspace_pool_spec.rb +0 -36
  122. data/tests/spec/wsmv/keep_alive_spec.rb +0 -21
  123. data/tests/spec/wsmv/receive_response_reader_spec.rb +0 -123
  124. data/tests/spec/wsmv/send_data_spec.rb +0 -30
  125. data/tests/spec/wsmv/wql_query_spec.rb +0 -13
  126. data/tests/spec/wsmv/write_stdin_spec.rb +0 -22
  127. data/winrm.gemspec +0 -47
@@ -1,5 +1,3 @@
1
- # encoding: UTF-8
2
- #
3
1
  # Copyright 2010 Dan Wanek <dan.wanek@gmail.com>
4
2
  #
5
3
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -1,5 +1,3 @@
1
- # -*- encoding: utf-8 -*-
2
- #
3
1
  # Copyright 2016 Matt Wrock <matt@mattwrock.com>
4
2
  #
5
3
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -1,126 +1,128 @@
1
- # -*- encoding: utf-8 -*-
2
- #
3
- # Copyright 2016 Shawn Neal <sneal@sneal.net>
4
- #
5
- # Licensed under the Apache License, Version 2.0 (the "License");
6
- # you may not use this file except in compliance with the License.
7
- # You may obtain a copy of the License at
8
- #
9
- # http://www.apache.org/licenses/LICENSE-2.0
10
- #
11
- # Unless required by applicable law or agreed to in writing, software
12
- # distributed under the License is distributed on an "AS IS" BASIS,
13
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
- # See the License for the specific language governing permissions and
15
- # limitations under the License.
16
-
17
- require_relative 'soap'
18
- require_relative 'header'
19
- require_relative 'command_output_decoder'
20
- require_relative '../output'
21
-
22
- module WinRM
23
- module WSMV
24
- # Class for reading wsmv Receive_Response messages
25
- class ReceiveResponseReader
26
- include WinRM::WSMV::SOAP
27
- include WinRM::WSMV::Header
28
-
29
- # Creates a new ReceiveResponseReader
30
- # @param transport [HttpTransport] The WinRM SOAP transport
31
- # @param logger [Logger] The logger to log diagnostic messages to
32
- def initialize(transport, logger)
33
- @transport = transport
34
- @logger = logger
35
- @output_decoder = CommandOutputDecoder.new
36
- end
37
-
38
- attr_reader :logger
39
-
40
- # Reads streams and returns decoded output
41
- # @param wsmv_message [WinRM::WSMV::Base] A wsmv message to send to endpoint
42
- # @yieldparam [string] standard out response text
43
- # @yieldparam [string] standard error response text
44
- # @yieldreturn [WinRM::Output] The command output
45
- def read_output(wsmv_message)
46
- with_output do |output|
47
- read_response(wsmv_message, true) do |stream, doc|
48
- handled_out = handle_stream(stream, output, doc)
49
- yield handled_out if handled_out && block_given?
50
- end
51
- end
52
- end
53
-
54
- # Reads streams sent in one or more receive response messages
55
- # @param wsmv_message [WinRM::WSMV::Base] A wsmv message to send to endpoint
56
- # @param wait_for_done_state whether to poll for a CommandState of Done
57
- # @yieldparam [Hash] Hash representation of stream with type and text
58
- # @yieldparam [REXML::Document] Complete SOAP envelope returned to wsmv_message
59
- def read_response(wsmv_message, wait_for_done_state = false)
60
- resp_doc = nil
61
- until command_done?(resp_doc, wait_for_done_state)
62
- logger.debug('[WinRM] Waiting for output...')
63
- resp_doc = send_get_output_message(wsmv_message.build)
64
- logger.debug('[WinRM] Processing output')
65
- read_streams(resp_doc) do |stream|
66
- yield stream, resp_doc
67
- end
68
- end
69
- end
70
-
71
- protected
72
-
73
- def with_output
74
- output = WinRM::Output.new
75
- yield output
76
- output.exitcode ||= 0
77
- output
78
- end
79
-
80
- private
81
-
82
- def handle_stream(stream, output, resp_doc)
83
- decoded_text = @output_decoder.decode(stream[:text])
84
- return unless decoded_text
85
-
86
- out = { stream[:type] => decoded_text }
87
- output << out
88
- if (code = REXML::XPath.first(resp_doc, "//#{NS_WIN_SHELL}:ExitCode"))
89
- output.exitcode = code.text.to_i
90
- end
91
- [out[:stdout], out[:stderr]]
92
- end
93
-
94
- def send_get_output_message(message)
95
- @transport.send_request(message)
96
- rescue WinRMWSManFault => e
97
- # If no output is available before the wsman:OperationTimeout expires,
98
- # the server MUST return a WSManFault with the Code attribute equal to
99
- # 2150858793. When the client receives this fault, it SHOULD issue
100
- # another Receive request.
101
- # http://msdn.microsoft.com/en-us/library/cc251676.aspx
102
- raise unless e.fault_code == '2150858793'
103
-
104
- logger.debug('[WinRM] retrying receive request after timeout')
105
- retry
106
- end
107
-
108
- def command_done?(resp_doc, wait_for_done_state)
109
- return false unless resp_doc
110
- return true unless wait_for_done_state
111
-
112
- REXML::XPath.match(
113
- resp_doc,
114
- "//*[@State='http://schemas.microsoft.com/wbem/wsman/1/windows/shell/" \
115
- "CommandState/Done']").any?
116
- end
117
-
118
- def read_streams(response_document)
119
- REXML::XPath.match(response_document, "//#{NS_WIN_SHELL}:Stream").each do |stream|
120
- next if stream.text.nil? || stream.text.empty?
121
- yield type: stream.attributes['Name'].to_sym, text: stream.text
122
- end
123
- end
124
- end
125
- end
126
- end
1
+ # Copyright 2016 Shawn Neal <sneal@sneal.net>
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ require_relative 'soap'
16
+ require_relative 'header'
17
+ require_relative 'command_output_decoder'
18
+ require_relative '../output'
19
+
20
+ module WinRM
21
+ module WSMV
22
+ # Class for reading wsmv Receive_Response messages
23
+ class ReceiveResponseReader
24
+ include WinRM::WSMV::SOAP
25
+ include WinRM::WSMV::Header
26
+
27
+ # Creates a new ReceiveResponseReader
28
+ # @param transport [HttpTransport] The WinRM SOAP transport
29
+ # @param logger [Logger] The logger to log diagnostic messages to
30
+ def initialize(transport, logger)
31
+ @transport = transport
32
+ @logger = logger
33
+ @output_decoder = CommandOutputDecoder.new
34
+ end
35
+
36
+ attr_reader :logger
37
+
38
+ # Reads streams and returns decoded output
39
+ # @param wsmv_message [WinRM::WSMV::Base] A wsmv message to send to endpoint
40
+ # @yieldparam [string] standard out response text
41
+ # @yieldparam [string] standard error response text
42
+ # @yieldreturn [WinRM::Output] The command output
43
+ def read_output(wsmv_message)
44
+ with_output do |output|
45
+ read_response(wsmv_message, true) do |stream, doc|
46
+ handled_out = handle_stream(stream, output, doc)
47
+ yield handled_out if handled_out && block_given?
48
+ end
49
+ end
50
+ end
51
+
52
+ # Reads streams sent in one or more receive response messages
53
+ # @param wsmv_message [WinRM::WSMV::Base] A wsmv message to send to endpoint
54
+ # @param wait_for_done_state whether to poll for a CommandState of Done
55
+ # @yieldparam [Hash] Hash representation of stream with type and text
56
+ # @yieldparam [REXML::Document] Complete SOAP envelope returned to wsmv_message
57
+ def read_response(wsmv_message, wait_for_done_state = false)
58
+ resp_doc = nil
59
+ until command_done?(resp_doc, wait_for_done_state)
60
+ logger.debug('[WinRM] Waiting for output...')
61
+ resp_doc = send_get_output_message(wsmv_message.build)
62
+ logger.debug('[WinRM] Processing output')
63
+ read_streams(resp_doc) do |stream|
64
+ yield stream, resp_doc
65
+ end
66
+ end
67
+ end
68
+
69
+ protected
70
+
71
+ def with_output
72
+ output = WinRM::Output.new
73
+ yield output
74
+ output.exitcode ||= 0
75
+ output
76
+ end
77
+
78
+ private
79
+
80
+ def handle_stream(stream, output, resp_doc)
81
+ decoded_text = @output_decoder.decode(stream[:text])
82
+ return unless decoded_text
83
+
84
+ out = { stream[:type] => decoded_text }
85
+ output << out
86
+ if (code = REXML::XPath.first(resp_doc, "//*[local-name() = 'ExitCode']"))
87
+ output.exitcode = code.text.to_i
88
+ end
89
+ [out[:stdout], out[:stderr]]
90
+ end
91
+
92
+ def send_get_output_message(message)
93
+ @transport.send_request(message)
94
+ rescue WinRMWSManFault => e
95
+ # If no output is available before the wsman:OperationTimeout expires,
96
+ # the server MUST return a WSManFault with the Code attribute equal to
97
+ # 2150858793. When the client receives this fault, it SHOULD issue
98
+ # another Receive request.
99
+ # http://msdn.microsoft.com/en-us/library/cc251676.aspx
100
+ raise unless e.fault_code == '2150858793'
101
+
102
+ logger.debug('[WinRM] retrying receive request after timeout')
103
+ retry
104
+ end
105
+
106
+ def command_done?(resp_doc, wait_for_done_state)
107
+ return false unless resp_doc
108
+ return true unless wait_for_done_state
109
+
110
+ REXML::XPath.match(
111
+ resp_doc,
112
+ "//*[@State='http://schemas.microsoft.com/wbem/wsman/1/windows/shell/" \
113
+ "CommandState/Done']"
114
+ ).any?
115
+ end
116
+
117
+ def read_streams(response_document)
118
+ body_path = "/*[local-name() = 'Envelope']/*[local-name() = 'Body']"
119
+ path = "#{body_path}/*[local-name() = 'ReceiveResponse']/*[local-name() = 'Stream']"
120
+ REXML::XPath.match(response_document, path).each do |stream|
121
+ next if stream.text.nil? || stream.text.empty?
122
+
123
+ yield type: stream.attributes['Name'].to_sym, text: stream.text
124
+ end
125
+ end
126
+ end
127
+ end
128
+ end
@@ -1,5 +1,3 @@
1
- # -*- encoding: utf-8 -*-
2
- #
3
1
  # Copyright 2016 Matt Wrock <matt@mattwrock.com>
4
2
  #
5
3
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -1,5 +1,3 @@
1
- # -*- encoding: utf-8 -*-
2
- #
3
1
  # Copyright 2010 Dan Wanek <dan.wanek@gmail.com>
4
2
  # Copyright 2016 Shawn Neal <sneal@sneal.net>
5
3
  #
@@ -1,56 +1,54 @@
1
- # -*- encoding: utf-8 -*-
2
-
3
- require 'nori'
4
- require_relative 'base'
5
-
6
- module WinRM
7
- module WSMV
8
- # WSMV message to 'pull' rest of enumeration results from Windows via WQL
9
- class WqlPull < Base
10
- def initialize(session_opts, namespace, enumeration_context)
11
- @session_opts = session_opts
12
- @namespace = namespace
13
- @enumeration_context = enumeration_context
14
- end
15
-
16
- def process_response(response)
17
- parser = Nori.new(
18
- parser: :rexml,
19
- advanced_typecasting: false,
20
- convert_tags_to: ->(tag) { tag.snakecase.to_sym },
21
- strip_namespaces: true
22
- )
23
- parser.parse(response.to_s)[:envelope][:body]
24
- end
25
-
26
- protected
27
-
28
- def create_header(header)
29
- header << Gyoku.xml(wql_header)
30
- end
31
-
32
- def create_body(body)
33
- body.tag!("#{NS_ENUM}:Pull") { |en| en << Gyoku.xml(wql_body) }
34
- end
35
-
36
- private
37
-
38
- def wql_header
39
- merge_headers(
40
- shared_headers(@session_opts),
41
- resource_uri_wmi(@namespace),
42
- action_enumerate_pull
43
- )
44
- end
45
-
46
- def wql_body
47
- {
48
- "#{NS_ENUM}:EnumerationContext" => @enumeration_context,
49
- "#{NS_WSMAN_DMTF}:OptimizeEnumeration" => nil,
50
- "#{NS_ENUM}:MaxElements" => '32000',
51
- "#{NS_WSMAN_MSFT}:SessionId" => "uuid:#{@session_opts[:session_id]}"
52
- }
53
- end
54
- end
55
- end
56
- end
1
+ require 'nori'
2
+ require_relative 'base'
3
+
4
+ module WinRM
5
+ module WSMV
6
+ # WSMV message to 'pull' rest of enumeration results from Windows via WQL
7
+ class WqlPull < Base
8
+ def initialize(session_opts, namespace, enumeration_context)
9
+ @session_opts = session_opts
10
+ @namespace = namespace
11
+ @enumeration_context = enumeration_context
12
+ end
13
+
14
+ def process_response(response)
15
+ parser = Nori.new(
16
+ parser: :rexml,
17
+ advanced_typecasting: false,
18
+ convert_tags_to: ->(tag) { tag.snakecase.to_sym },
19
+ strip_namespaces: true
20
+ )
21
+ parser.parse(response.to_s)[:envelope][:body]
22
+ end
23
+
24
+ protected
25
+
26
+ def create_header(header)
27
+ header << Gyoku.xml(wql_header)
28
+ end
29
+
30
+ def create_body(body)
31
+ body.tag!("#{NS_ENUM}:Pull") { |en| en << Gyoku.xml(wql_body) }
32
+ end
33
+
34
+ private
35
+
36
+ def wql_header
37
+ merge_headers(
38
+ shared_headers(@session_opts),
39
+ resource_uri_wmi(@namespace),
40
+ action_enumerate_pull
41
+ )
42
+ end
43
+
44
+ def wql_body
45
+ {
46
+ "#{NS_ENUM}:EnumerationContext" => @enumeration_context,
47
+ "#{NS_WSMAN_DMTF}:OptimizeEnumeration" => nil,
48
+ "#{NS_ENUM}:MaxElements" => '32000',
49
+ "#{NS_WSMAN_MSFT}:SessionId" => "uuid:#{@session_opts[:session_id]}"
50
+ }
51
+ end
52
+ end
53
+ end
54
+ end
@@ -1,99 +1,98 @@
1
- # -*- encoding: utf-8 -*-
2
- #
3
- # Copyright 2016 Shawn Neal <sneal@sneal.net>
4
- #
5
- # Licensed under the Apache License, Version 2.0 (the "License");
6
- # you may not use this file except in compliance with the License.
7
- # You may obtain a copy of the License at
8
- #
9
- # http://www.apache.org/licenses/LICENSE-2.0
10
- #
11
- # Unless required by applicable law or agreed to in writing, software
12
- # distributed under the License is distributed on an "AS IS" BASIS,
13
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
- # See the License for the specific language governing permissions and
15
- # limitations under the License.
16
-
17
- require 'nori'
18
- require_relative 'base'
19
-
20
- module WinRM
21
- module WSMV
22
- # WSMV message to query Windows via WQL
23
- class WqlQuery < Base
24
- def initialize(transport, session_opts, wql, namespace = nil)
25
- @session_opts = session_opts
26
- @wql = wql
27
- @namespace = namespace
28
- @transport = transport
29
- end
30
-
31
- def process_response(response, &block)
32
- parser = Nori.new(
33
- parser: :rexml,
34
- advanced_typecasting: false,
35
- convert_tags_to: ->(tag) { tag.snakecase.to_sym },
36
- strip_namespaces: true
37
- )
38
- @items = Hash.new { |h, k| h[k] = [] }
39
-
40
- hresp = parser.parse(response.to_s)[:envelope][:body][:enumerate_response]
41
- process_items hresp[:items], &block
42
-
43
- # Perform WS-Enum PULL's until we have all the elements
44
- enumeration_context = hresp[:enumeration_context]
45
- until enumeration_context.nil?
46
- query = WqlPull.new(@session_opts, @namespace, enumeration_context)
47
- hresp = query.process_response(@transport.send_request(query.build))[:pull_response]
48
- process_items hresp[:items], &block
49
- enumeration_context = hresp[:enumeration_context]
50
- end
51
-
52
- @items
53
- end
54
-
55
- protected
56
-
57
- def create_header(header)
58
- header << Gyoku.xml(wql_header)
59
- end
60
-
61
- def create_body(body)
62
- body.tag!("#{NS_ENUM}:Enumerate") { |en| en << Gyoku.xml(wql_body) }
63
- end
64
-
65
- private
66
-
67
- def process_items(items, &block)
68
- return if items.nil?
69
- items.each_pair do |k, v|
70
- # Normalize items so the type always has an array even if it's just a single item.
71
- v_ary = v.is_a?(Array) ? v : [v]
72
- if block
73
- v_ary.each { |val| yield k, val }
74
- else
75
- @items[k] += v_ary
76
- end
77
- end
78
- end
79
-
80
- def wql_header
81
- merge_headers(shared_headers(@session_opts), resource_uri_wmi(@namespace), action_enumerate)
82
- end
83
-
84
- def wql_body
85
- {
86
- "#{NS_WSMAN_DMTF}:OptimizeEnumeration" => nil,
87
- "#{NS_WSMAN_DMTF}:MaxElements" => '32000',
88
- "#{NS_WSMAN_DMTF}:Filter" => @wql,
89
- "#{NS_WSMAN_MSFT}:SessionId" => "uuid:#{@session_opts[:session_id]}",
90
- :attributes! => {
91
- "#{NS_WSMAN_DMTF}:Filter" => {
92
- 'Dialect' => 'http://schemas.microsoft.com/wbem/wsman/1/WQL'
93
- }
94
- }
95
- }
96
- end
97
- end
98
- end
99
- end
1
+ # Copyright 2016 Shawn Neal <sneal@sneal.net>
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ require 'nori'
16
+ require_relative 'base'
17
+
18
+ module WinRM
19
+ module WSMV
20
+ # WSMV message to query Windows via WQL
21
+ class WqlQuery < Base
22
+ def initialize(transport, session_opts, wql, namespace = nil)
23
+ @session_opts = session_opts
24
+ @wql = wql
25
+ @namespace = namespace
26
+ @transport = transport
27
+ end
28
+
29
+ def process_response(response, &block)
30
+ parser = Nori.new(
31
+ parser: :rexml,
32
+ advanced_typecasting: false,
33
+ convert_tags_to: ->(tag) { tag.snakecase.to_sym },
34
+ strip_namespaces: true
35
+ )
36
+ @items = Hash.new { |h, k| h[k] = [] }
37
+
38
+ hresp = parser.parse(response.to_s)[:envelope][:body][:enumerate_response]
39
+ process_items hresp[:items], &block
40
+
41
+ # Perform WS-Enum PULL's until we have all the elements
42
+ enumeration_context = hresp[:enumeration_context]
43
+ until enumeration_context.nil?
44
+ query = WqlPull.new(@session_opts, @namespace, enumeration_context)
45
+ hresp = query.process_response(@transport.send_request(query.build))[:pull_response]
46
+ process_items hresp[:items], &block
47
+ enumeration_context = hresp[:enumeration_context]
48
+ end
49
+
50
+ @items
51
+ end
52
+
53
+ protected
54
+
55
+ def create_header(header)
56
+ header << Gyoku.xml(wql_header)
57
+ end
58
+
59
+ def create_body(body)
60
+ body.tag!("#{NS_ENUM}:Enumerate") { |en| en << Gyoku.xml(wql_body) }
61
+ end
62
+
63
+ private
64
+
65
+ def process_items(items, &block)
66
+ return if items.nil?
67
+
68
+ items.each_pair do |k, v|
69
+ # Normalize items so the type always has an array even if it's just a single item.
70
+ v_ary = v.is_a?(Array) ? v : [v]
71
+ if block
72
+ v_ary.each { |val| yield k, val }
73
+ else
74
+ @items[k] += v_ary
75
+ end
76
+ end
77
+ end
78
+
79
+ def wql_header
80
+ merge_headers(shared_headers(@session_opts), resource_uri_wmi(@namespace), action_enumerate)
81
+ end
82
+
83
+ def wql_body
84
+ {
85
+ "#{NS_WSMAN_DMTF}:OptimizeEnumeration" => nil,
86
+ "#{NS_WSMAN_DMTF}:MaxElements" => '32000',
87
+ "#{NS_WSMAN_DMTF}:Filter" => @wql,
88
+ "#{NS_WSMAN_MSFT}:SessionId" => "uuid:#{@session_opts[:session_id]}",
89
+ :attributes! => {
90
+ "#{NS_WSMAN_DMTF}:Filter" => {
91
+ 'Dialect' => 'http://schemas.microsoft.com/wbem/wsman/1/WQL'
92
+ }
93
+ }
94
+ }
95
+ end
96
+ end
97
+ end
98
+ end
@@ -1,5 +1,3 @@
1
- # -*- encoding: utf-8 -*-
2
- #
3
1
  # Copyright 2016 Shawn Neal <sneal@sneal.net>
4
2
  #
5
3
  # Licensed under the Apache License, Version 2.0 (the "License");
data/lib/winrm.rb CHANGED
@@ -1,5 +1,3 @@
1
- # encoding: UTF-8
2
- #
3
1
  # Copyright 2010 Dan Wanek <dan.wanek@gmail.com>
4
2
  #
5
3
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -30,9 +28,9 @@ module WinRM
30
28
  Logging.logger.root.appenders = Logging.appenders.stderr
31
29
  rescue ArgumentError
32
30
  # This means that the logging level wasn't valid
33
- $stderr.puts "Invalid WINRM_LOG level is set: #{ENV['WINRM_LOG']}"
34
- $stderr.puts ''
35
- $stderr.puts 'Please use one of the standard log levels: ' \
31
+ warn "Invalid WINRM_LOG level is set: #{ENV['WINRM_LOG']}"
32
+ warn ''
33
+ warn 'Please use one of the standard log levels: ' \
36
34
  'debug, info, warn, or error'
37
35
  end
38
36
  end