查找array1中但不在array2中的项目

Mic*_*ael 4 arrays powershell foreach

我的电脑中提取两列由两种不同的工具,Array1Array2.

现在我需要提取那些Array1但不在其中的那些Array2.

我设法通过这样做获得所有匹配的:

$matchingComp = @()

foreach ($SCCMcomputer in $SCCMcomputers) {
    foreach ($eWPTcomputer in $eWPTcomputers) {
        if ($SCCMcomputer.Computername -eq $eWPTComputer.Computername) {
            $obj = New-Object PSObject
            $obj | Add-Member -MemberType NoteProperty -Name "ComputerName" -Value $SCCMcomputer.Computername
            $matchingComp +=$obj
        }
    }
}

$matchingComp | Export-Csv $inEWPT -Delimiter "," -NoTypeInformation
Run Code Online (Sandbox Code Playgroud)

但我仍然需要那些$SCCMcomputer但不在$eWPTcomputers......

我已经在SO上找到了一些与其他语言(例如Perl)的解决方案,但对于PowerShell没有.

UPDATE

我仍然无法使用以下公式在Excel中获得正确的输出:

在此输入图像描述

输出看起来像:

在此输入图像描述

意味着有些人在这里,有些则没有.powershell中的输出是这样的

在此输入图像描述

表示0KB是emtpy.

$SCCMcomputers | Export-Csv $sccmexport -Delimiter "," -NoTypeInformation
$eWPTcomputers | Export-Csv $ewptexport -Delimiter "," -NoTypeInformation
Compare-Object -ReferenceObject $SCCMcomputers -DifferenceObject $eWPTcomputers | ?{$_.sideIndicator -eq "=>"} |select inputobject | Export-Csv $inEWPT -NoTypeInformation
Compare-Object -ReferenceObject $SCCMcomputers -DifferenceObject $eWPTcomputers | ?{$_.sideIndicator -eq "=="} |select inputobject | Export-Csv $inBoth -NoTypeInformation
Compare-Object -ReferenceObject $SCCMcomputers -DifferenceObject $eWPTcomputers | ?{$_.sideIndicator -eq "<="} |select inputobject | Export-Csv $inSCCM -NoTypeInformation
Run Code Online (Sandbox Code Playgroud)

SCCMcomptuers/eWPTcomputers的列名(或其名称)都是"Computername"

知道我可能做错了吗?两个计算机阵列都是从SQL和哈希表(我认为它被称为)生成的:@{Computername=......}@{Computername....就像这样.

更新2

foreach ($t in $sccmComputers) {
    $Test1 += $t.computername
}

$Test2 = @()
foreach ($t in $ewptComputers) {
    $Test2 += $t.computername
}
Run Code Online (Sandbox Code Playgroud)

通过删除Hashtable的Header并且只有阵列充满了弦乐作品幻想.....甚至-Property computername不起作用......:S

Loï*_*HEL 12

使用compare-objectcmdlet

Compare-Object -ReferenceObject $sccm -DifferenceObject $wpt | ?{$_.sideIndicator -eq "<="} |select inputobject

例如:

$sccm=@(1,2,3)   
$wpt=@(2,4)

Compare-Object -ReferenceObject $sccm -DifferenceObject $wpt -IncludeEqual   
Run Code Online (Sandbox Code Playgroud)

将输出:

InputObject SideIndicator


      2 ==            
      4 =>            
      1 <=            
      3 <=
Run Code Online (Sandbox Code Playgroud)

这意味着值"2"在两个对象上,"1"和"3"仅在"左侧"(即参考对象),而"4"仅在差异对象上