Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find items which are in array1 but NOT in array2

I extracted two lists of computers from two different tools, Array1 and 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

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

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

UPDATE

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

enter image description here

the output looks like:

enter image description here

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

enter image description here

means 0KB is 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

And both Column Name (or what it's called) from SCCMcomptuers/eWPTcomputers is "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.

Update 2

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

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

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

like image 785
Michael Avatar asked Dec 05 '22 05:12

Michael


1 Answers

use compare-object cmdlet

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

example :

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

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

will output :

InputObject SideIndicator


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

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

like image 189
Loïc MICHEL Avatar answered Dec 06 '22 20:12

Loïc MICHEL