Powershell array:不包含名为 Contains 的方法



>我有这个Powershell代码:

$find_filesize = @(123, 395, 4929, 92345)
#$fpath is a file item
$fsize = (Get-Item $fpath).length
if ( $find_filesize.Contains($fsize) ) { 
... }

但它返回此错误:

Method of invocation failed because [System.Object[]] doesn't contain a method named 'Contains'.
At powershell.ps1:4
+ if ( $find_filesize.Contains($fsize) ) { 

我也试过:

[]int32[]]$$find_filesize = 123, 395, 4929, 92345

但这给出了同样的错误。

我错过了什么?据我了解,我将其定义为数组。

像这样尝试:

$find_filesize = @(123, 395, 4929, 92345)
#$fpath is a file item
$fsize = (Get-Item $fpath).length
if ( $find_filesize -match $fsize ) { 
... }

您也可以使用contains运算符执行此操作。不过,我总是选择match作为我的首选运营商。不能在数组上调用包含方法。

最新更新