lib/vagrant/util/powershell.rb in vagrant-unbundled-2.1.1.0 vs lib/vagrant/util/powershell.rb in vagrant-unbundled-2.1.2.0

- old
+ new

@@ -18,16 +18,24 @@ # @return [String|nil] a powershell executable, depending on environment def self.executable if !defined?(@_powershell_executable) @_powershell_executable = "powershell" - # Try to use WSL interoperability if PowerShell is not symlinked to - # the container. - if Which.which(@_powershell_executable).nil? && Platform.wsl? - @_powershell_executable += ".exe" + if Which.which(@_powershell_executable).nil? + # Try to use WSL interoperability if PowerShell is not symlinked to + # the container. + if Platform.wsl? + @_powershell_executable += ".exe" - if Which.which(@_powershell_executable).nil? + if Which.which(@_powershell_executable).nil? + @_powershell_executable = "/mnt/c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe" + + if Which.which(@_powershell_executable).nil? + @_powershell_executable = nil + end + end + else @_powershell_executable = nil end end end @_powershell_executable @@ -39,23 +47,34 @@ end # Execute a powershell script. # # @param [String] path Path to the PowerShell script to execute. + # @param [Array<String>] args Command arguments + # @param [Hash] opts Options passed to execute + # @option opts [Hash] :env Custom environment variables # @return [Subprocess::Result] def self.execute(path, *args, **opts, &block) validate_install! if opts.delete(:sudo) || opts.delete(:runas) powerup_command(path, args, opts) else + if mpath = opts.delete(:module_path) + m_env = opts.fetch(:env, {}) + m_env["PSModulePath"] = "$env:PSModulePath+';#{mpath}'" + opts[:env] = m_env + end + if env = opts.delete(:env) + env = env.map{|k,v| "$env:#{k}=#{v}"}.join(";") + "; " + end command = [ executable, "-NoLogo", "-NoProfile", "-NonInteractive", "-ExecutionPolicy", "Bypass", - "&('#{path}')", + "#{env}&('#{path}')", args ].flatten # Append on the options hash since Subprocess doesn't use # Ruby 2.0 style options yet. @@ -66,22 +85,32 @@ end # Execute a powershell command. # # @param [String] command PowerShell command to execute. + # @param [Hash] opts Extra options + # @option opts [Hash] :env Custom environment variables # @return [nil, String] Returns nil if exit code is non-zero. # Returns stdout string if exit code is zero. - def self.execute_cmd(command) + def self.execute_cmd(command, **opts) validate_install! + if mpath = opts.delete(:module_path) + m_env = opts.fetch(:env, {}) + m_env["PSModulePath"] = "$env:PSModulePath+';#{mpath}'" + opts[:env] = m_env + end + if env = opts.delete(:env) + env = env.map{|k,v| "$env:#{k}=#{v}"}.join(";") + "; " + end c = [ executable, "-NoLogo", "-NoProfile", "-NonInteractive", "-ExecutionPolicy", "Bypass", "-Command", - command + "#{env}#{command}" ].flatten.compact r = Subprocess.execute(*c) return nil if r.exit_code != 0 return r.stdout.chomp @@ -89,20 +118,29 @@ # Execute a powershell command and return a result # # @param [String] command PowerShell command to execute. # @param [Hash] opts A collection of options for subprocess::execute + # @option opts [Hash] :env Custom environment variables # @param [Block] block Ruby block def self.execute_inline(*command, **opts, &block) validate_install! + if mpath = opts.delete(:module_path) + m_env = opts.fetch(:env, {}) + m_env["PSModulePath"] = "$env:PSModulePath+';#{mpath}'" + opts[:env] = m_env + end + if env = opts.delete(:env) + env = env.map{|k,v| "$env:#{k}=#{v}"}.join(";") + "; " + end c = [ executable, "-NoLogo", "-NoProfile", "-NonInteractive", "-ExecutionPolicy", "Bypass", "-Command", - command + "#{env}#{command}" ].flatten.compact c << opts Subprocess.execute(*c, &block) end