Last active
January 10, 2017 21:38
-
-
Save cdhunt/7b0c4547ecadb6d0a3e3db2716c589ca to your computer and use it in GitHub Desktop.
Messing around with a filter function that is a mashup of Where-Object and If/Then. It might be a solution looking for a problem, but it's also fairly versatile. Do you see any value in it?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Where-ObjectMaybe | |
{ | |
[CmdletBinding(DefaultParameterSetName='Just')] | |
[Alias('Maybe')] | |
Param | |
( | |
[Parameter(ValueFromPipeline=$true, | |
Position=4)] | |
$InputObject, | |
[Parameter(Position=0)] | |
[Type] | |
$Type, | |
[Parameter(ParameterSetName='Just', Position=1)] | |
[object] | |
$Just, | |
[Parameter(ParameterSetName='Of', Position=1)] | |
[object[]] | |
$Of, | |
[Parameter(ParameterSetName='FilterScript', Position=2)] | |
[ScriptBlock] | |
$FilterScript, | |
[Parameter(ParameterSetName='Just', Position=3)] | |
[Parameter(ParameterSetName='Of', Position=3)] | |
[Parameter(ParameterSetName='FilterScript', Position=3)] | |
[object] | |
$OtherWise | |
) | |
Begin | |
{ | |
$yesMaybe = $false | |
} | |
Process | |
{ | |
foreach ($i in $InputObject) | |
{ | |
if ($PSBoundParameters.ContainsKey('Type') -and $PSBoundParameters.ContainsKey('Just')) | |
{ | |
if ($i -is $Type -and $i -eq $Just) { | |
Write-Output -InputObject $i | |
$yesMaybe = $true | |
} | |
} | |
elseif ($PSBoundParameters.ContainsKey('Just')) | |
{ | |
if ($i -eq $Just) { | |
Write-Output -InputObject $i | |
$yesMaybe = $true | |
} | |
} | |
elseif ($PSBoundParameters.ContainsKey('Type') -and $PSBoundParameters.ContainsKey('Of')) | |
{ | |
if ($i -is $Type -and $Of -contains $i) { | |
Write-Output -InputObject $i | |
$yesMaybe = $true | |
} | |
} | |
elseif ($PSBoundParameters.ContainsKey('Of')) | |
{ | |
if ($Of -contains $i) { | |
Write-Output -InputObject $i | |
$yesMaybe = $true | |
} | |
} | |
elseif ($PSBoundParameters.ContainsKey('Type') -and $PSBoundParameters.ContainsKey('FilterScript')) | |
{ | |
if ($i -is $Type -and $FilterScript.InvokeWithContext($null, (New-Object PSVariable @('_', $i)), $null)) { | |
Write-Output -InputObject $i | |
$yesMaybe = $true | |
} | |
} | |
elseif ($PSBoundParameters.ContainsKey('FilterScript')) | |
{ | |
if ($FilterScript.InvokeWithContext($null, (New-Object PSVariable @('_', $i)), $null)) { | |
Write-Output -InputObject $i | |
$yesMaybe = $true | |
} | |
} | |
elseif ($PSBoundParameters.ContainsKey('Type')) | |
{ | |
if ($i -is $Type) { | |
Write-Output -InputObject $i | |
$yesMaybe = $true | |
} | |
} | |
} | |
} | |
End | |
{ | |
if ($PSBoundParameters.ContainsKey('OtherWise') -and -not $yesMaybe) | |
{ | |
Write-Output -InputObject $OtherWise | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Write-Host '1 | maybe -Just 1' -ForegroundColor Yellow | |
1 | Maybe -Just 1 | |
Write-Host '1 | maybe -Just 2' -ForegroundColor Yellow | |
1 | Maybe -Just 2 | |
Write-Host '1 | maybe -Just 2 -OtherWise -1' -ForegroundColor Yellow | |
1 | Maybe -Just 2 -OtherWise -1 | |
Write-Host 'echo 1 2 3 | Maybe -Just 4 -OtherWise $false' -ForegroundColor Yellow | |
echo 1 2 3 | Maybe -Just 4 -OtherWise $false | |
Write-Host '1 | maybe int -OtherWise -1' -ForegroundColor Yellow | |
1 | Maybe int -OtherWise -1 | |
Write-Host "'string' | maybe int -OtherWise -1" -ForegroundColor Yellow | |
'string' | Maybe int -OtherWise -1 | |
Write-Host '$null | maybe int' -ForegroundColor Yellow | |
$null | Maybe -just int | |
Write-Host '$null | maybe int -OtherWise 0' -ForegroundColor Yellow | |
$null | Maybe int -OtherWise 0 | |
Write-Host '$null | Maybe -OtherWise $false' -ForegroundColor Yellow | |
$null | Maybe -OtherWise $false | |
Write-Host 'echo 1 2 3 | Maybe -Of @(1,2)' -ForegroundColor Yellow | |
echo 1 2 3 | Maybe -Of @(1,2) | |
Write-Host 'echo 1 2 b | Maybe -Of @(1,"b")' -ForegroundColor Yellow | |
echo 1 2 b | Maybe -Of @(1,'b') | |
Write-Host 'echo 1 2 b | Maybe int' -ForegroundColor Yellow | |
echo 1 2 b | Maybe int | |
Write-Host 'echo 1 2 b | Maybe int -of @(2,3)' -ForegroundColor Yellow | |
echo 1 2 b | Maybe int -of @(2,3) | |
Write-Host 'echo 1 2 3 | Maybe -Of @(4,5) -OtherWise @()' -ForegroundColor Yellow | |
$array = echo 1 2 3 | Maybe -Of @(4,5) -OtherWise @() | |
$array.count | |
Write-Host "Get-Process | Maybe -FilterScript {$_.Name -eq 'PowerShell'} -OtherWise 'No instance of PowerShell is running'" -ForegroundColor Yellow | |
Get-Process | Maybe -FilterScript {$_.Name -eq 'PowerShell'} -OtherWise 'No instance of PowerShell is running' | |
Write-Host "Get-Process | Maybe -FilterScript {$_.Name -eq 'NotARealProcess'} -OtherWise 'No instance of NotARealProcess is running'" -ForegroundColor Yellow | |
Get-Process | Maybe -FilterScript {$_.Name -eq 'NotARealProcess'} -OtherWise 'No instance of NotARealProcess is running' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PS> 1 | maybe -Just 1 | |
1 | |
PS> 1 | maybe -Just 2 | |
PS> 1 | maybe -Just 2 -OtherWise -1 | |
-1 | |
PS> echo 1 2 3 | Maybe -Just 4 -OtherWise $false | |
False | |
PS> 1 | maybe int -OtherWise -1 | |
1 | |
PS> 'string' | maybe int -OtherWise -1 | |
-1 | |
PS> $null | maybe int | |
PS> $null | maybe int -OtherWise 0 | |
0 | |
PS> $null | Maybe -OtherWise $false | |
False | |
PS> echo 1 2 3 | Maybe -Of @(1,2) | |
1 | |
2 | |
PS> echo 1 2 b | Maybe -Of @(1,"b") | |
1 | |
b | |
PS> echo 1 2 b | Maybe int | |
1 | |
2 | |
PS> echo 1 2 b | Maybe int -of @(2,3) | |
2 | |
PS> echo 1 2 3 | Maybe -Of @(4,5) -OtherWise @() | |
0 | |
PS> Get-Process | Maybe -FilterScript {.Name -eq 'PowerShell'} -OtherWise 'No instance of PowerShell is running' | |
Handles NPM(K) PM(K) WS(K) CPU(s) Id SI ProcessName | |
------- ------ ----- ----- ------ -- -- ----------- | |
1406 63 274732 4248 27.31 1112 1 powershell | |
616 36 77100 16344 9.73 5080 1 powershell | |
757 40 118064 56484 3.13 38680 1 powershell | |
PS> Get-Process | Maybe -FilterScript {.Name -eq 'NotARealProcess'} -OtherWise 'No instance of NotARealProcess is running' | |
No instance of NotARealProcess is running |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment