Sha256: b5047221ab1f6ea7e4397ac7eac5b73d69a34645e6016c4cf2395996e38970af

Contents?: true

Size: 1.79 KB

Versions: 21

Compression:

Stored size: 1.79 KB

Contents

require 'optparse'

require "vagrant/util/safe_puts"

module VagrantPlugins
  module CommandWinRM
    class Command < Vagrant.plugin("2", :command)
      include Vagrant::Util::SafePuts

      def self.synopsis
        "executes commands on a machine via WinRM"
      end

      def execute
        options = {
          command: [],
          shell: :powershell
        }

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

          o.on("-c", "--command COMMAND", "Execute a WinRM command directly") do |c|
            options[:command] << c
          end

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

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

        argv = parse_options(opts)
        return if !argv

        with_target_vms(argv, single_target: true) do |machine|
          if machine.config.vm.communicator != :winrm
            raise Vagrant::Errors::WinRMInvalidCommunicator
          end

          opts = {
            shell: options[:shell],
            elevated: !!options[:elevated]
          }

          options[:command].each do |cmd|
            begin
              machine.communicate.execute(cmd, opts) do |type, data|
                io = type == :stderr ? $stderr : $stdout
                safe_puts(data, io: io, printer: :print)
              end
            rescue VagrantPlugins::CommunicatorWinRM::Errors::WinRMBadExitStatus
              return 1
            end
          end
        end

        # Success, exit status 0
        0
      end
    end
  end
end

Version data entries

21 entries across 21 versions & 3 rubygems

Version Path
vagrant-unbundled-2.3.6.0 plugins/commands/winrm/command.rb
tamtam-vagrant-reload-1.2.1 vendor/cache/vagrant-2092df529ae7/plugins/commands/winrm/command.rb
vagrant-unbundled-2.3.3.0 plugins/commands/winrm/command.rb
vagrant-unbundled-2.3.2.0 plugins/commands/winrm/command.rb
vagrant-unbundled-2.2.19.0 plugins/commands/winrm/command.rb
vagrant-unbundled-2.2.18.0 plugins/commands/winrm/command.rb
vagrant-unbundled-2.2.16.0 plugins/commands/winrm/command.rb
vagrant-unbundled-2.2.14.0 plugins/commands/winrm/command.rb
vagrant-aws-mkubenka-0.7.2.pre.24 vendor/bundle/ruby/2.7.0/bundler/gems/vagrant-22795b161bf6/plugins/commands/winrm/command.rb
vagrant-unbundled-2.2.10.0 plugins/commands/winrm/command.rb
vagrant-unbundled-2.2.9.0 plugins/commands/winrm/command.rb
vagrant-unbundled-2.2.8.0 plugins/commands/winrm/command.rb
vagrant-unbundled-2.2.7.0 plugins/commands/winrm/command.rb
vagrant-unbundled-2.2.6.2 plugins/commands/winrm/command.rb
vagrant-unbundled-2.2.6.1 plugins/commands/winrm/command.rb
vagrant-unbundled-2.2.6.0 plugins/commands/winrm/command.rb
vagrant-unbundled-2.2.5.0 plugins/commands/winrm/command.rb
vagrant-unbundled-2.2.4.0 plugins/commands/winrm/command.rb
vagrant-unbundled-2.2.3.0 plugins/commands/winrm/command.rb
vagrant-unbundled-2.2.2.0 plugins/commands/winrm/command.rb