Determine if the PsCustomObjects array contains an instance with a property value

I need to determine if the PsCustomObject array PsCustomObject an element with a Title property matching the value. I need a boolean value to use with Pester statements:

 $Items -<function> $Name | Should Be $True 

Assuming that:

 $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 corresponding instance, but not logical:

 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?

+6
source share
1 answer

From the comments:

 $Items.Title -contains 'foo' 
+8
source

Source: https://habr.com/ru/post/989404/


All Articles