Last active
April 7, 2021 11:49
-
-
Save PrzemyslawKlys/8e5668c786222b7ce04c8d4b525e32a4 to your computer and use it in GitHub Desktop.
Dynamically generate Where-Object based on PSCustomObject
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
$Test = [PSCustomObject] @{ | |
Name = 'Przemek' | |
Surname = 'Klys' | |
"Difffrent parametr" = 'ok' | |
} | |
function Get-WhereObject { | |
[cmdletBinding()] | |
param( | |
[Parameter(Mandatory, Position = 0, ValueFromPipeline)][PSCustomObject] $Test | |
) | |
[string] $Output = @( | |
$Count = 0 | |
"Where-Object {" | |
foreach ($Name in $Test.PSObject.Properties.Name) { | |
$Count++ | |
"`$_." + "'$Name' -eq '$($Test.($Name))'" | |
if ($Count -lt $Test.PSObject.Properties.Name.Count) { | |
" -and " | |
} | |
} | |
"}" | |
) | |
[scriptblock]::Create($Output) | |
} | |
$Test | Get-WhereObject |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment