powershell - Compare-Object not working if I don't list the properties -


i have 2 excel spreadsheets trying compare:

$oledbadapter = new-object system.data.oledb.oledbdataadapter “select * [report$]“,”provider=microsoft.ace.oledb.12.0;data source=s:\fis-bic reporting\report output files\product-marketing\test_xi\ecm - pipeline lob_04182013_040544.xls;extended properties=”"excel 12.0 xml;hdr=yes”";” $rowsreturned = $oledbadapter.fill($datatable)  $oledbadapter2 = new-object system.data.oledb.oledbdataadapter “select * [report$]“,”provider=microsoft.ace.oledb.12.0;data source=s:\fis-bic reporting\report output files\product-marketing\ecm - pipeline lob_04182013_074004.xls;extended properties=”"excel 12.0 xml;hdr=yes”";” $rowsreturned2 = $oledbadapter2.fill($datatable2)  compare-object $datatable $datatable2  

it returns nothing. know in 6th column, different. if specify "-property f6", return difference. idea why doesn't unless specify property? number of columns can vary (though same each of files in comparison), specifying properties won't work.

if don't specify -property parameter, compare-object doesn't compare properties, compares results of invoking .tostring() method on both objects. so, compare-object $datatable $datatable2 compares $datatable1.tostring() $datatable1.tostring(). .tostring() method returns empty string when invoked on datatable object, there no difference report.

for example:

$file1 = get-item somefilename $file1 = get-item anotherfilename compare-object $file1 $file2 

this return difference between full paths of 2 files, this:

inputobject              sideindicator -----------              ------------- <path>\anotherfilename   => <path>\somefilename      <= 

that's because invoking .tostring() on fileinfo object returns fullname property, you're comparing files' full path names.

although -property parameter accepts multiple properties, listing properties not solution. aside being tedious, not give results want. if list multiple properties, compare-object compares combination of properties, , if 1 of listed properties different, returns result showing listed properties (both ones same , ones different) single difference.

what need iterate on list of properties, , invoke compare-object once each property:

$properties = ($datatable | get-member -membertype property | select-object -expandproperty name) foreach ($property in $properties) {   compare-object $datatable $datatable2 -property "$property" | format-table -autosize } 
  • in cases, when comparing properties of 2 objects, you'd want use get-member -membertype properties, in order cover property types. however, if you're comparing datatable objects, you're better off using get-member -membertype property you're comparing properties corresponding data fields, not other properties of datatable objects have nothing data.

  • this written assuming number of columns same, stated, or @ least number of columns in $datatable2 doesn't exceed number of columns in $datatable.

    if can't reliably assume that, derive $properties array whichever 1 has more columns, comparing ($datatable | get-member -membertype property).count ($datatable2 | get-member -membertype property).count , using properties whichever greater.

  • using format-table important, it's not there make things pretty. if list multiple objects of same type (in case, arrays), powershell remembers format of first object, , uses subsequent objects, unless explicitly specify format. since name of first column different each property (i.e., each column spreadsheet), first column empty first difference encountered.

    the -autosize switch optional. is there make things pretty. must pipe results formatting filter. can use format-list if prefer.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -