class Chef::PowerShell

Attributes

errors[R]
result[R]
verbose[R]

Public Class Methods

new(script) click to toggle source

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:windowssystem32 or in the same location as ruby.exe).

Requires: .NET Framework 4.0 or higher on the target machine.

@param script [String] script to run @return [Object] output

# File lib/chef/powershell.rb, line 37
def initialize(script)
  # This Powershell DLL source lives here: https://github.com/chef/chef-powershell-shim
  # Every merge into that repo triggers a Habitat build and promotion. Running
  # the rake :update_chef_exec_dll task in this (chef/chef) repo will pull down
  # the built packages and copy the binaries to distro/ruby_bin_folder. Bundle install
  # ensures that the correct architecture binaries are installed into the path.
  @dll ||= "Chef.PowerShell.Wrapper.dll"
  exec(script)
end

Public Instance Methods

error!() click to toggle source

@raise [Chef::PowerShell::CommandFailed] raise if the command failed

# File lib/chef/powershell.rb, line 63
def error!
  raise Chef::PowerShell::CommandFailed, "Unexpected exit in PowerShell command: #{@errors}" if error?
end
error?() click to toggle source

Was there an error running the command

@return [Boolean]

# File lib/chef/powershell.rb, line 52
def error?
  return true if errors.count > 0

  false
end

Protected Instance Methods

exec(script) click to toggle source
# File lib/chef/powershell.rb, line 69
def exec(script)
  FFI.ffi_lib @dll
  FFI.attach_function :execute_powershell, :ExecuteScript, [:string], :pointer
  execution = FFI.execute_powershell(script).read_utf16string
  hashed_outcome = Chef::JSONCompat.parse(execution)
  @result = Chef::JSONCompat.parse(hashed_outcome["result"])
  @errors = hashed_outcome["errors"]
  @verbose = hashed_outcome["verbose"]
end