class WinRM::Connection

WinRM connection used to establish a session with the remote WinRM service.

Attributes

logger[RW]

Public Class Methods

new(connection_opts) click to toggle source

Creates a new WinRM connection See the ConnectionOpts class for connection options.

# File lib/winrm/connection.rb, line 26
def initialize(connection_opts)
  configure_connection_opts(connection_opts)
  configure_logger
end

Public Instance Methods

run_wql(wql, namespace = 'root/cimv2/*', &block) click to toggle source

Executes a WQL query against the WinRM connection @param wql [String] The wql query @param namespace [String] namespace for query - default is root/cimv2/* @return [Hash] Hash representation of wql query response (Hash is empty if a block is given) @yeild [type, item] Yields the time name and item for every item

# File lib/winrm/connection.rb, line 56
def run_wql(wql, namespace = 'root/cimv2/*', &block)
  query = WinRM::WSMV::WqlQuery.new(transport, @connection_opts, wql, namespace)
  query.process_response(transport.send_request(query.build), &block)
end
shell(shell_type, shell_opts = {}) { |shell| ... } click to toggle source

Creates a new shell on the remote Windows server associated with this connection. @param shell_type [Symbol] The shell type :cmd or :powershell @param shell_opts [Hash] Options targeted for the created shell @return [Shell] PowerShell or Cmd shell instance.

# File lib/winrm/connection.rb, line 38
def shell(shell_type, shell_opts = {})
  shell = shell_factory.create_shell(shell_type, shell_opts)
  if block_given?
    begin
      yield shell
    ensure
      shell.close
    end
  else
    shell
  end
end

Private Instance Methods

configure_connection_opts(connection_opts) click to toggle source
# File lib/winrm/connection.rb, line 63
def configure_connection_opts(connection_opts)
  @connection_opts = ConnectionOpts.create_with_defaults(connection_opts)
end
configure_logger() click to toggle source
# File lib/winrm/connection.rb, line 67
def configure_logger
  @logger = Logging.logger[self]
  logger.level = :warn
  logger.add_appenders(Logging.appenders.stdout)
end
shell_factory() click to toggle source
# File lib/winrm/connection.rb, line 73
def shell_factory
  @shell_factory ||= WinRM::Shells::ShellFactory.new(@connection_opts, transport, logger)
end
transport() click to toggle source
# File lib/winrm/connection.rb, line 77
def transport
  @transport ||= begin
    transport_factory = WinRM::HTTP::TransportFactory.new
    transport_factory.create_transport(@connection_opts)
  end
end