T
T
The0dium2021-02-19 10:24:12
PowerShell
The0dium, 2021-02-19 10:24:12

Comparing two arrays in Powershell?

Good afternoon. I would like to ask the community for help. At least a hint in which direction to move (with Powershell I'm on you, no experience ...)
The essence of the task: There is a web page with a table, and there is an unloaded .csv file with an almost identical table. I need to make sure that PS compares these two tables and displays the result, in which row what are the discrepancies. Those. not just the number of the non-matching line, but the entire line. I'm at a dead end. I am attaching the code.

$inhalt = Invoke-WebRequest -uri 'тут адрес сайта' -WebSession $Cookie

$stammArtikelTabelle = $inhalt.ParsedHtml.getElementsByTagName("table");
$stammArtikelRow = $stammArtikelTabelle[0].getElementsByTagName("tr");

$importfile = "C:\test\Stammartikel_Export.CSV" 
$import = Import-csv $importfile -Delimiter ";" -Encoding Default

function cleanInnerHtml($spalte) {
    return $spalte.innerhtml.replace(" ","").trim();
}


$stammArtikelWebseite = @();

$jumpedOverFirstElement = $false
foreach($stammArtikelCol in $stammArtikelRow) {
    if (!$jumpedOverFirstElement) { $jumpedOverFirstElement = $true; continue;}

    $spalten = $stammArtikelCol.getElementsByTagName("td");
    $newRoW = @{}
    $newRow.artikelnummer = cleanInnerHtml $spalten[0]
    $newRow.beschreibung = cleanInnerHtml $spalten[1]
    $newRow.warengruppe = cleanInnerHtml $spalten[2]
    $newRow.preisohne = cleanInnerHtml $spalten[3]
    $newRow.preismit = cleanInnerHtml $spalten[4]

    $stammArtikelWebseite += $newRoW
}   

$stammArtikelwebseite[0]

#exit
Write-Host " "

$stammArtikelcsv = @();

foreach($row in $import) {
    $matnr = $row.Matnr.TrimStart('0')
    $bezeichnung = $row.Bezeichnung.Trim()
    $ltext = $row.Langtext.Trim()
    $waren = $row.Warengruppe
    $noMWST = $row.'Preis ohne MWST'.Trim()
    $MWST = $row.'Preis mit MWST'.Trim()
    $hersteller = $row.'Hersteller '

    foreach($spalte in $row){
        $newRow1 = @{}
        $newRow1.artikelnummer = $matnr
        $newRow1.beschreibung  = $bezeichnung
        $newRow1.warengruppe   = $waren
        $newRow1.preisohne     = $noMWST + " €"
        $newRow1.preismit      = $MWST + " €"
        $stammArtikelcsv += $newRow1
        }
    }
$stammArtikelcsv[0]

foreach ($newRoW in $stammArtikelwebseite[0]) {
    if ($stammArtikelcsv[0] -notcontains $newRoW) {
        "$newRoW is extra"
    }

}


In general, I brought both tables to a common view, but now it is difficult to compare two arrays, each of which contains a hash table.

I would be very grateful for any help.
Addition:
The general view of the html table: 602f8029e6374081967663.png
.csv looks something like this:
0000000000000060186082;AccessPoint Cisco Catalyst 92120AXI ;AccessPoint/Repeater; 700.00 ; 833.00

It is necessary that the array is compared line by line, and, for example, if the price has changed, another array is created, where the name of the product and the changed price would be.

What the script outputs now:
602f825886096132966672.jpeg

At the moment, it is clear that I made the most similar arrays from two tables, so that PS would be more convenient to compare. But how to make this comparison is still a mystery to me. I hope I didn't describe it too hard)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
MaxKozlov, 2021-02-19
@The0dium

You will need to convert your hastable to [PSCustomObject]and
explore the possibilities Compare-Object
Converting is easy - [PSCustomObject]$hastable
And then when you have objects, not hashes, they can be compared by individual properties
In particular, you can try

#...
# вместо # $stammArtikelWebseite += $newRoW
$stammArtikelWebseite += [PSCustomObject]$newRoW
# ...
# то что в $Import - уже объекты, их можно менять на месте
# или также из вашего хеша сделать 
# вместо # $stammArtikelcsv += $newRow1
$stammArtikelcsv += [PSCistomObject]$newRow1
#...

# А потом:
Compare-Object -ReferenceObject  $stammArtikelWebseite  -DifferenceObject $stammArtikelcsv -Property preisohne

A
alhaos, 2021-02-22
@alhaos

$tupel1 = [System.Tuple]::Create(1, "cisco", 100)
$tupel2 = [System.Tuple]::Create(1, "cisco", 200)
$tupel3 = [System.Tuple]::Create(1, "cisco", 100)

$tupel1 -eq $tupel2
$tupel2 -eq $tupel3
$tupel1 -eq $tupel3

There are such dumb ones)))
aka kartezh, they can be compared in their entirety
False
False
True

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question