Class: VagrantPlugins::VagrantWinRM::WinRM

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-winrm/commands/winrm.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.synopsisObject



7
8
9
# File 'lib/vagrant-winrm/commands/winrm.rb', line 7

def self.synopsis
  'connects to machine via WinRM'
end

Instance Method Details

#executeObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/vagrant-winrm/commands/winrm.rb', line 11

def execute
  options = { shell: :powershell }

  opts = OptionParser.new do |o|
    o.banner = 'Usage: vagrant winrm [options] [name]'
    o.separator 'Options:'

    o.on('-c', '--command COMMAND', 'Execute a WinRM command directly') do |c|
      options[:command] = Array.new if options[:command].nil?
      options[:command].push c
    end

    o.on('-e', '--elevated', 'Run all commands with elevated credentials') do |e|
      options[:elevated] = true
    end

    o.on('-s', '--shell SHELL', [:powershell, :cmd], 'Use the specified shell (powershell, cmd)') do |s|
      options[:shell] = s
    end

    o.on('--plugin-version', 'Print the version of the plugin and exit') do
      options[:version] = true
    end
  end

  # Parse the options and return if we don't have any target.
  argv = parse_options(opts)
  return unless argv

  if options[:version]
    require "#{VagrantPlugins::VagrantWinRM.source_root}/lib/version"
    safe_puts "Vagrant-winrm plugin #{VERSION}"
    return 0
  end

  return 0 unless options[:command]

  # Execute the actual WinRM command
  with_target_vms(argv, single_target: true) do |vm|

    raise Errors::ConfigurationError, { :communicator => vm.config.vm.communicator } if vm.config.vm.communicator != :winrm

    exit_code = 0
    @logger.debug("Executing a batch of #{options[:command].length} on remote machine with #{options[:shell]}")

    options[:command].each do |c|
      @logger.debug("Executing command: #{c}")
      exit_code |= vm.communicate.execute(c, shell: options[:shell], elevated: options[:elevated]) { |type, data| (type == :stderr ? $stderr : $stdout).print data }
    end
    return exit_code
  end
end