Class: PuppetX::PowerShellModule::Helper

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/puppet_x/encore/powershellmodule/helper.rb

Overview

Helper class for Caching Instances

Instance Method Summary collapse

Instance Method Details

#full_cmd(command) ⇒ Object



26
27
28
# File 'lib/puppet_x/encore/powershellmodule/helper.rb', line 26

def full_cmd(command)
  "#{setup_cmd}; #{sec_proto_cmd}; #{command}"
end

#powershell(command, fail_on_failure: true, full_result: false) ⇒ Object



66
67
68
69
70
71
# File 'lib/puppet_x/encore/powershellmodule/helper.rb', line 66

def powershell(command, fail_on_failure: true, full_result: false)
  cmd = full_cmd(command)
  Puppet.debug("Running powershell command: #{cmd}")
  result = powershell_instance.execute(cmd)
  process_result(result, cmd, fail_on_failure: fail_on_failure, full_result: full_result)
end

#powershell_instanceObject



60
61
62
63
64
# File 'lib/puppet_x/encore/powershellmodule/helper.rb', line 60

def powershell_instance
  # lazy initialize because this might not exist on a system
  @powershell ||= Pwsh::Manager.instance(Pwsh::Manager.powershell_path,
                                         Pwsh::Manager.powershell_args)
end

#process_result(result, command, fail_on_failure: true, full_result: false) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/puppet_x/encore/powershellmodule/helper.rb', line 30

def process_result(result, command, fail_on_failure: true, full_result: false)
  Puppet.debug("Result for command: #{command}\n stdout = #{result[:stdout]} \n stderr = #{result[:stderr]}")
  if fail_on_failure && result[:exitcode] != 0
    raise "Error when executing command: #{command}\n stdout = #{result[:stdout]} \n stderr = #{result[:stderr]}"
  end

  # should we return full result or stdout broken-up by lines
  if full_result
    result
  else
    # if stdout had nothing printed to it, it returns nil, instead we always
    # want an empty string
    stdout = result[:stdout] || ''
    stdout.lines
  end
end

#pwsh(command, fail_on_failure: true, full_result: false) ⇒ Object



53
54
55
56
57
58
# File 'lib/puppet_x/encore/powershellmodule/helper.rb', line 53

def pwsh(command, fail_on_failure: true, full_result: false)
  cmd = full_cmd(command)
  Puppet.debug("Running pwsh command: #{cmd}")
  result = pwsh_instance.execute(cmd)
  process_result(result, cmd, fail_on_failure: fail_on_failure, full_result: full_result)
end

#pwsh_instanceObject



47
48
49
50
51
# File 'lib/puppet_x/encore/powershellmodule/helper.rb', line 47

def pwsh_instance
  # lazy initialize because this might not exist on a system
  @pwsh ||= Pwsh::Manager.instance(Pwsh::Manager.pwsh_path,
                                   Pwsh::Manager.pwsh_args)
end

#sec_proto_cmdObject



18
19
20
21
22
23
24
# File 'lib/puppet_x/encore/powershellmodule/helper.rb', line 18

def sec_proto_cmd
  # The SecurityProtocol section of the -Command forces PowerShell to use TLSv1.2,
  # which is not enabled by default unless explicitly configured system-wide in the registry.
  # The PowerShell Gallery website enforces the use of TLSv1.2 for all incoming connections,
  # so without forcing TLSv1.2 here the command will fail.
  '[Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12'
end

#setup_cmdObject



14
15
16
# File 'lib/puppet_x/encore/powershellmodule/helper.rb', line 14

def setup_cmd
  "$ProgressPreference = 'SilentlyContinue'; $ErrorActionPreference = 'Stop'"
end