Skip to content

Instantly share code, notes, and snippets.

@randomchance
Created August 18, 2016 20:50
Show Gist options
  • Save randomchance/90ea0722c27dd709daf393cb97a96a78 to your computer and use it in GitHub Desktop.
Save randomchance/90ea0722c27dd709daf393cb97a96a78 to your computer and use it in GitHub Desktop.
Simple ANY and ALL functions for PowerShell.
<#
.SYNOPSIS
Assert-All returns true if EVERY object in the pipeline evaluates to true.
.DESCRIPTION
Assert-All returns true if every item in the pipeline evaluates to true.
Assert-All will not return until the entire pipeline has been processed.
Thanks to a feature in PowerShell that will invoke a scriptblock with the current pipeline object, you can
pass a scriptblock that returns a boolean into the Filter parameter.
The Scriptblock syntax is the same as a filter function, for example: {$_ -like "FancyStuff"}.
I'll eventually create a module for this stuff, until then direct questions to https://github.com/randomchance
.PARAMETER Filter
An expression evaluated against the pipeline.
.EXAMPLE
($true,$true,$true) | Assert-All
True
.EXAMPLE
($true,$false,$true) | Assert-All
False
.EXAMPLE
(0..5) | Assert-All -Filter {$_ -gt 0}
True
.EXAMPLE
(0..5) | Assert-All -Filter {$_ -lt 3} -Verbose
VERBOSE: [ ] 0
VERBOSE: [ ] 1
VERBOSE: [ ] 2
VERBOSE: [ X ] 3
VERBOSE: [ X ] 4
VERBOSE: [ X ] 5
False
.EXAMPLE
ls | Assert-All -Filter {$_.Name.Length -lt 3}
.OUTPUTS
System.Boolean
#>
function Assert-All{
[cmdletbinding()]
param(
[parameter(ValueFromPipeLine=$true,Mandatory=$true)]
[bool]$Filter
)
begin{
$EverythingIsAwesome = $true
}
process{
if($Filter -eq $false){
Write-Verbose "[ X ] $_"
$EverythingIsAwesome = $false
} else {
Write-Verbose "[ ] $_"
}
}
end {
$EverythingIsAwesome
}
}
<#
.SYNOPSIS
Assert-Any returns true if ANY object in the pipeline evaluates to true.
.DESCRIPTION
Assert-Any returns true if any item in the pipeline evaluates to true.
Assert-Any will not return until the entire pipeline has been processed.
Thanks to a feature in PowerShell that will invoke a scriptblock with the current pipeline object, you can
pass a scriptblock that returns a boolean into the Filter parameter.
The Scriptblock syntax is the same as a filter function, for example: {$_ -like "FancyStuff"}.
I'll eventually create a module for this stuff, until then direct questions to https://github.com/randomchance
.PARAMETER Filter
An expression evaluated against the pipeline.
.EXAMPLE
($false,$false,$false) | Assert-Any
False
.EXAMPLE
($true,$false,$false) | Assert-Any
True
.EXAMPLE
(0..5) | Assert-Any -Filter {$_ -eq 4} -Verbose
VERBOSE: [ ] 0
VERBOSE: [ ] 1
VERBOSE: [ ] 2
VERBOSE: [ ] 3
VERBOSE: [ X ] 4
VERBOSE: [ ] 5
True
.EXAMPLE
(0..5) | Assert-Any -Filter {$_ -gt 6} -Verbose
VERBOSE: [ ] 0
VERBOSE: [ ] 1
VERBOSE: [ ] 2
VERBOSE: [ ] 3
VERBOSE: [ ] 4
VERBOSE: [ ] 5
False
.EXAMPLE
ls | Assert-Any -Filter {$_.Name.Length -lt 3}
.OUTPUTS
System.Boolean
#>
function Assert-Any{
[cmdletbinding()]
param(
[parameter(ValueFromPipeLine=$true,Mandatory=$true)]
[bool]$Filter
)
begin{
$SomethingIsAwesome = $false
}
process{
if($Filter -eq $true){
Write-Verbose "[ X ] $_"
$SomethingIsAwesome = $true
} else {
Write-Verbose "[ ] $_"
}
}
end{
$SomethingIsAwesome
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment