Sha256: 507783456e7c6c7c4f36a96de0a52fae50883173b5e9e8033ea02c1a16676c66

Contents?: true

Size: 1.34 KB

Versions: 1

Compression:

Stored size: 1.34 KB

Contents

require_relative "subprocess"
require_relative "which"

module Vagrant
  module Util
    # Executes PowerShell scripts.
    #
    # This is primarily a convenience wrapper around Subprocess that
    # properly sets powershell flags for you.
    class PowerShell
      def self.available?
        !!Which.which("powershell")
      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)
        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

      # Returns the version of PowerShell that is installed.
      #
      # @return [String]
      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
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
vagrant-cloudstack-1.2.0 vendor/bundle/bundler/gems/vagrant-c84e05fd063f/lib/vagrant/util/powershell.rb