From 65ab2188a6b48835aa0280394427fa675076bb86 Mon Sep 17 00:00:00 2001
From: Chris Cunningham <therealchriscunningham@outlook.com>
Date: Sun, 13 Jan 2019 16:58:24 +0100
Subject: [PATCH 1/2] replace call to enter-pssession with a native ruby REPL
 within winrm

---
 plugins/hosts/windows/cap/ps.rb | 52 +++++++++++++++++----------------
 vagrant.gemspec                 |  1 +
 2 files changed, 28 insertions(+), 25 deletions(-)

diff --git a/plugins/hosts/windows/cap/ps.rb b/plugins/hosts/windows/cap/ps.rb
index ec778359c..7b1f99b47 100644
--- a/plugins/hosts/windows/cap/ps.rb
+++ b/plugins/hosts/windows/cap/ps.rb
@@ -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
diff --git a/vagrant.gemspec b/vagrant.gemspec
index 885c0d359..5ff34fcab 100644
--- a/vagrant.gemspec
+++ b/vagrant.gemspec
@@ -28,6 +28,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.2.2"
   s.add_dependency "wdm", "~> 0.1.0"

From ad1cca534150ffd0514d09b8f041a8a5f462842d Mon Sep 17 00:00:00 2001
From: Chris Cunningham <therealchriscunningham@outlook.com>
Date: Sat, 29 Jun 2019 13:09:06 +0200
Subject: [PATCH 2/2] fix gemspec

---
 vagrant.gemspec | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/vagrant.gemspec b/vagrant.gemspec
index 5ff34fcab..36bcc588c 100644
--- a/vagrant.gemspec
+++ b/vagrant.gemspec
@@ -28,7 +28,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 "rb-readline", "~> 0.5.5"
   s.add_dependency "rest-client", ">= 1.6.0", "< 3.0"
   s.add_dependency "rubyzip", "~> 1.2.2"
   s.add_dependency "wdm", "~> 0.1.0"