How to properly run Powershell scripts containing BITS operations from Task Scheduler?

1

I am trying to perform an automatic backup of some files and folders based on a configuration. This is done using Powershell 5 which in turn relies on Background Intelligent Transfer Service (BITS). E.g.:

Start-BitsTransfer $Source\* $Destination -RetryInterval 60 -RetryTimeout 600

Powershell scripts are run using a bat:

powershell -ExecutionPolicy unrestricted .\Main.ps1

The batch is executed using a Scheduled Task:

User account: AD service account
Run: whether user is logged on or not

When I run the batch directly using the same account set on scheduled task, it runs perfectly. When I run it from the Task scheduler, BITS operations issue the following error:

The operation being requested was not performed because the user has not logged on to the network. The specified service does not exist. (Exception from HRESULT: 0x800704DD)

I tried to change the user for BITS service, by setting it to the same service account I use for the scheduled task, but the service does not start anymore:

The Background Intelligent Transfer Service service failed to start due to the following error:
The account specified for this service is different from the account specified for other services running in the same process.

Going back to the scheduled task, I have changed to run "only when user is logged on". Of course, this would make sense only if I set autologon on this user (otherwise, it will not run if not logged in).

Question: How can I automate a backup using BITS without relying on tricks such as autologon?

windows
windows-server-2016
scheduled-task
bits
powershell-v5.0
asked on Server Fault Mar 8, 2018 by Alexei • edited Mar 8, 2018 by Lenniey

1 Answer

2

It looks like you're only going to be able to run that script when you're logged in. From the MS documentation "Using Windows Powershell to Create BITS Transfer Jobs" found here.

When you use *-BitsTransfer cmdlets from within a process that runs in a noninteractive context, such as a Windows service, you may not be able to add files to BITS jobs, which can result in a suspended state. For the job to proceed, the identity that was used to create a transfer job must be logged on. For example, when creating a BITS job in a PowerShell script that was executed as a Task Scheduler job, the BITS transfer will never complete unless the Task Scheduler's task setting "Run only when user is logged on" is enabled.

Try using robocopy instead.

answered on Server Fault Mar 8, 2018 by sippybear • edited Mar 8, 2018 by Alexei

User contributions licensed under CC BY-SA 3.0