2

我正在使用 Powershell 中的比较对象根据大小、上次写入时间和版本号比较 dll 文件。这些文件存储在远程服务器上。我得到了结果。唯一的问题是如何从结果中获取版本号的值。在我之前的问题中,我采用了一种未优化的不同方法,您可以在此处查看:Comparing files based on version number and some other criteria and Formatting the output 我更新的脚本是:

$s1=New-PSSession -ComputerName $c1
$first=Invoke-Command -Session $s1 -ScriptBlock{param($path1) Get-ChildItem -Path $path1 -Filter *.dll} -ArgumentList $path1

$s2=New-PSSession -ComputerName $c2
$second=Invoke-Command -Session $s2 -ScriptBlock{param($path2) Get-ChildItem -Path $path2 -Filter *.dll} -ArgumentList $path2

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

这里 c1 和 c2 是计算机名,path1 和 path2 分别是 c1 和 c2 中文件夹的路径。输出不包含版本号。它的格式如下:

Name          : PhotoViewer.dll
Length        : 20480
LastWriteTime : 8/9/2015 4:46:08 PM
SideIndicator : <=
VersionInfo   : 
4

1 回答 1

1

对象属性反序列化的深度可能存在限制。无论如何,这是一种可行的方法。

检查此链接以获取更多信息链接

$first=Invoke-Command -Session $s1 -ScriptBlock{param($path1) Get-ChildItem -Path $path1 -Filter *.dll | Export-Clixml -Path '\\networkshare\first.xml' } -ArgumentList $path1

$second=Invoke-Command -Session $s2 -ScriptBlock{param($path2) Get-ChildItem -Path $path2 -Filter *.dll |  Export-Clixml -Path '\\networkshare\second.xml'} -ArgumentList $path2

$first_xml = Import-Clixml -Path '\\networkshare\first.xml'
$second_xml = Import-Clixml -Path '\\networkshare\second.xml'

Compare-Object -ReferenceObject $first_xml -DifferenceObject $second_xml -Property Name, Length, LastWriteTime, VersionInfo -PassThru  | 
 Select-Object Name, Length, LastWriteTime, sideindicator,@{n='VersionInfo';e= { $_.VersionInfo.productversion }}
于 2015-12-15T08:28:57.043 回答