2016-09-22 12:43:57 +00:00
|
|
|
---
|
|
|
|
title: About the powershell Resource
|
2018-02-16 00:28:15 +00:00
|
|
|
platform: windows
|
2016-09-22 12:43:57 +00:00
|
|
|
---
|
|
|
|
|
|
|
|
# powershell
|
|
|
|
|
2019-04-26 18:24:29 +00:00
|
|
|
Use the `powershell` Chef InSpec audit resource to test a Powershell script on the Windows platform.
|
2016-09-22 12:43:57 +00:00
|
|
|
|
2017-10-03 21:35:10 +00:00
|
|
|
<br>
|
|
|
|
|
2018-08-09 12:34:49 +00:00
|
|
|
## Availability
|
|
|
|
|
|
|
|
### Installation
|
|
|
|
|
2019-04-26 18:24:29 +00:00
|
|
|
This resource is distributed along with Chef InSpec itself. You can use it automatically.
|
2018-08-09 12:34:49 +00:00
|
|
|
|
|
|
|
### Version
|
|
|
|
|
|
|
|
This resource first became available in v1.0.0 of InSpec.
|
|
|
|
|
2016-09-27 19:03:23 +00:00
|
|
|
## Syntax
|
2016-09-22 12:43:57 +00:00
|
|
|
|
|
|
|
A `powershell` resource block declares a Powershell script to be tested, and then compares the output of that command to the matcher in the test:
|
|
|
|
|
|
|
|
script = <<-EOH
|
|
|
|
# a PowerShell script
|
|
|
|
EOH
|
|
|
|
|
2016-11-20 01:37:24 +00:00
|
|
|
describe powershell(script) do
|
2018-02-01 10:51:12 +00:00
|
|
|
its('property') { should eq 'output' }
|
2016-09-22 12:43:57 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
where
|
|
|
|
|
|
|
|
* `'script'` must specify a Powershell script to be run
|
|
|
|
* `'matcher'` is one of `exit_status`, `stderr`, or `stdout`
|
|
|
|
* `'output'` tests the output of the command run on the system versus the output value stated in the test
|
|
|
|
|
2017-10-03 21:35:10 +00:00
|
|
|
<br>
|
2016-09-22 12:43:57 +00:00
|
|
|
|
2016-09-27 19:03:23 +00:00
|
|
|
## Examples
|
2016-09-22 12:43:57 +00:00
|
|
|
|
2019-04-26 18:24:29 +00:00
|
|
|
The following examples show how to use this Chef InSpec audit resource.
|
2016-09-22 12:43:57 +00:00
|
|
|
|
2016-09-27 19:03:23 +00:00
|
|
|
### Get all groups of Administrator user
|
2016-09-22 12:43:57 +00:00
|
|
|
|
|
|
|
script = <<-EOH
|
|
|
|
# find user
|
|
|
|
$user = Get-WmiObject Win32_UserAccount -filter "Name = 'Administrator'"
|
|
|
|
# get related groups
|
|
|
|
$groups = $user.GetRelated('Win32_Group') | Select-Object -Property Caption, Domain, Name, LocalAccount, SID, SIDType, Status
|
|
|
|
$groups | ConvertTo-Json
|
|
|
|
EOH
|
|
|
|
|
|
|
|
describe powershell(script) do
|
|
|
|
its('stdout') { should_not eq '' }
|
|
|
|
end
|
|
|
|
|
2016-09-27 19:03:23 +00:00
|
|
|
### Write-Output 'hello'
|
2016-09-22 12:43:57 +00:00
|
|
|
|
|
|
|
The following Powershell script:
|
|
|
|
|
|
|
|
script = <<-EOH
|
|
|
|
Write-Output 'hello'
|
|
|
|
EOH
|
|
|
|
|
|
|
|
can be tested in the following ways.
|
|
|
|
|
|
|
|
For a newline:
|
|
|
|
|
|
|
|
describe powershell(script) do
|
|
|
|
its('stdout') { should eq "hello\r\n" }
|
|
|
|
its('stderr') { should eq '' }
|
|
|
|
end
|
|
|
|
|
|
|
|
Removing whitespace `\r\n` from `stdout`:
|
|
|
|
|
|
|
|
describe powershell(script) do
|
|
|
|
its('strip') { should eq "hello" }
|
|
|
|
end
|
|
|
|
|
|
|
|
No newline:
|
|
|
|
|
|
|
|
describe powershell("'hello' | Write-Host -NoNewLine") do
|
|
|
|
its('stdout') { should eq 'hello' }
|
|
|
|
its('stderr') { should eq '' }
|
|
|
|
end
|
2017-10-03 21:35:10 +00:00
|
|
|
|
|
|
|
<br>
|
|
|
|
|
|
|
|
## Matchers
|
|
|
|
|
2018-02-16 03:07:18 +00:00
|
|
|
For a full list of available matchers, please visit our [matchers page](https://www.inspec.io/docs/reference/matchers/).
|
2017-10-03 21:35:10 +00:00
|
|
|
|
|
|
|
### exit_status
|
|
|
|
|
|
|
|
The `exit_status` matcher tests the exit status for the command:
|
|
|
|
|
|
|
|
its('exit_status') { should eq 123 }
|
|
|
|
|
|
|
|
### stderr
|
|
|
|
|
|
|
|
The `stderr` matcher tests results of the command as returned in standard error (stderr):
|
|
|
|
|
|
|
|
its('stderr') { should eq 'error' }
|
|
|
|
|
|
|
|
### stdout
|
|
|
|
|
|
|
|
The `stdout` matcher tests results of the command as returned in standard output (stdout):
|
|
|
|
|
|
|
|
its('stdout') { should eq '/^1$/' }
|