summaryrefslogtreecommitdiff
path: root/lib/chef/mixin/powershell_out.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/chef/mixin/powershell_out.rb')
-rw-r--r--lib/chef/mixin/powershell_out.rb17
1 files changed, 12 insertions, 5 deletions
diff --git a/lib/chef/mixin/powershell_out.rb b/lib/chef/mixin/powershell_out.rb
index 1ecc9bac98..e6519fe5a0 100644
--- a/lib/chef/mixin/powershell_out.rb
+++ b/lib/chef/mixin/powershell_out.rb
@@ -28,19 +28,24 @@ class Chef
# can be set to :i386 or :x86_64 to force the windows architecture.
#
# @param script [String] script to run
+ # @param interpreter [Symbol] the interpreter type, `:powershell` or `:pwsh`
# @param options [Hash] options hash
# @return [Mixlib::Shellout] mixlib-shellout object
def powershell_out(*command_args)
script = command_args.first
options = command_args.last.is_a?(Hash) ? command_args.last : nil
+ interpreter = command_args[1].is_a?(Symbol) ? command_args[1] : :powershell
- run_command_with_os_architecture(script, options)
+ raise ArgumentError, "Expected interpreter of :powershell or :pwsh" unless [:powershell, :pwsh].include?(interpreter)
+
+ run_command_with_os_architecture(script, interpreter, options)
end
# Run a command under powershell with the same API as shell_out!
# (raises exceptions on errors)
#
# @param script [String] script to run
+ # @param interpreter [Symbol] the interpreter type, `:powershell` or `:pwsh`
# @param options [Hash] options hash
# @return [Mixlib::Shellout] mixlib-shellout object
def powershell_out!(*command_args)
@@ -56,16 +61,17 @@ class Chef
# because chef-client runs as a 32-bit app on 64-bit windows).
#
# @param script [String] script to run
+ # @param interpreter [Symbol] the interpreter type, `:powershell` or `:pwsh`
# @param options [Hash] options hash
# @return [Mixlib::Shellout] mixlib-shellout object
- def run_command_with_os_architecture(script, options)
+ def run_command_with_os_architecture(script, interpreter, options)
options ||= {}
options = options.dup
arch = options.delete(:architecture)
with_os_architecture(nil, architecture: arch) do
shell_out(
- build_powershell_command(script),
+ build_powershell_command(script, interpreter),
**options
)
end
@@ -74,8 +80,9 @@ class Chef
# Helper to build a powershell command around the script to run.
#
# @param script [String] script to run
+ # @param interpreter [Symbol] the interpreter type, `:powershell` or `:pwsh`
# @return [String] powershell command to execute
- def build_powershell_command(script)
+ def build_powershell_command(script, interpreter)
flags = [
# Hides the copyright banner at startup.
"-NoLogo",
@@ -91,7 +98,7 @@ class Chef
"-InputFormat None",
]
- "powershell.exe #{flags.join(" ")} -Command \"#{script.gsub('"', '\"')}\""
+ "#{interpreter}.exe #{flags.join(" ")} -Command \"#{script.gsub('"', '\"')}\""
end
end
end