Class: Inspec::Resources::PowershellScript
- Defined in:
- lib/resources/powershell.rb
Direct Known Subclasses
Instance Attribute Summary
Attributes inherited from Cmd
Instance Method Summary collapse
-
#exist? ⇒ Boolean
we cannot determine if a command exists, because that does not work for scripts.
-
#initialize(script) ⇒ PowershellScript
constructor
A new instance of PowershellScript.
-
#strip ⇒ Object
Removes leading and trailing whitespace from stdout.
- #to_s ⇒ Object
Methods inherited from Cmd
#exit_status, #result, #stderr, #stdout
Constructor Details
#initialize(script) ⇒ PowershellScript
Returns a new instance of PowershellScript.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/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
40 41 42 |
# File 'lib/resources/powershell.rb', line 40 def exist? nil end |
#strip ⇒ Object
Removes leading and trailing whitespace from stdout
45 46 47 |
# File 'lib/resources/powershell.rb', line 45 def strip result.stdout&.strip end |
#to_s ⇒ Object
49 50 51 |
# File 'lib/resources/powershell.rb', line 49 def to_s 'Powershell' end |