Powershell – Start-BitsTransfer Works when triggered manually from powershell but not using a windows service

bitsJenkinspowershell

I'm using BITS to transfer file from one windows 2008 R2 server to the other. When I run the command manually from powershell or regular command line it works perfectly. Now when I use the same command as part of a build script in Jenkins it fails with the following error :

Start-BitsTransfer : Cannot find path '\192.168.1.210\C$' because it does not exist.

Jenkins runs as a windows service under "Local System" account. I thought changing the windows service to run under "Network Service" account might help, but that's not the case either.

Is there's some security reason which does not allow BITS to run from a windows service?

Here's the Powershell script I have deploy.ps1:

Function Get-PSCredential($User,$Password)
{
 $SecPass = convertto-securestring -asplaintext -string $Password -force
 $Creds = new-object System.Management.Automation.PSCredential -argumentlist $User,$SecPass
 Return $Creds
}

$credential = Get-PSCredential -User jenkins -Password jenkins

Import-Module BitsTransfer
Start-BitsTransfer -source c:\file.zip -destination \\192.168.1.210\C$\Website -credential $credential

Confirming again, the above powershell script works perfectly fine when i trigger it manually myself using powershell or windows command.

This is the command I use in Jenkins to trigger the script:

Powershell.exe -noprofile -executionpolicy Bypass -file C:\deploy.ps1 

Best Answer

Probably not the answer you were hoping for:

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.

From MSDN, where it seems like it's not possible...

Related Topic