Answer the question
In order to leave comments, you need to log in
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"
}
}
Answer the question
In order to leave comments, you need to log in
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
$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
False
False
True
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question