scriptblock返回compare-object的对象

时间:2018-02-27 14:30:39

标签: powershell start-job scriptblock compareobject

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

为了加快速度,我想通过一个线程将每个位置放到一个脚本块中,我让对象生成。 之后,我希望scriptblock的输出作为对象在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的结果是哈希表(我认为?)而不是对象。

任何输入都会在我出错的地方欣赏,或者我如何以不同的方式做到这一点

1 个答案:

答案 0 :(得分:1)

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

$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