比较对象的脚本块返回对象

scriptblock returning object for compare-object

我正在编写一个脚本来通过比较对象比较 2 个 samba 位置。

为了加快速度,我想通过一个线程将每个位置提供给一个脚本块,让我在其中创建对象。 之后,我希望脚本块的输出作为对象在 Compare-Object cmdlet 中使用它。

到目前为止我有什么:

$nas_smb_share =  "\nas\loc\"
$cs_dest ="\dest2\loc"


$check_hash = {
Param($loc)

$fso = (dir $loc -Recurse | Where-object{(!$_.psiscontainer) -AND ($_.LastWriteTime -gt (Get-Date).AddHours(-20))} | get-hash -Algorithm MD5)

return $fso
 }


$compare_loc =@($nas_smb_share, $cs_dest)



foreach ($check in $compare_loc) 
{
$running = @(Get-Job | Where-Object { $_.State -eq 'Running' })
if ($running.Count -le 3) 
{
   $j = Start-Job -ScriptBlock $check_hash -ArgumentList $check -Name $check

} else 
{
     $running | Wait-Job
}
Get-Job | Receive-Job  
$test = Receive-Job -Name $nas_smb_share -Keep
$test2 = Receive-Job -Name $cs_dest -Keep

}
Get-Job | Wait-Job | Receive-Job 

所以仍然需要在某处添加:

 (Compare-Object -ReferenceObject $fso -DifferenceObject $fsoBU -Property hash -PassThru).Path | %{if ($_.SideIndicator -eq "=>" ){$result = ("$($_.InputObject)")}}

(dir $cs_dest -Recurse  | Where-Object {(!$_.psiscontainer)} | get-hash -Algorithm MD5 | ? {$_.hashstring -match $result})

但是 test 和 test2 的结果是哈希表(我认为?)而不是对象。

任何关于我出错的地方或我如何以不同的方式做的意见都将不胜感激

如果您想return第二个位置的校验和不匹配的文件名,以下版本会有所帮助。

$nas_smb_share = "\nas\loc\"
$cs_dest = "\dest2\loc"
$compare_loc = @($nas_smb_share, $cs_dest)
$check_hash = {
    Param($loc)
    return Get-ChildItem $loc -Recurse | Where-object {(!$_.psiscontainer) -AND ($_.LastWriteTime -gt (Get-Date).AddHours(-20))} | Get-FileHash -Algorithm MD5
}
$Jobs = @()
foreach ($check in $compare_loc) {
    $Jobs += Start-Job -ScriptBlock $check_hash -ArgumentList $check -Name $check
}
$Jobs | Wait-Job | Out-Null
$test = Receive-Job -Name $nas_smb_share -Keep
$test2 = Receive-Job -Name $cs_dest -Keep
(Compare-Object -ReferenceObject $test -DifferenceObject $test2 -Property Hash -PassThru | Where-Object { $_.SideIndicator -eq "=>" }).Path