Powershell 如果我没有';不要列出属性
我有两个Excel电子表格要比较:Powershell 如果我没有';不要列出属性,powershell,powershell-2.0,Powershell,Powershell 2.0,我有两个Excel电子表格要比较: $OleDbAdapter = New-Object System.Data.OleDb.OleDbDataAdapter “Select * from [Report$]“,”Provider=Microsoft.ACE.OLEDB.12.0;Data Source=S:\FIS-BIC Reporting\Report Output Files\Product-Marketing\TEST_XI\ECM - Pipeline by LOB_04182013
$OleDbAdapter = New-Object System.Data.OleDb.OleDbDataAdapter “Select * from [Report$]“,”Provider=Microsoft.ACE.OLEDB.12.0;Data Source=S:\FIS-BIC Reporting\Report Output Files\Product-Marketing\TEST_XI\ECM - Pipeline by LOB_04182013_040544.xls;Extended Properties=”"Excel 12.0 Xml;HDR=YES”";”
$RowsReturned = $OleDbAdapter.Fill($DataTable)
$OleDbAdapter2 = New-Object System.Data.OleDb.OleDbDataAdapter “Select * from [Report$]“,”Provider=Microsoft.ACE.OLEDB.12.0;Data Source=S:\FIS-BIC Reporting\Report Output Files\Product-Marketing\ECM - Pipeline by LOB_04182013_074004.xls;Extended Properties=”"Excel 12.0 Xml;HDR=YES”";”
$RowsReturned2 = $OleDbAdapter2.Fill($DataTable2)
Compare-Object $DataTable $DataTable2
它什么也不返回。我知道在第六栏中,他们是不同的。如果我指定“-property F6”,它将返回差异。知道为什么没有,除非我指定了属性吗?列的数量可能会有所不同(尽管在比较中每个文件的列数相同),因此专门指定属性不起作用。如果不指定-Property参数,比较对象不会比较所有属性,它会比较调用.ToString()的结果两个对象上的方法。因此,
比较对象$DataTable$DataTable2
将$DataTable1.ToString()与$DataTable1.ToString()进行比较。.ToString()方法在DataTable对象上调用时返回一个空字符串,因此报告没有差异
例如:
$file1 = Get-Item somefilename
$file1 = Get-Item anotherfilename
Compare-Object $file1 $file2
这将返回两个文件的完整路径之间的差异,如下所示:
InputObject SideIndicator
----------- -------------
<path>\anotherfilename =>
<path>\somefilename <=
- 在大多数情况下,在比较两个对象的所有属性时,您可能希望使用
,以获取覆盖所有属性类型。但是,如果要比较DataTable对象,最好使用Get Member-MemberType properties
,以便只比较与数据字段相对应的属性,而不比较与数据无关的DataTable对象的其他属性Get Member-MemberType Property
- 这是在假定列数与您所述相同的情况下编写的,或者至少假定$DataTable2中的列数不超过$DataTable中的列数
如果无法可靠地假设,请通过比较
($DataTable | Get Member-MemberType Property)和
数组($DataTable2 | Get Member-MemberType Property)中的$properties并使用其中较大的属性,从中派生$properties
- 使用格式表很重要,它不仅仅是为了让事情看起来漂亮。如果列出相同类型的多个对象(在本例中为数组),PowerShell将记住第一个对象的格式,并将其用于所有后续对象,除非您明确指定格式。由于每个属性(即电子表格中的每一列)的第一列名称不同,因此除了遇到的第一个差异外,第一列将为空
开关是可选的。那只是为了让事情看起来漂亮。但您必须将结果通过管道传输到格式过滤器。如果愿意,也可以使用格式列表-AutoSize
- 包含有关
比较对象如何工作的良好背景信息
但是,有一种方法可以通用地使用-Property
来比较所有列值——假设两个输入表具有相同的列结构,或者只应通过第一个表的列来比较这些表:
# The original collection.
$coll1 = [pscustomobject] @{ one = '1a'; two = '2a'; three = '3a' },
[pscustomobject] @{ one = "1b"; two = "2b"; three = '3b' }
# The other collection to compare the original to.
# Note the difference in the 2nd object in column 'two'
$coll2 = [pscustomobject] @{ one = '1a'; two = '2a'; three = '3a' },
[pscustomobject] @{ one = "1b"; two = "2b!"; three = '3b' }
# PSv3+: Get the array of all property names to compare
# from the original collection.
# Note:
# * The assumption is that both collections have the same set of
# properties (or that the collections should only be compared by
# the *first* collection's properties).
# * In PSv2-, use the following instead:
# $propsToCompare = $coll1[0].psobject.properties | % { $_.name }
$propsToCompare = $coll1[0].psobject.properties.name
# Compare the 2 collections by all property values.
# -PassThru means that any input object in which a difference is found
# is passed through as-is.
Compare-Object $coll1 $coll2 -Property $propsToCompare -PassThru
上述收益率:
1个2个3边指示器
--- --- ----- -------------
1b 2b!3b=>
1b 2b
告诉您所选对象仅限于右侧,反之亦然,感谢您的全面响应。我不知道它没有比较所有的属性。不过我有个问题。我真的想逐行比较。为此,我想我需要向每个行添加一个名为RowNum的键,并将其指定为键。您是说要比较整行,并在比较行中的任何列包含不同数据时返回结果?如果是,请参阅我在中间写的部分:“虽然-属性参数接受多个属性,列出所有的属性……不会给出您想要的结果。”如果您想比较整行,列出--属性参数中的所有属性(即列名)将给出您想要的结果。是这样吗?正确-整排。如果能知道哪一列有区别就好了,但这不是必要的。有没有一种方法可以在不列出每个属性的情况下做到这一点?每个电子表格可以有不同数量的列。我计划对1000个电子表格进行批量比较(它们是集合,所以只比较电子表格a和b、c和d等)。A和B有相同的#列,但c和d将不同。您可以将$DataTable1的所有属性名称读入一个数组,并将其用作-property参数。我需要更多地了解这些数据表的结构,以便更具体地了解如何使用这些数据表。我没有安装用于Excel的OLEDB驱动程序,因此无法在本地复制。酒店的名称是什么样的?你说你可以使用F6作为属性名,但这对我来说没有意义。属性名称是否与Excel列名相同,即A、B、C、D等?如果所有行都具有相同的属性,那么行#如何成为属性名称的一部分?
# The original collection.
$coll1 = [pscustomobject] @{ one = '1a'; two = '2a'; three = '3a' },
[pscustomobject] @{ one = "1b"; two = "2b"; three = '3b' }
# The other collection to compare the original to.
# Note the difference in the 2nd object in column 'two'
$coll2 = [pscustomobject] @{ one = '1a'; two = '2a'; three = '3a' },
[pscustomobject] @{ one = "1b"; two = "2b!"; three = '3b' }
# PSv3+: Get the array of all property names to compare
# from the original collection.
# Note:
# * The assumption is that both collections have the same set of
# properties (or that the collections should only be compared by
# the *first* collection's properties).
# * In PSv2-, use the following instead:
# $propsToCompare = $coll1[0].psobject.properties | % { $_.name }
$propsToCompare = $coll1[0].psobject.properties.name
# Compare the 2 collections by all property values.
# -PassThru means that any input object in which a difference is found
# is passed through as-is.
Compare-Object $coll1 $coll2 -Property $propsToCompare -PassThru