class Vagrant::Util::PowerShell

Executes PowerShell scripts.

This is primarily a convenience wrapper around Subprocess that properly sets powershell flags for you.

Public Class Methods

available?() click to toggle source
# File lib/vagrant/util/powershell.rb, line 11
def self.available?
  !!Which.which("powershell")
end
execute(path, *args, **opts, &block) click to toggle source

Execute a powershell script.

@param [String] path Path to the PowerShell script to execute. @return [Subprocess::Result]

# File lib/vagrant/util/powershell.rb, line 19
def self.execute(path, *args, **opts, &block)
  command = [
    "powershell",
    "-NoProfile",
    "-ExecutionPolicy", "Bypass",
    "&('#{path}')",
    args
  ].flatten

  # Append on the options hash since Subprocess doesn't use
  # Ruby 2.0 style options yet.
  command << opts

  Subprocess.execute(*command, &block)
end
version() click to toggle source

Returns the version of PowerShell that is installed.

@return [String]

# File lib/vagrant/util/powershell.rb, line 38
def self.version
  command = [
    "powershell",
    "-NoProfile",
    "-ExecutionPolicy", "Bypass",
    "$PSVersionTable.PSVersion.Major"
  ].flatten

  r = Subprocess.execute(*command)
  return nil if r.exit_code != 0
  return r.stdout.chomp
end