0

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

Michael
  • 57
  • 1
  • 11

1 Answers1

1

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
t1meless
  • 504
  • 2
  • 9
  • thanks for this! Any reason why one can't pass an object along? – Michael Feb 28 '18 at 12:37
  • Could you elaborate the question? In the provided code each job returns an array of PSCustomObject. Then Compare-Object cmdlet performs comparison between the arrays. – t1meless Mar 01 '18 at 12:29
  • i'm used to c# where a method can retun an object. You then can compare those objects and it's attributes to each other. I was just wonderif it it was possible with powershell. You're solution works like charm but left me wondering :p – Michael Mar 01 '18 at 12:31
  • $test and $test2 arrays contain the objects in question. You could iterate through them by your own cycle. – t1meless Mar 05 '18 at 17:41