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

6,135

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.

Share:
6,135

Related videos on Youtube

Alexei
Author by

Alexei

C#.NET & SQL Server developer with a strong Angular flavor. If you are interested in a software development community with less drama and more tolerance to "opinioned" questions, check Software Development Codidact.

Updated on September 18, 2022

Comments

  • Alexei
    Alexei over 1 year

    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?

  • Alexei
    Alexei about 6 years
    Yes, robocopy is much better as it covers everything I need (progress and retrials) without the need of interactive context. Also, BITS requires custom code for deep copy of directories. Thanks.