Powershell Copy-Item fails silently

copypowershellwindows-server-2008

I have a PowerShell 2.0 script running on Windows Server 2008 R2 64bit that copies some Hyper-V .vhd files to another server as a 'backup solution'.

The script gets a list of the .vhd's to copy then iterates over that list to copy them using Copy-Item. It also writes some logging info to a file as well. The files are copied to another server (Windows Server 2003 Sp2) into a directory compressed with NTFS compression.

One of the files isn't copied. It's relatively big ~ 68Gb. The others are 20Gb or less. The wierd thing is that during the copy process the file appears on the destination server and the log file generated seems to indicate the file is copied due to the difference in the times of the log file entries.

I see no error messages on the log file and nothing in the event log of either machine.

Here's the code that does the copy.

Get-ChildItem $VMSource *.vhd -Recurse | foreach-object {

    $time = Get-Date -format HH.mm.ss
    Add-Content $logFileName "$time : File Copy ($_) started" 

    $fullname = $_.FullName
    Add-Content $logFileName "$time : Copying $fullname to $VMDestination" 

    Copy-Item $fullname $VMDestination -Force -ErrorAction SilentlyContinue -ErrorVariable errors

    foreach($error in $errors)
    {
        if ($error.Exception -ne $null)
        {
            Add-Content $logFileName "'tERROR COPYING FILE : $($error.Exception)"
        }
    }

    $time = Get-Date -format HH.mm.ss
    Add-Content $logFileName "$time : File Copy ($_) finished"
}

I can only think there's some problem with copying a file that big to a compressed directory maybe? Any ideas?

Best Answer

The problem was copy of that file in general. I tried copying the file in Explorer and it failed after about an hour with an error stating that part of the file was locked by another process.

I ended up amending the script to zip the file before moving it to the destination. The script now runs successfully albeit taking much longer.

Cheers for any answers tho.