如何查找 Powershell 数组是否包含另一个数组的对象

cra*_*mmy 4 arrays powershell

我必须在 Powershell 中排列数组。每个数组包含一个对象数组。这些对象有两个属性:

名称:字符串
ID:GUID

第一个 Array 中有 4413 个对象,第二个有 4405 个对象。计数无关,但我只提到它们是为了注意 Array1 和 Array2 的内容不同。

这是我当前的代码(伪):

#Fill Array1
$Array1 = Fill-Array1

#Fill Array2
$Array2 = Fill-Array2

#Loop through the arrays and write out the names of all items in Array2 that are different than Array1
ForEach($Val in $Array2)
{
    $Name = $Val.Name

    If($Array1 -notcontains $Val) //this does not work
    {
        Write-Host $Name
    }
}
Run Code Online (Sandbox Code Playgroud)

检查 Array1 中对象是否存在的正确方法是什么?是我做嵌套循环的唯一选择吗?

更新,使用下面Manu P的答案,以下是我实施解决方案的方式:

#Fill Array1
    $Array1 = Fill-Array1

    #Fill Array2
    $Array2 = Fill-Array2

    #Compare the arrays
    $ComparedResults = Compare-Object -ReferenceObject $Array1 -DifferenceObject $Array2 #I left out the -IncludeEqual param because I don't care about those results

    ForEach($Result in $ComparedResults)
    {
        If($Result.SideIndicator -eq "=>") #the value in Array2 is different than Array1
        {
            $Val = $Result.InputObject

            Write-Host $Val.Name            
        }        
    }
Run Code Online (Sandbox Code Playgroud)