我有一个类似的powershell对象数组
$EdosContainers= @([pscustomobject]@{Container='FoundationObjectStoreConfigMetadata';PartitionKey='/templateName'}
[pscustomobject]@{Container='FoundationObjectStoreObjectInformation';PartitionKey='/processExecutionId'}
[pscustomobject]@{Container='FoundationObjectStoreObjectMetadata';PartitionKey='/processExecutionId'}
[pscustomobject]@{Container='FoundationObjectStoreConfigMetadataTransfomed';PartitionKey='/templateName'})
但我需要检查给定的值是否存在于数组中或容器属性中。
我知道我们可以使用-contains方法来检查数组中是否存在项。但既然我的是一个对象数组,我们怎么能做到这一点呢?
您可以利用成员访问枚举,通过直接将属性访问应用于数组,从数组的元素中提取所有.Container
属性值,从而允许您使用-contains
:
# -> $true
$EdosContainers.Container -contains 'FoundationObjectStoreConfigMetadataTransfomed'
如果要避免隐式创建包含所有.Container
值的数组,请使用内部.Where()
方法:
[bool] $EdosContainers.Where(
{ $_.Container -eq 'FoundationObjectStoreConfigMetadataTransfomed' },
'First' # Stop, once a match is found.
)
虽然这比使用成员访问枚举更节省内存,但对于大型数组,它也明显较慢。