2

I am trying to sync two directories over the network between servers using UNC paths. The files are 20+ GB. I wrote a Powershell script to compare two directories and then copy any files missing from one to the other. However this doesn't appear to work. Once the script is run it finds the files properly but when it tries to copy them it appears that the files are instantly copied (which is impossible). Even though the files take up 20+ GB of space on the hard drive they don't contain any data.

$SERVER1 = "SERVER1"
$SERVER2 = "SERVER2"
$VDISKPATH = "e$\vdisks"

#Automatically get vDisks and pvp files in VDISKPATH folder
$FILES1 = Get-ChildItem -Path \\$SERVER1\$VDISKPATH\* -Include *.vhd,*.pvp
$FILES2 = Get-ChildItem -Path \\$SERVER2\$VDISKPATH\* -Include *.vhd,*.pvp

if ( $FILES1 -and $FILES2 ) {
    $DIFF = Compare-Object -ReferenceObject $FILES1 -DifferenceObject $FILES2 -Property name, length
}

# Copy all of the missing files from 
if ($DIFF) {
    $DIFF | Where-Object { $PSItem.SideIndicator -eq "<="} | ForEach-Object {
    Copy-Item "\\$SERVER1\$VDISKPATH\$($_.name)" -Destination \\$SERVER2\$VDISKPATH
    }
    $DIFF | Where-Object { $PSItem.SideIndicator -eq "=>"} | ForEach-Object { Write-Host "Copying $($_.name) to $SERVER1" |
    Copy-Item "\\$SERVER2\$VDISKPATH\$($_.name)" -Destination \\$SERVER1\$VDISKPATH
    }
Write-Host "All file copies have finished!"
}
else {
    Write-Host "All files are equal"
    Compare-Object -ReferenceObject $FILES1.name -DifferenceObject $FILES2.name -IncludeEqual
}

Typically the file copy (if done manually) takes about 15 minutes. I let the script run for 90 minutes and it never finished. Can someone help me figure out why the files are not coping properly?

4

2 回答 2

1

我会robocopy用于这样的任务:

robocopy \\SERVER1\E$\vdisks \\SERVER2\E$\vdisks *.vhd *.pvp /z
于 2012-11-17T10:40:01.113 回答
0

where-object 的管道输出仍将是差异对象。所以而不是:

$_.name

在你的 foreach 中,你想要:

$_.InputObject.Name
于 2012-11-16T22:53:07.300 回答