From 4ff5291b89a9a11f5005689443087cdfb7827fae Mon Sep 17 00:00:00 2001 From: Chris Roberts Date: Fri, 9 Nov 2018 15:26:55 -0800 Subject: [PATCH] Update powerup to handle spaces properly in arguments --- lib/vagrant/util/powershell.rb | 48 ++++++++++++++++------------------ 1 file changed, 23 insertions(+), 25 deletions(-) diff --git a/lib/vagrant/util/powershell.rb b/lib/vagrant/util/powershell.rb index 9f76b730f..dfe0122b2 100644 --- a/lib/vagrant/util/powershell.rb +++ b/lib/vagrant/util/powershell.rb @@ -1,3 +1,4 @@ +require "base64" require "tmpdir" require_relative "subprocess" require_relative "which" @@ -208,18 +209,26 @@ module Vagrant # @return [Array] def self.powerup_command(path, args, opts) Dir.mktmpdir("vagrant") do |dpath| - all_args = ["-NoProfile", "-NonInteractive", "-ExecutionPolicy", "Bypass", path] + args - arg_list = "@('" + all_args.join("', '") + "')" + all_args = [path] + args.flatten.map{ |a| + a.gsub(/^['"](.+)['"]$/, "\\1") + } + arg_list = "\"" + all_args.join("\" \"") + "\"" stdout = File.join(dpath, "stdout.txt") stderr = File.join(dpath, "stderr.txt") - exitcode = File.join(dpath, "exitcode.txt") - script = "$sp = Start-Process -FilePath powershell -ArgumentList #{arg_list} " \ - "-PassThru -Wait -RedirectStandardOutput '#{stdout}' -RedirectStandardError '#{stderr}' -WindowStyle Hidden; " \ - "if($sp){ Set-Content -Path '#{exitcode}' -Value $sp.ExitCode;exit $sp.ExitCode; }else{ exit 1 }" + script = "& #{arg_list} ; exit $LASTEXITCODE;" + script_content = Base64.urlsafe_encode64(script.encode("UTF-16LE", "UTF-8")) - # escape quotes so we can nest our script within a start-process - script.gsub!("'", "''") + # Wrap so we can redirect output to read later + wrapper = "$p = Start-Process -FilePath powershell -ArgumentList @('-NoLogo', '-NoProfile', " \ + "'-NonInteractive', '-ExecutionPolicy', 'Bypass', '-EncodedCommand', '#{script_content}') " \ + "-PassThru -WindowStyle Hidden -Wait -RedirectStandardOutput '#{stdout}' -RedirectStandardError '#{stderr}'; " \ + "if($p){ exit $p.ExitCode; }else{ exit 1 }" + wrapper_content = Base64.urlsafe_encode64(wrapper.encode("UTF-16LE", "UTF-8")) + + powerup = "$p = Start-Process -FilePath powershell -ArgumentList @('-NoLogo', '-NoProfile', " \ + "'-NonInteractive', '-ExecutionPolicy', 'Bypass', '-EncodedCommand', '#{wrapper_content}') " \ + "-PassThru -WindowStyle Hidden -Wait -Verb RunAs; if($p){ exit $p.ExitCode; }else{ exit 1 }" cmd = [ "powershell", @@ -227,31 +236,20 @@ module Vagrant "-NoProfile", "-NonInteractive", "-ExecutionPolicy", "Bypass", - "-Command", "$p = Start-Process -FilePath powershell -ArgumentList " \ - "@('-NoLogo', '-NoProfile', '-NonInteractive', '-ExecutionPolicy', 'Bypass', '-Command', '#{script}') " \ - "-PassThru -Wait -WindowStyle Hidden -Verb RunAs; if($p){ exit $p.ExitCode; }else{ exit 1 }" + "-Command", powerup ] result = Subprocess.execute(*cmd.push(opts)) + r_stdout = result.stdout if File.exist?(stdout) - r_stdout = File.read(stdout) - else - r_stdout = result.stdout + r_stdout += File.read(stdout) end + r_stderr = result.stderr if File.exist?(stderr) - r_stderr = File.read(stderr) - else - r_stderr = result.stderr + r_stderr += File.read(stderr) end - code = 1 - if File.exist?(exitcode) - code_txt = File.read(exitcode).strip - if code_txt.match(/^\d+$/) - code = code_txt.to_i - end - end - Subprocess::Result.new(code, r_stdout, r_stderr) + Subprocess::Result.new(result.exit_code, r_stdout, r_stderr) end end