Class: Inspec::Resources::Powershell

Inherits:
Cmd
  • Object
show all
Defined in:
lib/inspec/resources/powershell.rb

Direct Known Subclasses

LegacyPowershell, VBScript

Instance Attribute Summary

Attributes inherited from Cmd

#command

Instance Method Summary collapse

Methods inherited from Cmd

#exit_status, #result, #stderr, #stdout

Constructor Details

#initialize(script) ⇒ Powershell

Returns a new instance of Powershell.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/inspec/resources/powershell.rb', line 20

def initialize(script)
  # PowerShell is the default shell on Windows, use the `command` resource
  return super(script) if inspec.os.windows?

  unless inspec.command("pwsh").exist?
    raise Inspec::Exceptions::ResourceSkipped, "Can not find `pwsh` command"
  end

  # Prevent progress stream from leaking into stderr
  command = "$ProgressPreference='SilentlyContinue';" + script

  # Encode as Base64 to remove any quotes/escapes/etc issues
  command = command.encode("UTF-16LE", "UTF-8")
  command = Base64.strict_encode64(command)

  # Use the `command` resource to execute the command via `pwsh`
  super("pwsh -encodedCommand '#{command}'")
end

Instance Method Details

#exist?Boolean

we cannot determine if a command exists, because that does not work for scripts

Returns:

  • (Boolean)


40
41
42
# File 'lib/inspec/resources/powershell.rb', line 40

def exist?
  nil
end

#resource_idObject



53
54
55
# File 'lib/inspec/resources/powershell.rb', line 53

def resource_id
  "Powershell"
end

#stripObject

Removes leading and trailing whitespace from stdout



45
46
47
# File 'lib/inspec/resources/powershell.rb', line 45

def strip
  result.stdout&.strip
end

#to_sObject



49
50
51
# File 'lib/inspec/resources/powershell.rb', line 49

def to_s
  "Powershell"
end