Is it possible to pass a where clause to an advanced function? An example to make it more clear;
$Fruits = @(
@{
Name = 'Kiwi'
Color = 'Green'
}
@{
Name = 'Banana'
Color = 'Yellow'
}
)
Function Get-Stuff {
Param (
[scriptblock]$Filter,
[hashtable[]]$Collection
)
$Collection.Where( { $Filter })
}
Get-Stuff -Filter { $_.Name -eq 'Kiwi' } -Collection $Fruits
In this case it would be great if the function can return the same as $Fruits.Where( { $_.Name -eq 'Kiwi' }).
As ansgar-wiechers stated in the above comment, you have to remove the outer scriptblock literal ({}):
$Fruits = @(
@{
Name = 'Kiwi'
Color = 'Green'
}
@{
Name = 'Banana'
Color = 'Yellow'
}
)
Function Get-Stuff {
Param (
[scriptblock]$Filter,
[hashtable[]]$Collection
)
# Subexpression removed.
$Collection.Where($Filter)
}
Get-Stuff -Filter { $_.Name -eq 'Kiwi' } -Collection $Fruits
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With