Skip to content

Commit

Permalink
hashicorp#9298: Try to fallback to "powershell.exe" on WSL if "powers…
Browse files Browse the repository at this point in the history
…hell" is not available
  • Loading branch information
BR0kEN- authored and chrisroberts committed Feb 26, 2018
1 parent f417889 commit 6967ba4
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions lib/vagrant/util/powershell.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,27 @@ class PowerShell
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.
Expand All @@ -27,12 +42,11 @@ def self.available?
# @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",
Expand All @@ -57,7 +71,7 @@ def self.execute(path, *args, **opts, &block)
def self.execute_cmd(command)
validate_install!
c = [
"powershell",
executable,
"-NoLogo",
"-NoProfile",
"-NonInteractive",
Expand All @@ -77,7 +91,7 @@ def self.execute_cmd(command)
def self.version
if !defined?(@_powershell_version)
command = [
"powershell",
executable,
"-NoLogo",
"-NoProfile",
"-NonInteractive",
Expand Down

0 comments on commit 6967ba4

Please sign in to comment.