Class: ChefPowerShell::PowerShell

Inherits:
Object
  • Object
show all
Defined in:
lib/chef-powershell/powershell.rb

Direct Known Subclasses

Pwsh

Defined Under Namespace

Modules: PowerMod

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(script, timeout: -1)) ⇒ 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:windowssystem32 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

  • timeout (Integer, nil) (defaults to: -1))

    timeout in seconds.



73
74
75
76
77
78
79
80
81
# File 'lib/chef-powershell/powershell.rb', line 73

def initialize(script, timeout: -1)
  # This Powershell DLL source lives here: https://github.com/chef/chef-powershell-shim

  # Every merge into that repo triggers a Habitat build and verification process.

  # There is no mechanism to build a Windows gem file. It has to be done manually running manual_gem_release.ps1

  # Bundle install ensures that the correct architecture binaries are installed into the path.

  # @powershell_dll = Gem.loaded_specs["chef-powershell"].full_gem_path + "/bin/ruby_bin_folder/#{ENV["PROCESSOR_ARCHITECTURE"]}/Chef.PowerShell.Wrapper.dll"

  @powershell_dll = self.class.resolve_wrapper_dll
  exec(script, timeout: timeout)
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



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

def errors
  @errors
end

#resultObject (readonly)

Returns the value of attribute result.



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

def result
  @result
end

#verboseObject (readonly)

Returns the value of attribute verbose.



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

def verbose
  @verbose
end

Class Method Details

.resolve_wrapper_dllObject

Raises:

  • (LoadError)


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/chef-powershell/powershell.rb', line 30

def self.resolve_wrapper_dll
  arch = ENV["PROCESSOR_ARCHITECTURE"] || "AMD64"
  searched = []

  # Gem path

  if Gem.loaded_specs["chef-powershell"]
    base = Gem.loaded_specs["chef-powershell"].full_gem_path
    gem_path = File.join(base, "bin", "ruby_bin_folder", arch, "Chef.PowerShell.Wrapper.dll")
    searched << gem_path
    return gem_path if File.exist?(gem_path)
  end

  # CHEF_POWERSHELL_BIN override

  if ENV["CHEF_POWERSHELL_BIN"]&.length.to_i > 0
    override = File.join(ENV["CHEF_POWERSHELL_BIN"], "Chef.PowerShell.Wrapper.dll")
    searched << override
    return override if File.exist?(override)
  end

  # Habitat package fallback (hab installed scenario)

  begin
    hab_path = `hab pkg path chef/chef-powershell-shim 2>NUL`.strip
    if hab_path != "" && File.directory?(hab_path)
      hab_dll = File.join(hab_path, "bin", "Chef.PowerShell.Wrapper.dll")
      searched << hab_dll
      return hab_dll if File.exist?(hab_dll)
    end
  rescue
    # ignore any hab errors, just proceed

  end

  raise LoadError, "Chef.PowerShell wrapper DLL not found. Searched: #{searched.join(", ")}. Populate binaries via 'rake update_chef_powershell_dlls' or set CHEF_POWERSHELL_BIN."
end

Instance Method Details

#error!Object

Raises:



97
98
99
# File 'lib/chef-powershell/powershell.rb', line 97

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

#error?Boolean

Was there an error running the command

Returns:

  • (Boolean)


88
89
90
91
92
# File 'lib/chef-powershell/powershell.rb', line 88

def error?
  return true if errors.any?

  false
end