我有一个文件对象列表,我想检查一个给定的文件对象是否出现在该列表中。 -Contains
运营商几乎是我正在寻找的,但似乎-Contains
使用非常严格的相等性测试,其中对象引用必须相同。有一些不太严格的选择吗?我希望以下代码中的$boolean
变量第二次以及第一次返回True
。替代“-Contains”比较值而不是参考
PS C:\Users\Public\Documents\temp> ls
Directory: C:\Users\Public\Documents\temp
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 14.08.2017 18.33 5 file1.txt
-a---- 14.08.2017 18.33 5 file2.txt
PS C:\Users\Public\Documents\temp> $files1 = Get-ChildItem .
PS C:\Users\Public\Documents\temp> $files2 = Get-ChildItem .
PS C:\Users\Public\Documents\temp> $file = $files1[1]
PS C:\Users\Public\Documents\temp> $boolean = $files1 -Contains $file
PS C:\Users\Public\Documents\temp> $boolean
True
PS C:\Users\Public\Documents\temp> $boolean = $files2 -Contains $file
PS C:\Users\Public\Documents\temp> $boolean
False
PS C:\Users\Public\Documents\temp>
您使用的是什么版本的PowerShell? –
'$ files2 = $ files1.Clone()'或使用'Where-Object' – wOxxOm
'[bool](Compare-Object $ files2 $ file -IncludeEqual -ExcludeDifferent)' – BenH