Search code examples
powershellstart-jobscriptblockcompareobject

scriptblock returning object for compare-object


I'm writing a script to compare 2 samba locations via compare object.

To speed things up i would like to give each location via a thread to a scriptblock where i let the object get made. After that i'd like the output from the scriptblock as an object to use it in the Compare-Object cmdlet.

What i have sofar:

$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 

so would still need to add this in somewhere:

 (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})

But the result from test and test2 are hashtable (i think?) and not an object.

Any input would be appreciated on where i went wrong, or how i could do it differently


Solution

  • If you want to return the names of the files in the second location whose checksums don't match, the following editions would help.

    $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