diff options
author | mwrock <matt@mattwrock.com> | 2020-10-01 16:31:42 -0700 |
---|---|---|
committer | mwrock <matt@mattwrock.com> | 2020-10-01 16:31:42 -0700 |
commit | c811d58a84cc2b92a24b28b3557f03c4ce17b5bc (patch) | |
tree | 8297a747d40a65822eccc55c8df57fe3c3e31967 /lib | |
parent | 6b077d998a0c3168cb7edb19d9d70d122ecafc95 (diff) | |
download | chef-c811d58a84cc2b92a24b28b3557f03c4ce17b5bc.tar.gz |
add interpreter arg to powershell_out allowing it to call pwsh.exe
Signed-off-by: mwrock <matt@mattwrock.com>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/chef/mixin/powershell_out.rb | 17 |
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..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 |