<NME> powershell.rb
<BEF> #
# Author:: Stuart Preston (<stuart@chef.io>)
# Copyright:: Copyright (c) Chef Software Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

require "ffi" unless defined?(FFI)
require_relative "json_compat"

class Chef
  class PowerShell
    extend FFI::Library

    attr_reader :result
    attr_reader :errors
    attr_reader :verbose

    # Run a command under PowerShell via FFI
    # This implementation requires the managed dll and native wrapper to be in the library search
    # path on Windows (i.e. c:\windows\system32 or in the same location as ruby.exe).
    #
    # @param script [String] script to run
    # @return [Object] output
    def initialize(*command_args)
      raise "This class can only be used on the Windows platform." unless RUBY_PLATFORM =~ /mswin|mingw32|windows/
      exec(command_args.first)
    end

    def exec(script)
      ps = WIN32OLE.new("Chef.PowerShell")
      outcome = ps.ExecuteScript(script)
    #
      @result = Chef::JSONCompat.parse(hashed_outcome["result"])
      @errors = hashed_outcome["errors"]
    end

    def error?
      return true if errors.count > 0
      false
    end
  end
end
      false
    end

    class CommandFailed < RuntimeError; end

    #
    # @raise [Chef::PowerShell::CommandFailed] raise if the command failed
    #
    def error!
      raise Chef::PowerShell::CommandFailed, "Unexpected exit in PowerShell command: #{@errors}" if error?
    end

    private

    def exec(script, timeout: -1)
      FFI.ffi_lib @dll
      FFI.attach_function :execute_powershell, :ExecuteScript, %i{string int}, :pointer
      timeout = -1 if timeout == 0 || timeout.nil?
      execution = FFI.execute_powershell(script, timeout).read_utf16string
      hashed_outcome = Chef::JSONCompat.parse(execution)
      @result = Chef::JSONCompat.parse(hashed_outcome["result"])
      @errors = hashed_outcome["errors"]
      @verbose = hashed_outcome["verbose"]
    end
  end
end

<MSG> Addressing initial feedback from review

Signed-off-by: Stuart Preston <1c8c08db85a36cc69d975124539be839dea91342@chef.io>

<DFF> @@ -32,11 +32,18 @@ class Chef
     #
     # @param script [String] script to run
     # @return [Object] output
-    def initialize(*command_args)
-      raise "This class can only be used on the Windows platform." unless RUBY_PLATFORM =~ /mswin|mingw32|windows/
-      exec(command_args.first)
+    def initialize(script)
+      raise "Chef::PowerShell can only be used on the Windows platform." unless RUBY_PLATFORM =~ /mswin|mingw32|windows/
+      exec(script)
     end
 
+    def error?
+      return true if errors.count > 0
+      false
+    end
+    
+    private
+
     def exec(script)
       ps = WIN32OLE.new("Chef.PowerShell")
       outcome = ps.ExecuteScript(script)
@@ -44,10 +51,5 @@ class Chef
       @result = Chef::JSONCompat.parse(hashed_outcome["result"])
       @errors = hashed_outcome["errors"]
     end
-
-    def error?
-      return true if errors.count > 0
-      false
-    end
   end
 end

{'addition_count': 10, 'commit_subject': 'Addressing initial feedback from review', 'deletion_count': 8, 'file_extension': '.rb', 'lang': 'rb', 'license': 'apache-2.0', 'repo_name': 'chef/chef'}