2

I would like to compare the version info of files from two different directories.

I could do this:

$files1 = (Get-Item "$path1\*.dll").VersionInfo
$files2 = (Get-Item "$path2\*.dll").VersionInfo
compare-object $files1 $files2

But then I get something like:

InputObject
-----------
File:             path1\AxInterop.ShDocVw.dll...
File:             path1\dom.dll...
(etc...)
File:             path2\AxInterop.ShDocVw.dll...
File:             path2\dom.dll...
(etc...)

I guess I could do something like:

$files1 = (Get-Item "$path1\*.dll").VersionInfo.ProductVersion
$files2 = (Get-Item "$path2\*.dll").VersionInfo.ProductVersion
compare-object $files1 $files2
$files1 = (Get-Item "$path1\*.dll").VersionInfo.FileVersion
$files2 = (Get-Item "$path2\*.dll").VersionInfo.FileVersion
compare-object $files1 $files2

but then if there is a difference, I'd have to go looking for what that difference is. I can't compare the files directly because one set is signed and the other isn't.

What would the best way to do this?

To clarify, the current compare-object cmdlet doesn't meet my needs because it shows the filename as different because it shows that they have different paths. This is irrelevant to me.

I would like to compare rows with the same filename but different version numbers. If a difference in version number is observed for the same filename or a filename doesn't exist in one of the tables, then show the difference.

4

1 回答 1

2

使用Compare-Objectcmdlet 的-Property参数来比较和输出感兴趣的属性。

Group-Object允许对结果对象进行分组,Select-Object并可用于从组对象中为每个文件名生成单个输出对象:

$files1 = (Get-Item $path1\*.dll).VersionInfo
$files2 = (Get-Item $path2\*.dll).VersionInfo

Compare-Object $files1 $files2 -Property { Split-Path -Leaf $_.FileName }, 
                                         ProductVersion, 
                                         FileVersion |
  Group-Object -Property ' Split-Path -Leaf $_.FileName ' | 
    Select-Object Name, 
           @{ n = 'SideIndicator'; e = { $_.Group.SideIndicator } },
           @{ n = 'ProductVersion'; e = { $_.Group.ProductVersion -join ' <-> ' } }, 
           @{ n = 'FileVersion'; e = { $_.Group.FileVersion -join ' <-> ' } }           

请注意,使用计算属性仅按文件比较输入对象,然后Group-Object通过Select-Object.

不幸的是,Compare-Object从 PowerShell [Core] 7.0 开始,不允许您命名计算属性[1],并且隐含的属性名称是脚本块 ( { ... })的文字内容
 Split-Path -Leaf $_.FileName ,这是必须传递给Group-Object -Property.

以上产生如下内容:

Name         SideIndicator ProductVersion              FileVersion
----         ------------- --------------              -----------
file1234.exe {=>, <=}      7.0.18362.1 <-> 7.0.18365.0 7.0.18362.1 <-> 7.0.18365.0
file1235.exe <=            10.0.18362.1                10.0.18362.1

也就是说,对于两个位置都存在但版本号不同的文件,SideIndicator显示
{=>, <=},属性中可能不同的版本号*Version由分隔<->


[1] 添加在上下文中命名计算属性的功能Compare-Object此 GitHub 功能请求的主题。

于 2020-02-28T01:44:06.183 回答