Scheduled Task running Powershell Script that calls Robocopy not finishing copy operation

Richard 1 Reputation point
2021-02-04T09:22:32.437+00:00

I have created a PowerShell script that is designed to connect to remote application servers, create a zip archive containing the server's application backup and then Robocopy the zip file back to my file server. There are 53 servers listed in a CSV and the script is working perfectly when run manually. The application backups are correctly zipped on the application servers, the zip files are correctly RoboCopied back to my file server, perfect!

Logic of script is as follows:

Import Server List From CSV
Using a ForEach Loop to :
Invoke-Command against each server in list to Zip up the days application backup on the application server
Create source and destination variables
Initiate Robocopy operation along the lines of -

RoboCopy $copysource $copydestination /MAXAGE:1 /MOV /LOG+:$copylog

The issue arises when the script is called by a Scheduled Task. The RoboCopy operation starts and the zip files begin to copy over the network, however, looking at the RoboCopy log, it just stops at anywhere between 1% or sometimes up to 40%. The scheduled task shows has having completed, but it is abandoning the RoboCopy operation mid copy without generating any error codes.

I have tried all the usual options on the scheduled task, such as running with highest privileges etc, but it clearly isn't a permission thing as it does get some of the way through the copy. It almost feels as though the job is timing out, but I've cleared the "Stop the task if it runs longer than:" setting and it is stopping after only a few minutes, certainly not a few days.

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,390 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Keep, Richard 1 Reputation point
    2021-02-23T11:27:50.457+00:00

    Does anyone have an answer to this, it's doing my nut in!

    0 comments No comments

  2. MotoX80 32,081 Reputation points
    2021-02-23T13:27:32.143+00:00

    Try using "Invoke-Command -SessionOption " and pass a New-PSSessionOption and setting -OperationTimeout to 0.

    https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/new-pssessionoption?view=powershell-5.1


  3. Richard 1 Reputation point
    2021-03-04T10:00:18.947+00:00

    MotoX80,

    The transcript doesn't really give any further information, only that the background tasks are being created. The tasks are running as they should and data is being copied to the file server from all the remote sites, but the RoboCopy logs are simply stopping when x% is complete with no error codes or messages logged anywhere. In other words, Powershell is doing what it should, it is creating multiple Robocopy jobs, the script has all the appropriate permissions as the copy operation is working for a period of time, no time outs or disconnect errors in the RoboCopy logs, it simply just stops copying even though the copy hasn't reached 100%.

    Tried replacing the RoboCopy with Start-Bitstransfer only to find out you can't use Start-BitsTransfer with a scheduled task as it needs to be run in the foreground and only seems able to do 3 or 4 jobs concurrently.

    The files I'm copying are around 700MB in size and some of the sites we are copying from have flaky connections, so Copy-Item and XCopy aren't options either.

    Also tried changing the script so the RoboCopy operations are kicked off on each remote server, however, again this doesn't seem to be achievable, due to the double hop authentication required to make it work.

    I'm now contemplating trying to do this with DFS Replication, but there are over 80 servers in 80 sites that I need to collect up the local backups from and this will be a major pain in the backside having to created so many DFS Replication groups.

    Before I give up on this, any further ideas?