This commit is contained in:
Chris Cunningham 2020-01-27 18:23:45 -05:00 committed by GitHub
commit d4596094c5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 25 deletions

View File

@ -1,7 +1,6 @@
require "pathname"
require "tmpdir"
require "vagrant/util/safe_exec"
require "readline"
module VagrantPlugins
module HostWindows
@ -10,35 +9,38 @@ module VagrantPlugins
def self.ps_client(env, ps_info)
logger = Log4r::Logger.new("vagrant::hosts::windows")
command = <<-EOS
$plain_password = "#{ps_info[:password]}"
$username = "#{ps_info[:username]}"
$port = "#{ps_info[:port]}"
$hostname = "#{ps_info[:host]}"
$password = ConvertTo-SecureString $plain_password -asplaintext -force
$creds = New-Object System.Management.Automation.PSCredential ("$hostname\\$username", $password)
function prompt { kill $PID }
Enter-PSSession -ComputerName $hostname -Credential $creds -Port $port
EOS
logger.debug("Starting remote powershell")
logger.debug("Starting remote powershell with command:\n#{command}")
options = {}
options[:user] = ps_info[:username]
options[:password] = ps_info[:password]
options[:endpoint] = "http://#{ps_info[:host]}:#{ps_info[:port]}/wsman"
options[:transport] = :plaintext
options[:basic_auth_only] = true
options[:operation_timeout] = 3600
args = ["-NoProfile"]
args << "-ExecutionPolicy"
args << "Bypass"
args << "-NoExit"
args << "-EncodedCommand"
args << encoded(command)
if ps_info[:extra_args]
args << ps_info[:extra_args]
shell = nil
client = WinRM::Connection.new(options)
shell = client.shell(:powershell)
prompt = "[#{options[:user]}@#{URI.parse(options[:endpoint]).host}]: PS> "
while (buf = Readline.readline(prompt, true))
if buf =~ /^exit/
break
else
shell.run(buf) do |stdout, stderr|
$stdout.write stdout
$stderr.write stderr
end
end
end
# Launch it
Vagrant::Util::SafeExec.exec("powershell", *args)
ensure
shell.close unless shell.nil?
end
def self.encoded(script)
encoded_script = script.encode('UTF-16LE', 'UTF-8')
encoded_script = script.encode("UTF-16LE", "UTF-8")
Base64.strict_encode64(encoded_script)
end
end

View File

@ -27,6 +27,7 @@ Gem::Specification.new do |s|
s.add_dependency "net-sftp", "~> 2.1"
s.add_dependency "net-scp", "~> 1.2.0"
s.add_dependency "rb-kqueue", "~> 0.2.0"
s.add_dependency "rb-readline", "~> 0.5.5"
s.add_dependency "rest-client", ">= 1.6.0", "< 3.0"
s.add_dependency "rubyzip", "~> 1.3"
s.add_dependency "wdm", "~> 0.1.0"