Skip to content

Instantly share code, notes, and snippets.

@stefanstranger
Last active March 6, 2022 01:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stefanstranger/98b6ca2f88a3b6e4c4365b5cc05e926a to your computer and use it in GitHub Desktop.
Save stefanstranger/98b6ca2f88a3b6e4c4365b5cc05e926a to your computer and use it in GitHub Desktop.
Testing dynamic where in PowerShell
$Test = [PSCustomObject] @{
FirstName = 'Stefan'
LastName = 'Stranger'
#"Difffrent parametr" = 'ok'
}
function Get-WhereObject {
[cmdletBinding()]
param(
[Parameter(Mandatory, Position = 0, ValueFromPipeline)][PSCustomObject] $Test
)
[string] $Output = @(
$Count = 0
foreach ($Name in $Test.PSObject.Properties.Name) {
$Count++
"`$_." + "'$Name' -eq '$($Test.($Name))'"
if ($Count -lt $Test.PSObject.Properties.Name.Count) {
" -and "
}
}
)
$Output
}
$Test | Get-WhereObject -OutVariable foo
Function Get-Example {
[CmdletBinding()]
param(
[parameter()][String]$FirstName,
[parameter()][String]$LastName
)
process {
return [PSCustomObject]@{
FirstName = $FirstName
LastName = $LastName
}
}
}
# Does not works
Get-Example -FirstName 'Stefan' -LastName 'Stranger' | Where-Object $([scriptblock]::Create($foo))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment