October 8, 2014 at 5:15 pm
Hi all-
I have a PS script that I use to transfer files from our data center to our corporate location so that we can restore in our development environment on a regular basis. For some reason, the script will randomly fail during the transfer but will not return an error for some reason. Because of this I'm not able to determine the exact cause and try to rectify the situation. Here's the code I'm using. If anyone has any suggestions that would be great.
Thanks
$array = @("\\HOMESERVER\Backup")
$target = "\\TARGETSERVER\SQL_Backup\"
$erroractionpreference = "Stop"
try
{
$files = get-childitem $target -Recurse -include *.*
foreach ($file in $files)
{
if ($file.LastWriteTime -le (get-date).addhours(-168))
{
remove-item $file
}
}
for ($i=0; $i -lt $array.length; $i++)
{
$source = $array[$i]
$files = get-childitem $source -Recurse -include *.bak
foreach ($file in $files)
{
if ($file.LastWriteTime -ge (get-date).addhours(-24))
{
<#$targetFile = $target + $file.FullName.SubString($source.Length)#>
$targetFile = $target + $file.Name
<# New-Item -ItemType File -Path $targetFile -Force #>
If(-not(Test-Path -Path $targetFile))
{
Copy-Item $file.FullName -destination $targetFile
}
}
}
}
#Everything is ok so send on to Monitor
if (-not $error)
{
#Everything is ok. Send ok to server
}
}
Catch
{
#Everything is not ok. Send error to server
}
October 9, 2014 at 12:03 am
In what way does it "randomly fail"? What operation failed?
Gaz
-- Stop your grinnin' and drop your linen...they're everywhere!!!
October 9, 2014 at 9:55 am
It fails during the file transfer of one of the larger files. There are about 6 files total with three of them being between 10GB and 75GB. Periodically there are only a couple of the files there on the destination when all of the files are there in the source. I'm suspecting our link is dropping at some time during the transfer. However, what I really need to figure out is why no error is returned for the failed file being copied and if there's a better way to transfer the files with some type of re-try on failure.
Thanks
October 9, 2014 at 10:29 am
Perhaps you should be using the cmdlets that use BITS (Background Intelligent Transfer Service - see here by Jeffrey Snover i.e. the man who brought us PowerShell).
NOTE: The module has since been renamed BitsTransfer (from FileTransfer) so the import command is:
Import-Module BitsTransfer -Verbose
Gaz
-- Stop your grinnin' and drop your linen...they're everywhere!!!
October 10, 2014 at 8:06 am
Any joy?
Gaz
-- Stop your grinnin' and drop your linen...they're everywhere!!!
October 10, 2014 at 12:55 pm
Yes, partially. I'm not able to get it to import the bitstransfer module when running it through a job, so I still need to look at what's going on there. When I run iStart-BitsTransfer from the command line I'm randomly getting access denied errors after the file transfer has been running for a while. I have a suspicion that this might be the actual error that I'm running into with the other method.
An error is one thing, but the thing that is still really bugging me is why:
$erroractionpreference = "Stop"
is not trapping the error and returning it back to the job. This is my biggest issue at this point.
Any thoughts on that would be appreciated.
Thanks
October 15, 2014 at 11:50 pm
I have been looking at this for a few days then came in this morning and in less than two minutes I think I have figured it out.
I had missed that you were running this as an agent script. Muppet!!! (me, not you)
Your catch block is consuming all errors. If you want it returned as an error you can replace your catch block with something similar to the following:
catch
{
#Everything is not ok. Send error to server
Write-Error $_.ToString() -ErrorAction Stop
}
Gaz
-- Stop your grinnin' and drop your linen...they're everywhere!!!
October 16, 2014 at 6:29 pm
Ok. Thanks I'll give that a try. I did have
throw $error
in there though so I thought that would have done it.
Additionally, most of the transfers are working, except for the larger files. They are throwing the following exception after running for several hours:
Start-BitsTransfer : Access is denied.
At C:\Windows\System32\WindowsPowerShell\v1.0\ProdBackupCopy.ps1:36 char:21
+ Start-BitsTransfer -Source $file.FullName -Destination $targ ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Start-BitsTransfer], Exception
+ FullyQualifiedErrorId : StartBitsTransferCOMException,Microsoft.BackgroundIntelligentTransfer.Management.NewBitsTransf
erCommand
So even though I specified a retryinterval and a retrytimeout it's abending when it hits this error.
Thanks again for the help.
October 16, 2014 at 11:40 pm
You are most welcome. Thanks for posting back to this thread as it may help others in future.
Gaz
-- Stop your grinnin' and drop your linen...they're everywhere!!!
October 20, 2014 at 4:21 am
I am sure that you would have but, just in case, have you called Complete-BitsTransfer?
Gaz
-- Stop your grinnin' and drop your linen...they're everywhere!!!
October 21, 2014 at 4:39 pm
Yeah I call Complete-BitsTransfer after every file. Still can't figure out why it gives me access denied though on the larger files.
October 21, 2014 at 11:46 pm
Have you posted on the MSDN managed forums?
Gaz
-- Stop your grinnin' and drop your linen...they're everywhere!!!
Viewing 12 posts - 1 through 11 (of 11 total)
You must be logged in to reply to this topic. Login to reply