I'm trying to set up a service where I can transfer a number of files to our IIS server using Microsoft BITS.
I've installed the required components on the IIS server and have successfully uploaded several files but it is unreliable as soon as I add authentication to the virtual directory.
I'm using this code fragment to do the transfer:
Get-ChildItem $sourcePath | % {
try
{
Start-BitsTransfer -Source $_.FullName -Destination "$destinationPath/$_" -TransferType Upload -Authentication Basic -Credential $credentials
}
catch
{
$_
}
}
If I leave out the authentication and credentials parameters and enable anonymous authentication, it works every time.
If I turn off anonymous authentication, add basic authentication and set $credentials
to be my domain and username, it asks for the password and works every time. Same with digest authentication.
If I use basic authentication and set up credentials with the following code, it works the first time and fails after that. If I wait a bit and try again, it sometimes resets and works first time then fails after that. Same with digest authentication.
The code that I use to set up the credentials is as follows:
$username = "mydomain\myuser"
$password = "mypassword"
$securePassword = ConvertTo-SecureString -String $password -AsPlainText -Force
$credentials = New-Object System.Management.Automation.PSCredential -ArgumentList ($username, $securePassword)
The error that it gives when it fails is:
Start-BitsTransfer : Value does not fall within the expected range.
At D:\Projects\Powershell\MoveFiles.ps1:48 char:9
+ Start-BitsTransfer -Source $_.FullName -Destination "$destinationPath/$_ ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Start-BitsTransfer], ArgumentException
+ FullyQualifiedErrorId : System.ArgumentException,Microsoft.BackgroundIntelligentTransfer.Management.NewBitsTransferCommand
The results are the same whether I use HTTP or HTTPS and the failed attempts aren't present in the IIS logs.
Has anyone got any ideas what the problem is? I think that it is a Powershell problem because it does work if I type in the password rather than set it up in Powershell.
A final bit of information that I've discovered... I wrote a simple BITS upload program using Sharp BITS and it worked with no issues.