Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine if an array of PSCustomObject's contains an instance with a property value

Tags:

I need to determine if an array of PSCustomObjects contains an item with its Title property matching a value. I need a Boolean value for use with Pester 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    http://f.io 

I suppose I could:

($Items -Match $Name).Count | Should Be 1 

Is there a better option?

like image 662
craig Avatar asked Jun 19 '15 13:06

craig


1 Answers

Use:

$Items.Title -contains 'foo' 
like image 147
Eris Avatar answered Oct 07 '22 00:10

Eris