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

newbie asked:

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 ‘$’ 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 -destination \$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 

My answer:

Bsaed on the fact that it starts with a single backslash in the error message, it looks like an escaping problem. PowerShell has an annoying habit of treating anything that begins with a backslash as a regular expression.

Try escaping that parameter. Three possible ways come to mind:

  1. Simply escape the first backslash:

  2. Single quote the whole parameter (don’t double quote it because $ is treated specially in double quoted items):

  3. Double all the backslashes:


View the full question and answer on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.