How to compare two arrays of DataRow objects in PowerShell?

I have two arrays of System.Data.DataRow objects that I want to compare.

There are two columns A and B in the rows. Column A is the key, and I want to find out which rows changed their column B and which rows were added or deleted.

How to do it in PowerShell?

+4
source share
3 answers

I wrote a script to do this a little back. a script (Compare-QueryResults.ps1) is available here , and you will also need my Run-SQLQuery script (available here ), or you can replace it with a script or your own function.

Basically, what the script does is take the results of each of your queries and break the datarows so that each field is its own object. He then uses Compare-Object to check for differences between the data in these rows. It returns a comparison object that shows all the differences between the returned data.

The results are an object, so you can save them in a variable and use the Sort-Object or Format- * commands with them.

Good luck. If you have any problems with the scripts, let me know, I would be happy to guide you through them. I used them to test applications, seeing which lines are changed by various actions in the program.

+4
source

To just compare the two System.Data.DataRow, you can do something like this:

 foreach ($property in ($row1 | Get-Member -MemberType Property)) { $pName = $property.Name if ($row1.$pName -ne $row2.$pName) { Write-Host "== $pName ==" $row1.$pName $row2.$pName } } 
+1
source

Do you need two DataRows arrays? the DataRow object has a RowState property that will give you what you need. See MSDN Docs: http://msdn.microsoft.com/

0
source

Source: https://habr.com/ru/post/1276403/


All Articles