Comparing files based on version number and some other criteria and Formatting the output

心已入冬 提交于 2019-12-11 10:46:44

问题


I am comparing 2 files based on size, last write time and version number using Compare-object in Powershell. I am getting the results. The only problem is how to get the value of the version number from the result.

   function dll_compare(){
   param($path1,$path2) 
   $first = Get-ChildItem -Path $path1 -Filter *.dll
   $second =  Get-ChildItem -Path $path2 -Filter *.dll
   $diff = Compare-Object -ReferenceObject $first -DifferenceObject $second -Property Name, Length, LastWriteTime, VersionInfo -PassThru  | Select Name, Length, LastWriteTime, sideindicator,@{n="path";e={$_.fullname}},@{n="VersionInfo";e={$_|Select-Object -ExpandProperty VersionInfo |Select-Object -Property Productversion}}
   $diff}

The result is in the following format: I want the versioninfo to contain the value, instead of "@{ProductVersion=10.0.10240.16384}"

Name          : PhotoViewer.dll    
Length        : 1827328    
LastWriteTime : 7/10/2015 4:31:20 PM    
SideIndicator : <=    
path          : D:\Site1\Dlls\PhotoViewer.dll    
VersionInfo   : @{ProductVersion=10.0.10240.16384}

回答1:


Replace this line:

$diff = Compare-Object -ReferenceObject $first -DifferenceObject $second -Property Name, Length, LastWriteTime, VersionInfo -PassThru  | Select Name, Length, LastWriteTime, sideindicator,@{n="path";e={$_.fullname}},@{n="VersionInfo";e={$_|Select-Object -ExpandProperty VersionInfo |Select-Object -Property Productversion}

with

$diff = Compare-Object -ReferenceObject $first -DifferenceObject $second -Property Name, Length, LastWriteTime, VersionInfo -PassThru |
    Select Name, Length, LastWriteTime, sideindicator,@{n = "path"; e ={ $_.fullname }}, @{n = "VersionInfo"; e = { $_.VersionInfo.Productversion }


来源:https://stackoverflow.com/questions/34195590/comparing-files-based-on-version-number-and-some-other-criteria-and-formatting-t

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!