class WinRM::FS::FileManager

Perform file transfer operations between a local machine and winrm endpoint

Public Class Methods

new(service) click to toggle source

Creates a new FileManager instance @param [WinRMWebService] WinRM web service client

# File lib/winrm-fs/file_manager.rb, line 25
def initialize(service)
  @service = service
  @logger = Logging.logger[self]
end

Public Instance Methods

checksum(path) click to toggle source

Gets the MD5 checksum of the specified file if it exists, otherwise '' @param [String] The remote file path

# File lib/winrm-fs/file_manager.rb, line 33
def checksum(path)
  @logger.debug("checksum: #{path}")
  script = WinRM::FS::Scripts.render('checksum', path: path)
  @service.powershell(script).stdout.chomp
end
create_dir(path) click to toggle source

Create the specifed directory recursively @param [String] The remote dir to create @return [Boolean] True if successful, otherwise false

# File lib/winrm-fs/file_manager.rb, line 42
def create_dir(path)
  @logger.debug("create_dir: #{path}")
  script = WinRM::FS::Scripts.render('create_dir', path: path)
  @service.powershell(script)[:exitcode] == 0
end
delete(path) click to toggle source

Deletes the file or directory at the specified path @param [String] The path to remove @return [Boolean] True if successful, otherwise False

# File lib/winrm-fs/file_manager.rb, line 51
def delete(path)
  @logger.debug("deleting: #{path}")
  script = WinRM::FS::Scripts.render('delete', path: path)
  @service.powershell(script)[:exitcode] == 0
end
download(remote_path, local_path) click to toggle source

Downloads the specified remote file to the specified local path @param [String] The full path on the remote machine @param [String] The full path to write the file to locally

# File lib/winrm-fs/file_manager.rb, line 60
def download(remote_path, local_path)
  @logger.debug("downloading: #{remote_path} -> #{local_path}")
  script = WinRM::FS::Scripts.render('download', path: remote_path)
  output = @service.powershell(script)
  return false if output[:exitcode] != 0
  contents = output.stdout.gsub('\n\r', '')
  out = Base64.decode64(contents)
  IO.binwrite(local_path, out)
  true
end
exists?(path) click to toggle source

Checks to see if the given path exists on the target file system. @param [String] The full path to the directory or file @return [Boolean] True if the file/dir exists, otherwise false.

# File lib/winrm-fs/file_manager.rb, line 74
def exists?(path)
  @logger.debug("exists?: #{path}")
  script = WinRM::FS::Scripts.render('exists', path: path)
  @service.powershell(script)[:exitcode] == 0
end
temp_dir() click to toggle source

Gets the current user's TEMP directory on the remote system, for example 'C:/Windows/Temp' @return [String] Full path to the temp directory

# File lib/winrm-fs/file_manager.rb, line 83
def temp_dir
  @guest_temp ||= (@service.cmd('echo %TEMP%')).stdout.chomp.gsub('\', '/')
end
upload(local_path, remote_path, &block) click to toggle source

Upload one or more local files and directories to a remote directory @example copy a single file to a winrm endpoint

file_manager.upload('/Users/sneal/myfile.txt', 'c:/foo/myfile.txt')

@example copy a single directory to a winrm endpoint

file_manager.upload('c:/dev/my_dir', '$env:AppData')

@param [String] A path to a local directory or file that will be copied

to the remote Windows box.

@param [String] The target directory or file

This path may contain powershell style environment variables

@yieldparam [Fixnum] Number of bytes copied in current payload sent to the winrm endpoint @yieldparam [Fixnum] The total number of bytes to be copied @yieldparam [String] Path of file being copied @yieldparam [String] Target path on the winrm endpoint @return [Fixnum] The total number of bytes copied

# File lib/winrm-fs/file_manager.rb, line 105
def upload(local_path, remote_path, &block)
  @logger.debug("uploading: #{local_path} -> #{remote_path}")

  upload_orchestrator = WinRM::FS::Core::UploadOrchestrator.new(@service)
  if File.file?(local_path)
    upload_orchestrator.upload_file(local_path, remote_path, &block)
  else
    upload_orchestrator.upload_directory(local_path, remote_path, &block)
  end
end