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

时间:2017-08-09 16:43:03

标签: arrays powershell

我必须在Powershell中使用数组。每个Array都包含一个对象数组。这些对象有两个属性:

名称:字符串
ID :GUID

第一个数组中有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
    }
}

检查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            
        }        
    }

2 个答案:

答案 0 :(得分:4)

您可以使用Compare-Object cmdlet:

Compare-Object -ReferenceObject $Array1 -DifferenceObject $Array2 -IncludeEqual

https://docs.microsoft.com/fr-fr/powershell/module/Microsoft.PowerShell.Utility/Compare-Object?view=powershell-5.0

https://technet.microsoft.com/fr-fr/library/ee156812.aspx

答案 1 :(得分:1)

由于GUID具有可比性,您可以使用此处描述的函数简单地(内部)连接数组:https://stackoverflow.com/a/45483110/1701026

$Array1 = @(
    [pscustomobject]@{Name = "a"; Id = [guid]::NewGuid()}
    [pscustomobject]@{Name = "b"; Id = [guid]::NewGuid()}
    [pscustomobject]@{Name = "c"; Id = [guid]"de5e32c6-5338-4287-8c21-2dd5957cfffe"}
    [pscustomobject]@{Name = "d"; Id = [guid]"345f9ac2-86a0-4b01-b843-5f4bd55f5e21"}
    [pscustomobject]@{Name = "e"; Id = [guid]"953c1442-ed51-445e-a8f5-687120d98f83"}
    [pscustomobject]@{Name = "f"; Id = [guid]::NewGuid()}
)

$Array2 = @(
    [pscustomobject]@{Name = "b"; Id = [guid]::NewGuid()}
    [pscustomobject]@{Name = "c"; Id = [guid]"345f9ac2-86a0-4b01-b843-5f4bd55f5e21"}
    [pscustomobject]@{Name = "d"; Id = [guid]"345f9ac2-86a0-4b01-b843-5f4bd55f5e21"}
    [pscustomobject]@{Name = "e"; Id = [guid]"953c1442-ed51-445e-a8f5-687120d98f83"}
    [pscustomobject]@{Name = "f"; Id = [guid]"953c1442-ed51-445e-a8f5-687120d98f83"}
    [pscustomobject]@{Name = "g"; Id = [guid]::NewGuid()}
)

$Array1 | InnerJoin $Array2 -Using Name,Id | Format-Table

结果:

Id                                   Name
--                                   ----
345f9ac2-86a0-4b01-b843-5f4bd55f5e21 d
953c1442-ed51-445e-a8f5-687120d98f83 e

获取Id不相等的所有同名记录:

$Array1 | InnerJoin $Array2 -on {$Left.Name -eq $Right.Name -and $Left.Id -ne $Right.Id} -Merge @{Name = {$Left.$_}} | Format-Table

结果:

Id                                                                           Name
--                                                                           ----
{3b175777-e88f-4248-965b-51dec8639fd6, a43d68de-9ed5-4a13-8f8e-61c6cc501458} b
{de5e32c6-5338-4287-8c21-2dd5957cfffe, 345f9ac2-86a0-4b01-b843-5f4bd55f5e21} c
{2a8c4bf7-cf3a-4c3d-b27b-ecb6f4a4fa43, 953c1442-ed51-445e-a8f5-687120d98f83} f