From c811d58a84cc2b92a24b28b3557f03c4ce17b5bc Mon Sep 17 00:00:00 2001 From: mwrock Date: Thu, 1 Oct 2020 16:31:42 -0700 Subject: add interpreter arg to powershell_out allowing it to call pwsh.exe Signed-off-by: mwrock --- lib/chef/mixin/powershell_out.rb | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) (limited to 'lib') diff --git a/lib/chef/mixin/powershell_out.rb b/lib/chef/mixin/powershell_out.rb index 1ecc9bac98..6d0989f8e0 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 %i{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 -- cgit v1.2.1