0

我正在编写一个脚本来通过比较对象比较 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 的结果是哈希表(我认为?)而不是对象。

任何输入将不胜感激我哪里出错了,或者我可以如何以不同的方式做

4

1 回答 1

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
于 2018-02-27T18:53:59.317 回答