07-21-2023, 03:05 PM
I need to determine if an array of `PSCustomObject`s contains an item with its `Title` property matching a value. I need a Boolean value for use with [Pester][1] assertions:
$Items -<function> $Name | Should Be $True
Assuming:
$Items = @()
$Items += [PsCustomObject]@{Title='foo';Url='http://f.io'}
$Items += [PsCustomObject]@{Title='bar';Url='http://b.io'}
`Contains` does not work:
PS> $Items -contains 'foo'
False
`Match` returns the matching instance, but it is not a Boolean:
PS> $Items -match 'foo'
Title Url
----- ---
foo
I suppose I could:
($Items -Match $Name).Count | Should Be 1
Is there a better option?
[1]:
$Items -<function> $Name | Should Be $True
Assuming:
$Items = @()
$Items += [PsCustomObject]@{Title='foo';Url='http://f.io'}
$Items += [PsCustomObject]@{Title='bar';Url='http://b.io'}
`Contains` does not work:
PS> $Items -contains 'foo'
False
`Match` returns the matching instance, but it is not a Boolean:
PS> $Items -match 'foo'
Title Url
----- ---
foo
[To see links please register here]
I suppose I could:
($Items -Match $Name).Count | Should Be 1
Is there a better option?
[1]:
[To see links please register here]