2014-08-27 19:17:30 +00:00
|
|
|
require "optparse"
|
|
|
|
|
2015-12-05 14:24:58 +00:00
|
|
|
require "vagrant/util/powershell"
|
2014-08-27 19:17:30 +00:00
|
|
|
require_relative "../../communicators/winrm/helper"
|
|
|
|
|
|
|
|
module VagrantPlugins
|
|
|
|
module CommandPS
|
|
|
|
class Command < Vagrant.plugin("2", :command)
|
|
|
|
def self.synopsis
|
|
|
|
"connects to machine via powershell remoting"
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
|
|
|
options = {}
|
|
|
|
|
|
|
|
opts = OptionParser.new do |o|
|
2015-11-18 18:51:18 +00:00
|
|
|
o.banner = "Usage: vagrant powershell [-- extra powershell args]"
|
2014-08-27 19:17:30 +00:00
|
|
|
|
2018-09-05 19:56:17 +00:00
|
|
|
o.separator ""
|
2018-09-05 20:26:22 +00:00
|
|
|
o.separator "Opens a PowerShell session on the host to the guest"
|
|
|
|
o.separator "machine if both support powershell remoting."
|
2014-08-27 19:17:30 +00:00
|
|
|
o.separator ""
|
|
|
|
o.separator "Options:"
|
|
|
|
o.separator ""
|
|
|
|
|
|
|
|
o.on("-c", "--command COMMAND", "Execute a powershell command directly") do |c|
|
|
|
|
options[:command] = c
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Parse out the extra args to send to the ps session, which
|
|
|
|
# is everything after the "--"
|
|
|
|
split_index = @argv.index("--")
|
|
|
|
if split_index
|
|
|
|
options[:extra_args] = @argv.drop(split_index + 1)
|
|
|
|
@argv = @argv.take(split_index)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Parse the options and return if we don't have any target.
|
|
|
|
argv = parse_options(opts)
|
|
|
|
return if !argv
|
|
|
|
|
|
|
|
# Check if the host even supports ps remoting
|
|
|
|
raise Errors::HostUnsupported if !@env.host.capability?(:ps_client)
|
|
|
|
|
|
|
|
# Execute ps session if we can
|
|
|
|
with_target_vms(argv, single_target: true) do |machine|
|
|
|
|
if !machine.communicate.ready?
|
|
|
|
raise Vagrant::Errors::VMNotCreatedError
|
|
|
|
end
|
|
|
|
|
2015-06-05 07:11:06 +00:00
|
|
|
if machine.config.vm.communicator != :winrm
|
2014-08-27 19:17:30 +00:00
|
|
|
raise VagrantPlugins::CommunicatorWinRM::Errors::WinRMNotReady
|
|
|
|
end
|
|
|
|
|
|
|
|
if !options[:command].nil?
|
2015-06-05 12:03:29 +00:00
|
|
|
out_code = machine.communicate.execute(options[:command].dup) do |type,data|
|
|
|
|
machine.ui.detail(data) if type == :stdout
|
|
|
|
end
|
2014-08-27 19:17:30 +00:00
|
|
|
if out_code == 0
|
2018-03-14 14:41:04 +00:00
|
|
|
machine.ui.success("Command: #{options[:command]} executed successfully with output code #{out_code}.")
|
2014-08-27 19:17:30 +00:00
|
|
|
end
|
2015-06-05 07:11:06 +00:00
|
|
|
next
|
2014-08-27 19:17:30 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
ps_info = VagrantPlugins::CommunicatorWinRM::Helper.winrm_info(machine)
|
|
|
|
ps_info[:username] = machine.config.winrm.username
|
|
|
|
ps_info[:password] = machine.config.winrm.password
|
|
|
|
# Extra arguments if we have any
|
|
|
|
ps_info[:extra_args] = options[:extra_args]
|
|
|
|
|
2015-06-05 07:11:06 +00:00
|
|
|
result = ready_ps_remoting_for(machine, ps_info)
|
2014-08-27 19:17:30 +00:00
|
|
|
|
|
|
|
machine.ui.detail(
|
|
|
|
"Creating powershell session to #{ps_info[:host]}:#{ps_info[:port]}")
|
|
|
|
machine.ui.detail("Username: #{ps_info[:username]}")
|
|
|
|
|
|
|
|
begin
|
|
|
|
@env.host.capability(:ps_client, ps_info)
|
|
|
|
ensure
|
|
|
|
if !result["PreviousTrustedHosts"].nil?
|
2015-06-05 07:11:06 +00:00
|
|
|
reset_ps_remoting_for(machine, ps_info)
|
2014-08-27 19:17:30 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def ready_ps_remoting_for(machine, ps_info)
|
|
|
|
machine.ui.output(I18n.t("vagrant_ps.detecting"))
|
|
|
|
script_path = File.expand_path("../scripts/enable_psremoting.ps1", __FILE__)
|
|
|
|
args = []
|
|
|
|
args << "-hostname" << ps_info[:host]
|
|
|
|
args << "-port" << ps_info[:port].to_s
|
|
|
|
args << "-username" << ps_info[:username]
|
|
|
|
args << "-password" << ps_info[:password]
|
|
|
|
result = Vagrant::Util::PowerShell.execute(script_path, *args)
|
|
|
|
if result.exit_code != 0
|
2015-06-06 05:24:05 +00:00
|
|
|
raise Errors::PowerShellError,
|
2014-08-27 19:17:30 +00:00
|
|
|
script: script_path,
|
|
|
|
stderr: result.stderr
|
|
|
|
end
|
|
|
|
|
|
|
|
result_output = JSON.parse(result.stdout)
|
|
|
|
raise Errors::PSRemotingUndetected if !result_output["Success"]
|
|
|
|
result_output
|
|
|
|
end
|
|
|
|
|
|
|
|
def reset_ps_remoting_for(machine, ps_info)
|
2018-03-14 14:41:04 +00:00
|
|
|
machine.ui.output(I18n.t("vagrant_ps.resetting"))
|
2014-08-27 19:17:30 +00:00
|
|
|
script_path = File.expand_path("../scripts/reset_trustedhosts.ps1", __FILE__)
|
|
|
|
args = []
|
|
|
|
args << "-hostname" << ps_info[:host]
|
|
|
|
result = Vagrant::Util::PowerShell.execute(script_path, *args)
|
|
|
|
if result.exit_code != 0
|
2015-06-06 05:24:05 +00:00
|
|
|
raise Errors::PowerShellError,
|
2014-08-27 19:17:30 +00:00
|
|
|
script: script_path,
|
|
|
|
stderr: result.stderr
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|