Skip to content

Instantly share code, notes, and snippets.

@PrzemyslawKlys
Last active April 7, 2021 11:49
Show Gist options
  • Save PrzemyslawKlys/8e5668c786222b7ce04c8d4b525e32a4 to your computer and use it in GitHub Desktop.
Save PrzemyslawKlys/8e5668c786222b7ce04c8d4b525e32a4 to your computer and use it in GitHub Desktop.
Dynamically generate Where-Object based on PSCustomObject
$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