lib/vagrant/util/powershell.rb in vagrant-unbundled-2.0.2.0 vs lib/vagrant/util/powershell.rb in vagrant-unbundled-2.0.3.0

- old
+ new

@@ -9,31 +9,46 @@ # This is primarily a convenience wrapper around Subprocess that # properly sets powershell flags for you. class PowerShell # NOTE: Version checks are only on Major MINIMUM_REQUIRED_VERSION = 3 + LOGGER = Log4r::Logger.new("vagrant::util::powershell") + # @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? + @_powershell_executable = nil + end + end + end + @_powershell_executable + end + # @return [Boolean] powershell executable available on PATH def self.available? - if !defined?(@_powershell_available) - @_powershell_available = !!Which.which("powershell") - end - @_powershell_available + !executable.nil? end # Execute a powershell script. # # @param [String] path Path to the PowerShell script to execute. # @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 command = [ - "powershell", + executable, "-NoLogo", "-NoProfile", "-NonInteractive", "-ExecutionPolicy", "Bypass", "&('#{path}')", @@ -54,11 +69,11 @@ # @return [nil, String] Returns nil if exit code is non-zero. # Returns stdout string if exit code is zero. def self.execute_cmd(command) validate_install! c = [ - "powershell", + executable, "-NoLogo", "-NoProfile", "-NonInteractive", "-ExecutionPolicy", "Bypass", "-Command", @@ -74,20 +89,26 @@ # # @return [String] def self.version if !defined?(@_powershell_version) command = [ - "powershell", + executable, "-NoLogo", "-NoProfile", "-NonInteractive", "-ExecutionPolicy", "Bypass", "-Command", "Write-Output $PSVersionTable.PSVersion.Major" ].flatten - r = Subprocess.execute(*command) - @_powershell_version = r.exit_code != 0 ? nil : r.stdout.chomp + version = nil + begin + r = Subprocess.execute(*command, notify: [:stdout, :stderr], timeout: 10) {|io_name,data| version = data} + rescue Vagrant::Util::Subprocess::TimeoutExceeded + LOGGER.debug("Timeout exceeded while attempting to determine version of Powershell.") + end + + @_powershell_version = version end @_powershell_version end # Validates that powershell is installed, available, and