Class: Chef::PowerShell

Inherits:
Object
  • Object
show all
Extended by:
FFI::Library
Defined in:
lib/chef/powershell.rb

Direct Known Subclasses

Pwsh

Defined Under Namespace

Classes: CommandFailed

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(script) ⇒ Object

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).

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

Parameters:

  • script (String)

    script to run


37
38
39
40
41
42
43
44
45
# 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

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors


26
27
28
# File 'lib/chef/powershell.rb', line 26

def errors
  @errors
end

#resultObject (readonly)

Returns the value of attribute result


25
26
27
# File 'lib/chef/powershell.rb', line 25

def result
  @result
end

#verboseObject (readonly)

Returns the value of attribute verbose


27
28
29
# File 'lib/chef/powershell.rb', line 27

def verbose
  @verbose
end

Instance Method Details

#error!Object

Raises:


63
64
65
# File 'lib/chef/powershell.rb', line 63

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

#error?Boolean

Was there an error running the command

Returns:

  • (Boolean)

52
53
54
55
56
# File 'lib/chef/powershell.rb', line 52

def error?
  return true if errors.count > 0

  false
end