2015-10-07 11:04:40 +00:00
|
|
|
# encoding: utf-8
|
|
|
|
# copyright: 2015, Vulcano Security GmbH
|
|
|
|
|
2016-03-08 18:06:55 +00:00
|
|
|
module Inspec::Resources
|
2016-03-18 11:29:36 +00:00
|
|
|
class PowershellScript < Cmd
|
|
|
|
name 'powershell'
|
2018-02-19 14:26:49 +00:00
|
|
|
supports platform: 'windows'
|
2018-04-12 21:57:56 +00:00
|
|
|
supports platform: 'unix'
|
2016-03-18 11:29:36 +00:00
|
|
|
desc 'Use the powershell InSpec audit resource to test a Windows PowerShell script on the Microsoft Windows platform.'
|
2019-03-19 14:17:32 +00:00
|
|
|
example <<~EXAMPLE
|
2016-03-08 18:06:55 +00:00
|
|
|
script = <<-EOH
|
2017-04-20 15:43:33 +00:00
|
|
|
# your powershell script
|
2016-03-08 18:06:55 +00:00
|
|
|
EOH
|
2015-11-27 13:02:38 +00:00
|
|
|
|
2016-03-18 11:29:36 +00:00
|
|
|
describe powershell(script) do
|
2016-03-08 18:06:55 +00:00
|
|
|
its('matcher') { should eq 'output' }
|
|
|
|
end
|
2019-03-19 14:17:32 +00:00
|
|
|
EXAMPLE
|
2015-10-07 11:13:37 +00:00
|
|
|
|
2016-03-08 18:06:55 +00:00
|
|
|
def initialize(script)
|
2018-03-29 15:57:15 +00:00
|
|
|
# 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}'")
|
2016-03-08 18:06:55 +00:00
|
|
|
end
|
2015-10-07 11:04:40 +00:00
|
|
|
|
2016-03-08 18:06:55 +00:00
|
|
|
# we cannot determine if a command exists, because that does not work for scripts
|
|
|
|
def exist?
|
|
|
|
nil
|
|
|
|
end
|
2015-10-12 11:01:58 +00:00
|
|
|
|
2016-03-26 21:25:53 +00:00
|
|
|
# Removes leading and trailing whitespace from stdout
|
|
|
|
def strip
|
2017-11-21 07:49:41 +00:00
|
|
|
result.stdout&.strip
|
2016-03-26 21:25:53 +00:00
|
|
|
end
|
|
|
|
|
2016-03-08 18:06:55 +00:00
|
|
|
def to_s
|
2016-03-18 11:29:36 +00:00
|
|
|
'Powershell'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# this is deprecated syntax and will be removed in future versions
|
|
|
|
class LegacyPowershellScript < PowershellScript
|
|
|
|
name 'script'
|
|
|
|
|
|
|
|
def initialize(script)
|
|
|
|
deprecated
|
|
|
|
super(script)
|
|
|
|
end
|
|
|
|
|
|
|
|
def deprecated
|
|
|
|
warn '[DEPRECATION] `script(script)` is deprecated. Please use `powershell(script)` instead.'
|
2016-03-08 18:06:55 +00:00
|
|
|
end
|
2015-10-12 11:01:58 +00:00
|
|
|
end
|
2015-10-07 11:04:40 +00:00
|
|
|
end
|