本文介绍了查找array1中但不在array2中的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从两个不同的工具 Array1 和 Array2 中提取了两台计算机列表。

I extracted two lists of computers from two different tools, Array1 and Array2.

现在我需要提取 Array1 中的那些,而不是 Array2 。

Now I need to extract the ones which are in Array1, but not in Array2.

我设法通过以下操作获得了所有匹配项:

I managed to get all the matching ones by doing this:

$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

但是我仍然需要其中的 $ SCCMcomputer ,但不在 $ eWPTcomputers ...

But I still need the ones that are in $SCCMcomputer but NOT in $eWPTcomputers...

我找到了其他语言(例如Perl)上的SO解决方案,但不适用于PowerShell。

I've found some solutions on SO with other languages (e.g. Perl) but not for PowerShell.

更新

在Excel中,使用以下公式,我仍然没有得到正确的输出:

I still don't get the correct output, in Excel with this formula:

输出看起来像:

表示有些在这里,有些不在。 Powershell中的输出是这样的

means some are here, some not. The output in powershell is like this

意味着

$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

以及SCCMcomptuers / eWPTcomputers的列名(或称为列名)是计算机名

And both Column Name (or what it's called) from SCCMcomptuers/eWPTcomputers is "Computername"

知道我可能做错了什么吗?这两个计算机数组都是从SQL生成的,并且都在哈希表中(我认为这叫它): @ {Computername = ......} @ {Computername .... 像这样。

Any idea what I could be doing wrong? Both computer arrays are generated from SQL and in hashtables (I think it's called): @{Computername=......}@{Computername...., something like this.

更新2

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

$Test2 = @()
foreach ($t in $ewptComputers) {
    $Test2 += $t.computername
}

通过删除哈希表的页眉,仅具有充满字符串的数组,就可以实现幻想……..即使-属性计算机名也不起作用...:S

By removing the Header of the Hashtable and just having arrays full of strings works fantasctic..... even -Property computername did not work... :S

推荐答案

使用比较对象 cmdlet

比较对象-ReferenceObject $ sccm -DifferenceObject $ wpt | ?{$ _。sideIndicator -eq< =} |选择输入对象

示例:

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

Compare-Object -ReferenceObject $sccm -DifferenceObject $wpt -IncludeEqual

将输出:

      2 ==
      4 =>
      1 <=
      3 <=


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

that means value "2" is on both objects, "1" and "3" only on "the left side" (ie the reference object), while "4" is only on the difference object

这篇关于查找array1中但不在array2中的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 04:53